Pyglet,如何让ESCAPE键不关闭窗口?

发布于 2024-08-26 07:32:37 字数 239 浏览 3 评论 0原文

我正在编写一个小示例程序,我想覆盖 ESC 关闭应用程序的默认 pyglet 行为。我有一些东西:

window = pyglet.window.Window()
@window.event
def on_key_press(symbol, modifiers):
    if symbol == pyglet.window.key.ESCAPE:
        pass

但这似乎不起作用。

I am writing a small sample program and I would like to override the default pyglet's behavioyr of ESC closing the app. I have something to the extent of:

window = pyglet.window.Window()
@window.event
def on_key_press(symbol, modifiers):
    if symbol == pyglet.window.key.ESCAPE:
        pass

but that does not seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乖乖公主 2024-09-02 07:32:37

我知道这个问题很老了,但以防万一。您必须返回 pyglet.event.EVENT_HANDLED 以防止默认行为。我没有测试它,但理论上这应该有效:

@window.event
def on_key_press(symbol, modifiers):
    if symbol == pyglet.window.key.ESCAPE:
        return pyglet.event.EVENT_HANDLED

I know the question is old, but just in case. You've got to return pyglet.event.EVENT_HANDLED to prevent default behaviour. I didn't test it, but in theory this should work:

@window.event
def on_key_press(symbol, modifiers):
    if symbol == pyglet.window.key.ESCAPE:
        return pyglet.event.EVENT_HANDLED
孤独岁月 2024-09-02 07:32:37

对我来说也一样。这个问题很老了,但我发现您应该使用窗口处理程序机制,从而使当前事件不会进一步传播。

您可以阻止剩余的事件
堆栈中的处理程序从接收
通过返回真值来事件。
以下事件处理程序,当
推到窗户上,将阻止
退出键退出
程序:

def on_key_press(symbol, modifiers):
    if symbol == key.ESCAPE:
        return True

window.push_handlers(on_key_press)

这是链接

Same for me. The question is old, but I've found out that you should use window handlers mechanisms, thus making the current event not to propagate further.

You can prevent the remaining event
handlers in the stack from receiving
the event by returning a true value.
The following event handler, when
pushed onto the window, will prevent
the escape key from exiting the
program:

def on_key_press(symbol, modifiers):
    if symbol == key.ESCAPE:
        return True

window.push_handlers(on_key_press)

Here is that link

木格 2024-09-02 07:32:37

pyglet-users 的 Google 群组中,建议可能会使窗口过载。 Window.on_key_press(),尽管没有它的代码示例。

On the Google group for pyglet-users it is suggest could overload the window.Window.on_key_press(), although there are no code example of it.

回眸一笑 2024-09-02 07:32:37

实际上很简单,子类化 Window 并覆盖 on_key_press,如下所示:

class MyWindow(pyglet.window.Window):  
    def on_key_press(self, symbol, modifiers):  
        if symbol == key.ESCAPE:  
            return pyglet.event.EVENT_HANDLED  

It's simple actually, subclass Window and overide the on_key_press, like this:

class MyWindow(pyglet.window.Window):  
    def on_key_press(self, symbol, modifiers):  
        if symbol == key.ESCAPE:  
            return pyglet.event.EVENT_HANDLED  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文