Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def on_key_press(self, symbol, modifiers):
if symbol == pyglet.window.key.ESCAPE:
pyglet.app.exit()
elif symbol == pyglet.window.key.F and modifiers == pyglet.window.key.MOD_CTRL:
self.show_fps = not self.show_fps
elif symbol == pyglet.window.key.SPACE:
self.button.on_key_press(symbol, modifiers)
Keyboard.NUM_INSERT = pyglet.window.key.NUM_INSERT
Keyboard.NUM_DELETE = pyglet.window.key.NUM_DELETE
Keyboard.NUM_EQUAL = pyglet.window.key.NUM_EQUAL
Keyboard.NUM_MULTIPLY = pyglet.window.key.NUM_MULTIPLY
Keyboard.NUM_ADD = pyglet.window.key.NUM_ADD
Keyboard.NUM_SEPARATOR = pyglet.window.key.NUM_SEPARATOR
Keyboard.NUM_SUBTRACT = pyglet.window.key.NUM_SUBTRACT
Keyboard.NUM_DECIMAL = pyglet.window.key.NUM_DECIMAL
Keyboard.NUM_DIVIDE = pyglet.window.key.NUM_DIVIDE
Keyboard.NUM_0 = pyglet.window.key.NUM_0
Keyboard.NUM_1 = pyglet.window.key.NUM_1
Keyboard.NUM_2 = pyglet.window.key.NUM_2
Keyboard.NUM_3 = pyglet.window.key.NUM_3
Keyboard.NUM_4 = pyglet.window.key.NUM_4
Keyboard.NUM_5 = pyglet.window.key.NUM_5
Keyboard.NUM_6 = pyglet.window.key.NUM_6
Keyboard.NUM_7 = pyglet.window.key.NUM_7
Keyboard.NUM_8 = pyglet.window.key.NUM_8
Keyboard.NUM_9 = pyglet.window.key.NUM_9
# Function keys
Keyboard.F1 = pyglet.window.key.F1
Keyboard.F2 = pyglet.window.key.F2
Keyboard.F3 = pyglet.window.key.F3
Keyboard.F4 = pyglet.window.key.F4
Keyboard.F5 = pyglet.window.key.F5
Keyboard.F6 = pyglet.window.key.F6
Keyboard.F7 = pyglet.window.key.F7
Keyboard.F8 = pyglet.window.key.F8
Keyboard.F9 = pyglet.window.key.F9
Keyboard.F10 = pyglet.window.key.F10
def _get_symbol_and_modifiers(ev):
sym = c_uint32()
carbon.GetEventParameter(ev, kEventParamKeyCode,
typeUInt32, c_void_p(), sizeof(sym), c_void_p(), byref(sym))
modifiers = c_uint32()
carbon.GetEventParameter(ev, kEventParamKeyModifiers,
typeUInt32, c_void_p(), sizeof(modifiers), c_void_p(),
byref(modifiers))
symbol = keymap.get(sym.value, None)
if symbol is None:
symbol = key.user_key(sym.value)
return (symbol, CarbonWindow._map_modifiers(modifiers.value))
def run():
# setup visualization and user input
window = pyglet.window.Window(width=600, height=600, visible=False)
keyboard = key.KeyStateHandler()
window.push_handlers(keyboard)
# help text
label = pyglet.text.Label('ESC: quit, arrow-keys: move',
font_size=20,
x=window.width//2, y=window.height//20,
anchor_x='center', anchor_y='center')
# build Rob the robot
rob_mass = 1 # 1 kg
rob_radius = 0.1 * M_TO_PIXELS # 0.1 meter radius, converted to pixels for display
rob_I = pymunk.moment_for_circle(rob_mass, 0, rob_radius) # moment of inertia for disk
rob_body = pymunk.Body(rob_mass, rob_I)
rob_body.position = 300, 300
rob_shape = pymunk.Circle(rob_body, rob_radius)
rob_shape.color = 255, 0, 0 # red
Keyboard.EXECUTE = pyglet.window.key.EXECUTE
Keyboard.INSERT = pyglet.window.key.INSERT
Keyboard.UNDO = pyglet.window.key.UNDO
Keyboard.REDO = pyglet.window.key.REDO
Keyboard.MENU = pyglet.window.key.MENU
Keyboard.FIND = pyglet.window.key.FIND
Keyboard.CANCEL = pyglet.window.key.CANCEL
Keyboard.HELP = pyglet.window.key.HELP
Keyboard.BREAK = pyglet.window.key.BREAK
Keyboard.MODESWITCH = pyglet.window.key.MODESWITCH
Keyboard.SCRIPTSWITCH = pyglet.window.key.SCRIPTSWITCH
Keyboard.FUNCTION = pyglet.window.key.FUNCTION
# Number pad
Keyboard.NUMLOCK = pyglet.window.key.NUMLOCK
Keyboard.NUM_SPACE = pyglet.window.key.NUM_SPACE
Keyboard.NUM_TAB = pyglet.window.key.NUM_TAB
Keyboard.NUM_ENTER = pyglet.window.key.NUM_ENTER
Keyboard.NUM_F1 = pyglet.window.key.NUM_F1
Keyboard.NUM_F2 = pyglet.window.key.NUM_F2
Keyboard.NUM_F3 = pyglet.window.key.NUM_F3
Keyboard.NUM_F4 = pyglet.window.key.NUM_F4
Keyboard.NUM_HOME = pyglet.window.key.NUM_HOME
Keyboard.NUM_LEFT = pyglet.window.key.NUM_LEFT
Keyboard.NUM_UP = pyglet.window.key.NUM_UP
Keyboard.NUM_RIGHT = pyglet.window.key.NUM_RIGHT
Keyboard.NUM_DOWN = pyglet.window.key.NUM_DOWN
Keyboard.NUM_PRIOR = pyglet.window.key.NUM_PRIOR
Keyboard.NUM_PAGE_UP = pyglet.window.key.NUM_PAGE_UP
Keyboard.NUM_NEXT = pyglet.window.key.NUM_NEXT
Keyboard.NUM_PAGE_DOWN = pyglet.window.key.NUM_PAGE_DOWN
Keyboard.NUM_END = pyglet.window.key.NUM_END
Keyboard.NUM_HOME = pyglet.window.key.NUM_HOME
Keyboard.NUM_LEFT = pyglet.window.key.NUM_LEFT
Keyboard.NUM_UP = pyglet.window.key.NUM_UP
Keyboard.NUM_RIGHT = pyglet.window.key.NUM_RIGHT
Keyboard.NUM_DOWN = pyglet.window.key.NUM_DOWN
Keyboard.NUM_PRIOR = pyglet.window.key.NUM_PRIOR
Keyboard.NUM_PAGE_UP = pyglet.window.key.NUM_PAGE_UP
Keyboard.NUM_NEXT = pyglet.window.key.NUM_NEXT
Keyboard.NUM_PAGE_DOWN = pyglet.window.key.NUM_PAGE_DOWN
Keyboard.NUM_END = pyglet.window.key.NUM_END
Keyboard.NUM_BEGIN = pyglet.window.key.NUM_BEGIN
Keyboard.NUM_INSERT = pyglet.window.key.NUM_INSERT
Keyboard.NUM_DELETE = pyglet.window.key.NUM_DELETE
Keyboard.NUM_EQUAL = pyglet.window.key.NUM_EQUAL
Keyboard.NUM_MULTIPLY = pyglet.window.key.NUM_MULTIPLY
Keyboard.NUM_ADD = pyglet.window.key.NUM_ADD
Keyboard.NUM_SEPARATOR = pyglet.window.key.NUM_SEPARATOR
Keyboard.NUM_SUBTRACT = pyglet.window.key.NUM_SUBTRACT
Keyboard.NUM_DECIMAL = pyglet.window.key.NUM_DECIMAL
Keyboard.NUM_DIVIDE = pyglet.window.key.NUM_DIVIDE
Keyboard.NUM_0 = pyglet.window.key.NUM_0
Keyboard.NUM_1 = pyglet.window.key.NUM_1
Keyboard.NUM_2 = pyglet.window.key.NUM_2
Keyboard.NUM_3 = pyglet.window.key.NUM_3
Keyboard.NUM_4 = pyglet.window.key.NUM_4
Keyboard.NUM_5 = pyglet.window.key.NUM_5
Keyboard.NUM_6 = pyglet.window.key.NUM_6
Keyboard.NUM_7 = pyglet.window.key.NUM_7
Keyboard.NUM_8 = pyglet.window.key.NUM_8
Keyboard.NUM_9 = pyglet.window.key.NUM_9
modifiers |= key.MOD_SHIFT
if _user32.GetKeyState(VK_CONTROL) & 0xff00:
modifiers |= key.MOD_CTRL
if _user32.GetKeyState(VK_LWIN) & 0xff00:
modifiers |= key.MOD_WINDOWS
if _user32.GetKeyState(VK_CAPITAL) & 0x00ff: # toggle
modifiers |= key.MOD_CAPSLOCK
if _user32.GetKeyState(VK_NUMLOCK) & 0x00ff: # toggle
modifiers |= key.MOD_NUMLOCK
if _user32.GetKeyState(VK_SCROLL) & 0x00ff: # toggle
modifiers |= key.MOD_SCROLLLOCK
if key_lParam:
if key_lParam & (1 << 29):
modifiers |= key.MOD_ALT
elif _user32.GetKeyState(VK_MENU) < 0:
modifiers |= key.MOD_ALT
return modifiers
def on_key_release(self, key, modifiers):
"""Handle pyglet key release. Invoke key release methods and
deactivate key hold functions
"""
key_mod = (key, modifiers & self.MODIFIER_MASK)
if key_mod in self._key_release_map:
self._key_release_map[key_mod]()
self.held_keys.discard(key_mod)
if __name__ == '__main__':
import pyglet
class TestKeyControls(KeyControls):
MODIFIER_MASK = ~(key.MOD_NUMLOCK | key.MOD_SCROLLLOCK | key.MOD_CTRL)
remapped = False
@KeyControls.key_hold(key.UP)
@KeyControls.key_hold(key.W)
def up(self, dt):
print('UP!')
@KeyControls.key_hold(key.LEFT)
@KeyControls.key_hold(key.A)
def left(self, dt):
print('LEFT!')
@KeyControls.key_hold(key.RIGHT)
@KeyControls.key_hold(key.D)
def right(self, dt):
def _get_modifiers(self, key_lParam=0):
modifiers = 0
if _user32.GetKeyState(VK_SHIFT) & 0xff00:
modifiers |= key.MOD_SHIFT
if _user32.GetKeyState(VK_CONTROL) & 0xff00:
modifiers |= key.MOD_CTRL
if _user32.GetKeyState(VK_LWIN) & 0xff00:
modifiers |= key.MOD_WINDOWS
if _user32.GetKeyState(VK_CAPITAL) & 0x00ff: # toggle
modifiers |= key.MOD_CAPSLOCK
if _user32.GetKeyState(VK_NUMLOCK) & 0x00ff: # toggle
modifiers |= key.MOD_NUMLOCK
if _user32.GetKeyState(VK_SCROLL) & 0x00ff: # toggle
modifiers |= key.MOD_SCROLLLOCK
if key_lParam:
if key_lParam & (1 << 29):
modifiers |= key.MOD_ALT
elif _user32.GetKeyState(VK_MENU) < 0:
modifiers |= key.MOD_ALT
return modifiers
def _get_modifiers(self, key_lParam=0):
modifiers = 0
if _user32.GetKeyState(VK_SHIFT) & 0xff00:
modifiers |= key.MOD_SHIFT
if _user32.GetKeyState(VK_CONTROL) & 0xff00:
modifiers |= key.MOD_CTRL
if _user32.GetKeyState(VK_LWIN) & 0xff00:
modifiers |= key.MOD_WINDOWS
if _user32.GetKeyState(VK_CAPITAL) & 0x00ff: # toggle
modifiers |= key.MOD_CAPSLOCK
if _user32.GetKeyState(VK_NUMLOCK) & 0x00ff: # toggle
modifiers |= key.MOD_NUMLOCK
if _user32.GetKeyState(VK_SCROLL) & 0x00ff: # toggle
modifiers |= key.MOD_SCROLLLOCK
if key_lParam:
if key_lParam & (1 << 29):
modifiers |= key.MOD_ALT
elif _user32.GetKeyState(VK_MENU) < 0:
modifiers |= key.MOD_ALT
return modifiers