def KeyPress(self, keyName, interval=0.25): keysym = string_to_keysym(keyName) if keysym == 0: # wrong key name is used, try to mapping in the dict keysym = string_to_keysym(key_mapping[keyName]) if keysym == 0: log.error( "keyName %s can not be found, please check the correct keyname by typing xev in terminal" ) return keycode = self._display.keysym_to_keycode( keysym) # 1 and ! will have same keycode self.SetFocus() if NeedShift(keyName): fake_input(self._display, X.KeyPress, 50) # 50 is keycode for Shift_L fake_input(self._display, X.KeyPress, keycode) time.sleep(interval) fake_input(self._display, X.KeyRelease, keycode) if NeedShift(keyName): fake_input(self._display, X.KeyRelease, 50) # 50 is keycode for Shift_L self._display.flush()
def lookup_character_value(self, character): """ Looks up the keysym for the character then returns the keycode mapping for that keysym. """ ch_keysym = string_to_keysym(character) if ch_keysym == 0: ch_keysym = string_to_keysym(special_X_keysyms[character]) return self.display.keysym_to_keycode(ch_keysym)
def get_key_state(key): keysym = string_to_keysym(key) keycode = dsp.keysym_to_keycode(keysym) if keycode == 0: return 0 keys = dsp.query_keymap() return (keys[keycode >> 3] >> (keycode & 0x07)) & 0x01
def _init_xlib(self): """Setup python-xlib components in the PyGTK event loop""" self.xdisp = Display() self.xroot = self.xdisp.screen().root # We want to receive KeyPress events self.xroot.change_attributes(event_mask=X.KeyPressMask) # unrecognized shortkeys now will be looked up in a hardcoded dict # and replaced by valid names like ',' -> 'comma' # while generating the self.keys dict self.keys = dict() for key in self._keys: transKey = key if key in KEYLOOKUP: transKey = KEYLOOKUP[key] code = self.xdisp.keysym_to_keycode(string_to_keysym(transKey)) self.keys[code] = self._keys[key] # Resolve strings to X11 mask constants for the modifier mask try: modmask = reduce(operator.ior, [getattr(X, "%sMask" % x) for x in self._modkeys]) self._movemodmask = reduce(operator.ior, [getattr(X, "%sMask" % x) for x in self._movemodkeys]) except Exception, err: logging.error("Error while resolving modifier key mask: %s", err) logging.error("Not binding keys for safety reasons. " "(eg. What if Ctrl+C got bound?)") modmask = 0 movemodmask = 0
def _init_xlib(self): """Setup python-xlib components in the PyGTK event loop""" disp = Display() self.xroot = disp.screen().root # We want to receive KeyPress events self.xroot.change_attributes(event_mask = X.KeyPressMask) self._key_n_mask = {} for key in self._keys: if len(key.split('-')) > 1: more_mask = key.split('-')[0:-1] real_key = key.split('-')[-1] else: more_mask = [] real_key = key try: modmask = reduce(operator.ior, [getattr(X, "%sMask" % x) for x in (self._modkeys + more_mask)]) except Exception, err: logging.error("Error while resolving modifier key mask: %s", err) logging.error("Not binding keys for safety reasons. (eg. What if Ctrl+C got bound?)") modmask = 0 else: keycode = disp.keysym_to_keycode(string_to_keysym(real_key)) self._key_n_mask[(keycode, modmask)] = self._keys[key] #Ignore all combinations of Mod2 (NumLock) and Lock (CapsLock) for ignored in [0, X.Mod2Mask, X.LockMask, X.Mod2Mask | X.LockMask]: self.xroot.grab_key(keycode, modmask | ignored, 1, X.GrabModeAsync, X.GrabModeAsync)
def _init_xlib(self): """Setup python-xlib components in the PyGTK event loop""" self.xdisp = Display() self.xroot = self.xdisp.screen().root # We want to receive KeyPress events self.xroot.change_attributes(event_mask=X.KeyPressMask) # unrecognized shortkeys now will be looked up in a hardcoded dict # and replaced by valid names like ',' -> 'comma' # while generating the self.keys dict self.keys = dict() for key in self._keys: transKey = key if key in KEYLOOKUP: transKey = KEYLOOKUP[key] code = self.xdisp.keysym_to_keycode(string_to_keysym(transKey)) self.keys[code] = self._keys[key] # Resolve strings to X11 mask constants for the modifier mask try: modmask = reduce(operator.ior, [getattr(X, "%sMask" % x) for x in self._modkeys]) self._movemodmask = reduce( operator.ior, [getattr(X, "%sMask" % x) for x in self._movemodkeys]) except Exception, err: logging.error("Error while resolving modifier key mask: %s", err) logging.error("Not binding keys for safety reasons. " "(eg. What if Ctrl+C got bound?)") modmask = 0 movemodmask = 0
def key_state(self, key): """ gives you the current keystate @todo: Sparate upper- and lowercase """ keysym = string_to_keysym(key) keycode = self.display.keysym_to_keycode(keysym) if (keycode == 0): return False keys = self.display.query_keymap() return (keys[keycode >> 3] >> (keycode & 0x07)) & 0x01 > 0
def key_state(self, key): """ gives you the current keystate @todo: Sparate upper- and lowercase """ keysym = string_to_keysym(key) keycode = self.display.keysym_to_keycode(keysym) if(keycode == 0): return False keys = self.display.query_keymap() return (keys[keycode >> 3] >> (keycode & 0x07)) & 0x01 > 0
def _init_xlib(self): """Setup python-xlib components in the PyGTK event loop""" self.xdisp = Display() self.xroot = self.xdisp.screen().root # We want to receive KeyPress events self.xroot.change_attributes(event_mask=X.KeyPressMask) self.keys = dict([(self.xdisp.keysym_to_keycode(string_to_keysym(x)), self._keys[x]) for x in self._keys]) # Resolve strings to X11 mask constants for the modifier mask try: modmask = reduce(operator.ior, [getattr(X, "%sMask" % x) for x in self._modkeys]) except Exception, err: logging.error("Error while resolving modifier key mask: %s", err) logging.error("Not binding keys for safety reasons. (eg. What if Ctrl+C got bound?)") modmask = 0
def __init__(self, mod = 'MOD_SHIFT', key = 'VK_SPACE'): qtcore.QThread.__init__(self) keysym = string_to_keysym('space') self.modifiers = X.ShiftMask | X.ControlMask self.disp = Display() self.key = self.disp.keysym_to_keycode(keysym) self.root = self.disp.screen().root self.root.change_attributes(event_mask=X.KeyPressMask) self.root.grab_key(self.key, self.modifiers, 0, False, X.GrabModeAsync, X.GrabModeAsync) self.root.grab_key(self.key, self.modifiers | X.LockMask, 0, False, X.GrabModeAsync, X.GrabModeAsync) self.root.grab_key(self.key, self.modifiers | X.Mod2Mask, 0, False, X.GrabModeAsync, X.GrabModeAsync) self.root.grab_key(self.key, self.modifiers | X.LockMask | X.Mod2Mask, 0, False, X.GrabModeAsync, X.GrabModeAsync) self.root.grab_key(self.key, self.modifiers | X.Mod3Mask | X.LockMask, 0, False, X.GrabModeAsync, X.GrabModeAsync) self.root.grab_key(self.key, self.modifiers | X.Mod3Mask | X.Mod2Mask, 0, False, X.GrabModeAsync, X.GrabModeAsync) self.root.grab_key(self.key, self.modifiers | X.Mod3Mask | X.LockMask | X.Mod2Mask, 0, False, X.GrabModeAsync, X.GrabModeAsync)
def send_key(key, state, up=False): """Send a key press or release to the QEMU window.""" time.sleep(0.1) t = KeyPress if up: t = KeyRelease sym = string_to_keysym(key) ke = t( time=int(time.time()), root=display.screen().root, window=qemu_win, same_screen=0, child=Xlib.X.NONE, root_x = 0, root_y = 0, event_x = 0, event_y = 0, state = 0xc, detail = display.keysym_to_keycode(sym) ) qemu_win.send_event(ke) display.flush()
def send_key(key, state, up=False): """Send a key press or release to the QEMU window.""" time.sleep(0.1) t = KeyPress if up: t = KeyRelease sym = string_to_keysym(key) ke = t(time=int(time.time()), root=display.screen().root, window=qemu_win, same_screen=0, child=Xlib.X.NONE, root_x=0, root_y=0, event_x=0, event_y=0, state=0xc, detail=display.keysym_to_keycode(sym)) qemu_win.send_event(ke) display.flush()
def _init_xlib(self): """Setup python-xlib components in the PyGTK event loop""" disp = Display() self.xroot = disp.screen().root # We want to receive KeyPress events self.xroot.change_attributes(event_mask=X.KeyPressMask) self._key_n_mask = {} for key in self._keys: if len(key.split('-')) > 1: more_mask = key.split('-')[0:-1] real_key = key.split('-')[-1] else: more_mask = [] real_key = key try: modmask = reduce(operator.ior, [ getattr(X, "%sMask" % x) for x in (self._modkeys + more_mask) ]) except Exception, err: logging.error("Error while resolving modifier key mask: %s", err) logging.error( "Not binding keys for safety reasons. (eg. What if Ctrl+C got bound?)" ) modmask = 0 else: keycode = disp.keysym_to_keycode(string_to_keysym(real_key)) self._key_n_mask[(keycode, modmask)] = self._keys[key] #Ignore all combinations of Mod2 (NumLock) and Lock (CapsLock) for ignored in [ 0, X.Mod2Mask, X.LockMask, X.Mod2Mask | X.LockMask ]: self.xroot.grab_key(keycode, modmask | ignored, 1, X.GrabModeAsync, X.GrabModeAsync)
elif not horizontal and amount >= 0: button = 5 # down else: return for _ in range(abs(amount)): fake_input(_display, X.ButtonPress, button) _display.sync() fake_input(_display, X.ButtonRelease, button) _display.sync() _display = Display(":0") # repeat held keys _display.change_keyboard_control(auto_repeat_mode=X.AutoRepeatModeOn) key_codes = { "KEY_ESC": _display.keysym_to_keycode(string_to_keysym("Escape")), "KEY_1": _display.keysym_to_keycode(string_to_keysym("1")), "KEY_2": _display.keysym_to_keycode(string_to_keysym("2")), "KEY_3": _display.keysym_to_keycode(string_to_keysym("3")), "KEY_4": _display.keysym_to_keycode(string_to_keysym("4")), "KEY_5": _display.keysym_to_keycode(string_to_keysym("5")), "KEY_6": _display.keysym_to_keycode(string_to_keysym("6")), "KEY_7": _display.keysym_to_keycode(string_to_keysym("7")), "KEY_8": _display.keysym_to_keycode(string_to_keysym("8")), "KEY_9": _display.keysym_to_keycode(string_to_keysym("9")), "KEY_0": _display.keysym_to_keycode(string_to_keysym("0")), "KEY_MINUS": _display.keysym_to_keycode(string_to_keysym("minus")), "KEY_EQUAL": _display.keysym_to_keycode(string_to_keysym("equal")), "KEY_BACKSPACE": _display.keysym_to_keycode(string_to_keysym("Backspace")), "KEY_TAB": _display.keysym_to_keycode(string_to_keysym("Tab")), "KEY_Q": _display.keysym_to_keycode(string_to_keysym("q")),
def get_key_state(key): keysym = string_to_keysym(key) keycode = dsp.keysym_to_keycode(keysym) if(keycode == 0): return 0 keys = dsp.query_keymap() return (keys[keycode >> 3] >> (keycode & 0x07)) & 0x01