Hi gurkesaft,
The style you show should work just fine, there's nothing wrong with
it.
Personally I prefer to think of that application has-a window, and it
has-a keyboard handler
class KeyHandler(object):
def on_key_press(self, key, modifiers):
...
def on_key_release(self, key, modifiers):
...
class MyApplication(object):
def __init__(self):
self.win = Window(...)
self.keyhandler = KeyHandler()
self.win.push_handlers(self.keyhandler)
This idea can be extended to give your application more than one
keyboard handler, adding and removing them by calling win.push_handlers
() or win.remove_handlers() as needed. (eg. one handler that knows
about on-screen menus, that is pushed whenever a menu is on screen.
Another handler which knows how to move the player around, pushed
whenever the player's character is moveable)
But it's just a matter of personal preference. Your way should also be
just fine.
On Nov 30, 3:56 pm, gurkesaft <[email protected]> wrote:
> Thanks everyone. Maybe this is the wrong way to do it, but I'm
> deriving a class from Window() so I have code that looks something
> more like
>
> class MyFancyWindow(pyglet.window.Window):
>
> def on_key_press(key, modifiers):
> ...
>
> def on_key_release(key, modifiers):
> ...
>
> I've seen several examples of this style of coding. Is there no way to
> write pyglet apps this way?
>
> On Nov 22, 9:55 am, Jonathan Hartley <[email protected]> wrote:
>
> > On Nov 22, 2:09 am, Tristam MacDonald <[email protected]> wrote:
>
> > > On Sat, Nov 21, 2009 at 9:00 PM, claudio canepa <[email protected]>
> > > wrote:
>
> > > > On Sat, Nov 21, 2009 at 2:37 PM, gurkesaft <[email protected]>
> > > > wrote:
>
> > > >> Hello,
>
> > > >> I'm using pyglet 1.1.3 on windows XP, python2.6. In deriving a class
> > > >> with Window() as a base, I noticed that overriding the on_key_release
> > > >> () did not seem to work. None of the code in this method would run
> > > >> when I released a key, while on_key_press() worked fine.
>
> > > >> From the command line, it seems pretty clear:
>
> > > >> >>> import pyglet
> > > >> >>> w = pyglet.window.Window()
>
> > > >> Then typing
>
> > > >> >>> w.on_key_press
>
> > > >> gives the result
>
> > > >> <bound method Win32Window.on_key_press of
> > > >> <pyglet.window.win32.Win32Window object at 0x03328EB0>>
>
> > > >> and typing
>
> > > >> >>> w.on_key_release
>
> > > >> gives the result
>
> > > >> AttributeError: 'Win32Window' object has no attribute 'on_key_release'
>
> > > >> While typing, auto-complete also shows me that on_key_release is not
> > > >> in the list. So it seems I don't have access to the on_key_release,
> > > >> but it is in the documentation.
>
> > > >> Anyone else experiencing this?
>
> > > >> Regards,
> > > >> Jack
>
> > > > I see the same, but it is fine:
> > > > 1. run the sample events.py found in the examples directory ( pyglet doc
> > > > and examples package), you will see that pyglet knows about key release
> > > > events.
>
> > > > 2. try this simple script:
>
> > > > import pyglet
> > > > window = pyglet.window.Window()
> > > > @window.event
> > > > def on_key_release(key, modifiers):
> > > > print 'released'
> > > > pyglet.app.run()
>
> > > > it will show that the key release events hit the on_key_release defined
> > > > in
> > > > the script.
>
> > > To expand on that, the only reason that pyglet.window.Window has an
> > > attribute 'on_key_pressed', is that it contains a default handler for key
> > > down events (quits if the ESC key is pressed).
>
> > > --
> > > Tristam MacDonaldhttp://swiftcoder.wordpress.com/
>
> > Right - there is no 'on_key_release' method on window.Window because
> > that class doesn't have any *handler* for those events. But the events
> > are still happening as they should, and you can opt to add your own
> > handler, like claudio's, if you choose.
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en.