Hello,
I don't quite understand your description of the problem. Running your code
on my machine, pressing a key prints *A key was pressed* followed by a
blank line. If I press '1', I have the additional message *Change Things*.
>From now on, pressing *any keys* prints only *Ole!*, and nothing else.
This is the expected behaviour. Using the decorator will *replace* the
current event handler on the top of the handlers stack. In your example
there is only one level of event handlers.
When you want to have different event handlers, you normally use the
push_handlers() method. When you're done, you use the pop_handlers() to
remove it. While several event handlers are stacked, you can let a previous
handler still run by returning EVENT_UNHANDLED from the newer event
handler. Otherwise returning EVENT_HANDLED will stop the event propagation.
Here is an example using push_handlers and pop_handlers.
import pyglet
from pyglet.event import EVENT_HANDLED, EVENT_UNHANDLED
window = pyglet.window.Window(width=200, height=200,
caption="Event handlers demo")
@window.event
def on_key_press(key, modifiers):
print('A key was pressed')
if key == pyglet.window.key._1:
print("Stacking `ole` event handler")
window.push_handlers(on_key_press=ole)
elif key == pyglet.window.key._2:
print("Stacking `zamzam` event handler")
window.push_handlers(on_key_press=zamzam)
print("-" * 60)
def ole(key, modifiers):
print('Ole! This handler lets the event propagate. '
'To remove the event, press 1')
if key == pyglet.window.key._1:
print('Removing `ole` as event handler')
window.pop_handlers()
# Here we need to stop propagation otherwise we are going to add
# ole again in the `on_key_press` event handler
return EVENT_HANDLED
return EVENT_UNHANDLED
def zamzam(key, modifiers):
print('Zamzam! This event handlers does not let the event propagate. '
' To remove the event, press 2')
if key == pyglet.window.key._2:
print('Removing `zamzam` as event handler')
window.pop_handlers()
return EVENT_HANDLED
pyglet.app.run()
Notice how `ole` allows you still to see the message from the previous
event handler while `zamzam` does not.
I hope this helps.
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.