Keyboards event on coco2d (python) and pyglet
Question: second question here, my first one was answer and really helped, so I'll try
again.

Here is the code and the explanation:

    
    
    import cocos
    from cocos.actions import *
    import pyglet
    from pyglet.window import key
    from pyglet.window.key import KeyStateHandler
    from cocos.director import director
    
    
    keys = KeyStateHandler()
    
    class ScaleTestLayer(cocos.layer.Layer):
        is_event_handler = True 
        def __init__(self):
            super( ScaleTestLayer, self ).__init__()
            self.sprite = cocos.sprite.Sprite('grossini.png')
            self.sprite.position = 320,240
            self.drag = False
            self.add(self.sprite)
            self.rect = self.sprite.get_rect()
    
        def on_mouse_release(self, x, y, buttons, modifiers):
            if self.rect.contains(x, y) == True:
                if self.drag == False:
                    scale = ScaleBy(5, duration=1)
                    if buttons == pyglet.window.mouse.LEFT:
                        self.sprite.do(scale)
                    if buttons == pyglet.window.mouse.MIDDLE:
                        rotate = RotateBy(180, 1)
                        self.sprite.do(rotate)
                    if buttons == pyglet.window.mouse.RIGHT:
                        scale = (Reverse(scale))
                        self.sprite.do(scale)  
                else:
                    self.drag = False    
    
        def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
            if self.rect.contains(x, y):
                self.drag = True
                self.sprite.position = director.get_virtual_coordinates (x, y)
            self.rect.center = self.sprite.position
    
        def on_key_press(self, symbol, modifiers):
            if keys[key.SPACE]:
                jump = JumpBy((5,0), duration=0.5)
                self.sprite.do(jump)
            if keys[key.RIGHT]:
                move = MoveBy((10,0), 0.5)
                self.sprite.do(move)
    
    
    
    if __name__ == "__main__":
        cocos.director.director.init()
        test_layer = ScaleTestLayer ()
        main_scene = cocos.scene.Scene (test_layer)
        cocos.director.director.run (main_scene)
    

So here's my problem. When I press SPACE or RIGHT it does not execute the
action. I printed the value and keys[key.SPACE] is return False, even though I
do press those buttons. What am I missing? maybe some push_handlers? I don't
know how to use it.

Thanks beforehand.


Answer: Yes, you need to use push_handlers on the `KeyStateHandler` so that it is
updated. I have not used cocos2d myself, only pyglet, so this may not be the
best or proper way to do it, but it should get you going.

    
    
    if __name__ == '__main__':
        cocos.director.director.init()
        cocos.director.director.window.push_handlers(keys)
        # Create layer, run, etc.
    

[Here is the
page](http://www.pyglet.org/doc/api/pyglet.window.key.KeyStateHandler-
class.html) on using a `KeyStateHandler` in the pyglet documentation. In
pyglet you create the window yourself and attach it there, but in cocos2d the
window is handled by the director.

I also found [this
page](http://cocos2d.org/doc/programming_guide/handling_events.html) in the
cocos2d documentation, which may, or may not be, the proper way of handling
key presses with cocos2d.

