# If you want that your layer receives director.window events # you must set this variable to 'True' is_event_handler = True
def__init__(self):
super( KeyDisplay, self ).__init__()
self.text = cocos.text.Label("", x=100, y=280 )
# To keep track of which keys are pressed: self.keys_pressed = set() self.update_text() self.add(self.text)
defupdate_text(self): key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed] text = 'Keys: '+','.join (key_names) # Update self.text self.text.element.text = text
这是一个定义key_passed的集,这应该是任何时候按下的一组键。然后,这些代码依然是没有作用的。我们应该告诉图层,当我们某键被按下或释放时要更新这一个字符集。换句话说,我们需要添加一个事件句柄在图层中。添加事件句柄在图层中仅仅需要在添加一个方法,让其被某个事件被调用。这里有俩个事件可以被我们选择 on_key_press and on_key_release:
defon_key_press(self, key, modifiers): """This function is called when a key is pressed. 'key' is a constant indicating which key was pressed. 'modifiers' is a bitwise or of several constants indicating which modifiers are active at the time of the press (ctrl, shift, capslock, etc.) """
self.keys_pressed.add (key) self.update_text()
defon_key_release(self, key, modifiers): """This function is called when a key is released. 'key' is a constant indicating which key was pressed. 'modifiers' is a bitwise or of several constants indicating which modifiers are active at the time of the press (ctrl, shift, capslock, etc.) Constants are the ones from pyglet.window.key """
self.keys_pressed.remove (key) self.update_text()
defupdate_text(self): key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed] text = 'Keys: '+','.join (key_names) # Update self.text self.text.element.text = text
import cocos from cocos.actions import * import pyglet
classKeyDisplay(cocos.layer.Layer):
# If you want that your layer receives director.window events # you must set this variable to 'True' is_event_handler = True
def__init__(self):
super( KeyDisplay, self ).__init__()
self.text = cocos.text.Label("", x=100, y=280 )
# To keep track of which keys are pressed: self.keys_pressed = set() self.update_text() self.add(self.text)
defupdate_text(self): key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed] text = 'Keys: '+','.join (key_names) # Update self.text self.text.element.text = text defon_key_press(self, key, modifiers): """This function is called when a key is pressed. 'key' is a constant indicating which key was pressed. 'modifiers' is a bitwise or of several constants indicating which modifiers are active at the time of the press (ctrl, shift, capslock, etc.) """
self.keys_pressed.add (key) self.update_text()
defon_key_release(self, key, modifiers): """This function is called when a key is released. 'key' is a constant indicating which key was pressed. 'modifiers' is a bitwise or of several constants indicating which modifiers are active at the time of the press (ctrl, shift, capslock, etc.) Constants are the ones from pyglet.window.key """
self.keys_pressed.remove (key) self.update_text()
defmain(): cocos.director.director.init() hello_layer =KeyDisplay( ) #hello_layer.do( RotateBy(360, duration=10) ) # A scene that contains the layer hello_layer main_scene = cocos.scene.Scene (hello_layer)
# And now, start the application, starting with main_scene cocos.director.director.run (main_scene) cocos.director.event
if __name__ == '__main__': main()
鼠标事件
处理鼠标输入的过程是相似的。我们可以有三个事件来供我们选择:on_mouse_press, on_mouse_motion and on_mouse_drag。接着我们就可以定义我们的图层:
defon_mouse_motion(self, x, y, dx, dy): """Called when the mouse moves over the app window with no button pressed (x, y) are the physical coordinates of the mouse (dx, dy) is the distance vector covered by the mouse pointer since the last call. """ self.update_text (x, y)
defon_mouse_drag(self, x, y, dx, dy, buttons, modifiers): """Called when the mouse moves over the app window with some button(s) pressed (x, y) are the physical coordinates of the mouse (dx, dy) is the distance vector covered by the mouse pointer since the last call. 'buttons' is a bitwise or of pyglet.window.mouse constants LEFT, MIDDLE, RIGHT 'modifiers' is a bitwise or of pyglet.window.key modifier constants (values like 'SHIFT', 'OPTION', 'ALT') """ self.update_text (x, y)
defon_mouse_press(self, x, y, buttons, modifiers): """This function is called when any mouse button is pressed (x, y) are the physical coordinates of the mouse 'buttons' is a bitwise or of pyglet.window.mouse constants LEFT, MIDDLE, RIGHT 'modifiers' is a bitwise or of pyglet.window.key modifier constants (values like 'SHIFT', 'OPTION', 'ALT') """ self.posx, self.posy = director.get_virtual_coordinates (x, y) self.update_text (x,y)