Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top