How to get and handler a key hold event?

28 views
Skip to first unread message

siy tong

unread,
Mar 18, 2013, 6:54:38 AM3/18/13
to cocos-...@googlegroups.com
The cocos2d only supply on_key_press and on_key_release event hanlder ?
I am making a rpg game . I want to move the sprite when I hold a key .
How to implement that function?

siy tong

unread,
Mar 18, 2013, 10:28:46 PM3/18/13
to cocos-...@googlegroups.com
use self.schedule(self.update)

Gerardo Marset

unread,
Jun 5, 2013, 2:08:36 PM6/5/13
to cocos-...@googlegroups.com

Actually what you need is probably what's used in the platformer test (test/test_platformer.py).
First, import key:

    from pyglet.window import key

then get a key state handler:

    keyboard = key.KeyStateHandler()
    director.window.push_handlers(keyboard)

and then you can do something like:

    if keyboard[key.SPACE]:
        print "the space key is being held!"

Or you could just track the pressing and releasing of keys.
Reply all
Reply to author
Forward
0 new messages