pyglet.window.Window does not have on_key_release?

15 views
Skip to first unread message

gurkesaft

unread,
Nov 21, 2009, 12:37:11 PM11/21/09
to pyglet-users
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

claudio canepa

unread,
Nov 21, 2009, 9:00:33 PM11/21/09
to pyglet...@googlegroups.com
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.


--
claudio

Tristam MacDonald

unread,
Nov 21, 2009, 9:09:28 PM11/21/09
to pyglet...@googlegroups.com
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 MacDonald
http://swiftcoder.wordpress.com/

Jonathan Hartley

unread,
Nov 22, 2009, 9:55:02 AM11/22/09
to pyglet-users
On Nov 22, 2:09 am, Tristam MacDonald <swiftco...@gmail.com> wrote:
> On Sat, Nov 21, 2009 at 9:00 PM, claudio canepa <ccanep...@gmail.com> wrote:
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.

gurkesaft

unread,
Nov 30, 2009, 10:56:46 AM11/30/09
to pyglet-users
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?

claudio canepa

unread,
Nov 30, 2009, 11:11:11 AM11/30/09
to pyglet...@googlegroups.com
On Mon, Nov 30, 2009 at 12:56 PM, gurkesaft <jack....@gmail.com> 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?


 

A matter of taste, use the style you prefer.

--
claxo
 

Jonathan Hartley

unread,
Nov 30, 2009, 4:34:21 PM11/30/09
to pyglet-users
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.
Reply all
Reply to author
Forward
0 new messages