def _event_to_key(self, display, event): """Converts an *X* event to a :class:`KeyCode`. :param display: The current *X* display. :param event: The event to convert. :return: a :class:`pynput.keyboard.KeyCode` :raises IndexError: if the key code is invalid """ keycode = event.detail index = shift_to_index(display, event.state) # First try special keys... keysym = self._keycode_to_keysym(display, keycode, index) if keysym in self._SPECIAL_KEYS: return self._SPECIAL_KEYS[keysym] elif keysym in self._KEYPAD_KEYS: # We must recalculate the index if numlock is active; index 1 is the # one to use return self._KEYPAD_KEYS[self._keycode_to_keysym( display, keycode, bool(event.state & numlock_mask(display)))] # ...then try characters... name = KEYSYMS.get(keysym, None) if name is not None and name in SYMBOLS: char = SYMBOLS[name][1].upper() if index & 1 else SYMBOLS[name][1] if char in DEAD_KEYS: return KeyCode.from_dead(DEAD_KEYS[char]) else: return KeyCode.from_char(char) # ...and fall back on a virtual key code return KeyCode.from_vk(keysym)
def _event_to_key(self, display, event): """Converts an *X* event to a :class:`KeyCode`. :param display: The current *X* display. :param event: The event to convert. :return: a :class:`pynput.keyboard.KeyCode` :raises IndexError: if the key code is invalid """ keycode = event.detail index = shift_to_index(display, event.state) # First try special keys... keysym = self._keycode_to_keysym(display, keycode, index) if keysym in self._SPECIAL_KEYS: return self._SPECIAL_KEYS[keysym] # ...then try characters... name = KEYSYMS[keysym] if name in SYMBOLS: char = SYMBOLS[name][1].upper() if index & 1 else SYMBOLS[name][1] if char in DEAD_KEYS: return KeyCode.from_dead(DEAD_KEYS[char]) else: return KeyCode.from_char(char) # ...and fall back on a virtual key code return KeyCode.from_vk(keysym)