开发者

Pyglet, how to make the ESCAPE key not close the window?

开发者 https://www.devze.com 2022-12-24 06:50 出处:网络
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:

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 se开发者_如何学JAVAem to work.


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


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


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.


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  
0

精彩评论

暂无评论...
验证码 换一张
取 消