Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

pyglet

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.

like image 411
Bartosz Radaczyński Avatar asked Mar 22 '10 20:03

Bartosz Radaczyński


2 Answers

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
like image 117
Imbrondir Avatar answered Sep 30 '22 04:09

Imbrondir


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

like image 31
varnie Avatar answered Sep 30 '22 03:09

varnie