def __init__(self, window, bindings): self.__keyboard_map = {} class Keyboard: """Namespace for keyboard connectors""" pass self.keyboard = Keyboard() for name, key in bindings.get('keyboard', {}).items(): key, mod = self.__translate_key(name, key) key_input = KeyboardInput(name) log.debug("Map key", mod, key, 'to', name, key_input) assert (mod, key) not in self.__keyboard_map setattr(self.keyboard, name, key_input) if mod == KeyMod.ctrl: self.__keyboard_map[(KeyMod.lctrl, key)] = key_input self.__keyboard_map[(KeyMod.rctrl, key)] = key_input else: self.__keyboard_map[(mod, key)] = key_input self.mouse = MouseInput() self._hdlrs = [ window.inputs.on_keydown.connect(self.__on_keydown), window.inputs.on_keyup.connect(self.__on_keyup), window.inputs.on_mousemove.connect(self.__on_mousemove), window.inputs.on_mousedown.connect(self.__on_mousedown), window.inputs.on_mouseup.connect(self.__on_mouseup), ] self.__events = [] self.__held = set()
def poll(self): to_fire = self.__events self.__events = [] for func in to_fire: func() for key in self.__held: log.debug("Trigger held key", key) key.key_held()
def __on_keyup(self, keymod, keysym, keycode): log.debug("Got keyboard input:", keymod, keysym, keycode) i = self.__get_keyboard_input(keymod, keysym, keycode) if i is not None: self.__events.append(i.key_released) try: self.__held.remove(i) except: log.warn("Couldn't remove held key", keymod, keysym, keycode, "cleaning up") self.__held = set() log.debug("Got keyboard input:", keymod, keysym, keycode, "remove", i)
def __on_mouseup(self, button, keymod): log.debug("Got mouse up event", button, keymod) pass
def __on_keydown(self, keymod, keysym, keycode): i = self.__get_keyboard_input(keymod, keysym, keycode) if i is not None: self.__events.append(i.key_pressed) self.__held.add(i) log.debug("Got keyboard input:", keymod, keysym, keycode, "add", i)