def sendkey(keystroke): ctrl = alt = shift = 0 key = "" splitted = keystroke.split(" ") for stroke in splitted: if stroke == "Ctrl": ctrl = 1 elif stroke == "Shift": shift = 1 elif stroke == "Alt": alt = 1 elif stroke == "Space": key = char_to_keycode(" ") else: # an ordinary key key = char_to_keycode(stroke) if ctrl == 1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, ctrlkey) if alt == 1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, altkey) if shift == 1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, shiftkey) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, key) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, key) if ctrl == 1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, ctrlkey) if alt == 1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, altkey) if shift == 1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, shiftkey) display.sync()
def setAsBar(self): self.setWindowFlags(Qt.FramelessWindowHint) self.setWindowFlags(Qt.X11BypassWindowManagerHint) self.setGeometry( QRect(self._xpos, self._ypos, self._width, self._height)) x11window = display.create_resource_object('window', int(self.winId())) _ATOM = display.intern_atom("ATOM") _TYPE = display.intern_atom("_NET_WM_WINDOW_TYPE") _DOCK = display.intern_atom("_NET_WM_WINDOW_TYPE_DOCK") x11window.change_property(_TYPE, _ATOM, 32, [_DOCK]) if self._ypos < 100: x11window.change_property(atom_strut_partial, atom_cardinal, 32, [ 0, 0, self._height, 0, 0, 0, 0, 0, self._xpos, self._xpos + self._width, 0, 0 ]) x11window.change_property(atom_strut, atom_cardinal, 32, [0, 0, self._height, 0]) else: # bottom x11window.change_property(atom_strut_partial, atom_cardinal, 32, [ 0, 0, 0, self._height, 0, 0, 0, 0, self._xpos, self._xpos + self._width, self._ypos, self._ypos + self._height ]) x11window.change_property(atom_strut, atom_cardinal, 32, [0, 0, 0, self._height]) display.sync()
def send_string(window, text):#{{{ """ Send a string to a window """ display = Xlib.display.Display() Xlib.XK.load_keysym_group("xkb") keysym_map = dict((Xlib.XK.keysym_to_string(x), x) for x in latin1.__dict__.values() if type(x) is int ) for char_index in range(len(text)): char = text[char_index] if char in keysym_map: key_sym = keysym_map[char] else: if char == "\t": char = "Tab" elif char == "\n": char = "Return" key_sym = Xlib.XK.string_to_keysym(char) if not key_sym: continue key_codes = display.keysym_to_keycodes(key_sym) if not key_codes: continue key_code, index = key_codes[0] m_state = 0 if index & 1: # Shift m_state = 1 if index & 4 | index & 2: # Alt Grid m_state = m_state | 0x80 for m_type in (Xlib.protocol.event.KeyPress, Xlib.protocol.event.KeyRelease): ev = m_type(time=0, child=0, state=m_state, root=window.query_tree().root, window=window, same_screen=1, \ root_x=0, root_y=0, event_x=0, event_y=1, detail=key_code) window.send_event(ev) display.sync()
def right_action(): keycode = display.keysym_to_keycode( Xlib.XK.string_to_keysym('Right') ) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) display.sync()
def xlib_send_string(text, window=None): """ Send a string to a window by remapping the keyboard. Slow, but works with all Unicode characters. """ if type(text) is not str: text = text.decode("utf8") if not window: window = xlib_get_active_window() display = xlib_get_display() original_mapping = display.get_keyboard_mapping(254, 1) try: for character in text: display.change_keyboard_mapping( 254, [[xlib_unicode_cp_to_keysym(ord(character))] * len(original_mapping[0])]) display.sync() for m_type in (Xlib.protocol.event.KeyPress, Xlib.protocol.event.KeyRelease): ev = m_type(time=0, child=0, state=0, root=window.query_tree().root, window=window, same_screen=1, \ root_x=0, root_y=0, event_x=0, event_y=1, detail=254) window.send_event(ev) display.sync() time.sleep(0.05) pass finally: display.change_keyboard_mapping(254, original_mapping)
def pushkey(keycode,shift,ctrl,alt): # Press Shift Alt Ctrl key if ctrl : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, ctrlkey) if shift : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, shiftkey) if alt : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, altkey) # Release Shift Alt Ctrl key Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) # Release special key if alt : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, altkey) if shift : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, shiftkey) if ctrl : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, ctrlkey) # sync try: display.sync() except KeyboardInterrupt: pass
def sendkey(keystroke): ctrl = alt = shift = 0 key = "" splitted = keystroke.split(" ") for stroke in splitted: if stroke == "Ctrl": ctrl = 1 elif stroke == "Shift": shift = 1 elif stroke == "Alt": alt = 1 elif stroke == "Space": key = char_to_keycode(" ") else: # an ordinary key key = char_to_keycode(stroke) if ctrl==1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, ctrlkey) if alt==1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, altkey) if shift==1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, shiftkey) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, key) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, key) if ctrl==1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, ctrlkey) if alt==1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, altkey) if shift==1: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, shiftkey) display.sync()
def change_workspace(name): print(f"Changing to {name}", flush=True) display = Xlib.display.Display() screen = display.screen() root = screen.root w, h = screen.width_in_pixels, screen.height_in_pixels try: hue = int(name)/10+1/3 except: hue = mkrand(name).random() if name not in colors: colors[name] = gen_colors(hue) proc = subprocess.Popen(["xrdb", "-merge"], stdin=subprocess.PIPE) proc.communicate(input=colors[name].encode()) proc.wait() i3.command("reload") if (name, w, h) not in backgrounds: backgrounds[name, w, h] = root.create_pixmap(w, h, screen.root_depth) gen_bg(backgrounds[name, w, h], hue, name) id = backgrounds[name, w, h].id root.change_property(display.get_atom("_XROOTPMAP_ID"), Xatom.PIXMAP, 32, [id]) root.change_property(display.get_atom("ESETROOT_PMAP_ID"), Xatom.PIXMAP, 32, [id]) root.change_attributes(background_pixmap=id) root.clear_area(0, 0, w, h) display.sync()
def xlib_send_string(text, window=None): """ Send a string to a window by remapping the keyboard. Slow, but works with all Unicode characters. """ if type(text) is not unicode: text = text.decode("utf8") if not window: window = xlib_get_active_window() display = xlib_get_display() original_mapping = display.get_keyboard_mapping(254, 1) try: for character in text: display.change_keyboard_mapping(254, [ [xlib_unicode_cp_to_keysym(ord(character))] * len(original_mapping[0]) ]) display.sync() for m_type in (Xlib.protocol.event.KeyPress, Xlib.protocol.event.KeyRelease): ev = m_type(time=0, child=0, state=0, root=window.query_tree().root, window=window, same_screen=1, \ root_x=0, root_y=0, event_x=0, event_y=1, detail=254) window.send_event(ev) display.sync() time.sleep(0.05) pass finally: display.change_keyboard_mapping(254, original_mapping)
def display_manager(display): """Traps *X* errors and raises an :class:``X11Error`` at the end if any error occurred. This handler also ensures that the :class:`Xlib.display.Display` being managed is sync'd. :param Xlib.display.Display display: The *X* display. :return: the display :rtype: Xlib.display.Display """ errors = [] def handler(*args): """The *Xlib* error handler. """ errors.append(args) old_handler = display.set_error_handler(handler) try: yield display display.sync() finally: display.set_error_handler(old_handler) if errors: raise X11Error(errors)
def send_string(str) : for ch in str : #print "sending", ch, "=", display.keysym_to_keycode(Xlib.XK.string_to_keysym(ch)) keycode, shift_mask = char_to_keycode(ch) if (UseXTest) : #print "Trying fake_input of", ch, ", shift_mask is", shift_mask if shift_mask != 0 : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, 50) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) if shift_mask != 0 : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, 50) else : event = Xlib.protocol.event.KeyPress( time = int(time.time()), root = display.screen().root, window = window, same_screen = 0, child = Xlib.X.NONE, root_x = 0, root_y = 0, event_x = 0, event_y = 0, state = shift_mask, detail = keycode ) window.send_event(event, propagate = True) event = Xlib.protocol.event.KeyRelease( time = int(time.time()), root = display.screen().root, window = window, same_screen = 0, child = Xlib.X.NONE, root_x = 0, root_y = 0, event_x = 0, event_y = 0, state = shift_mask, detail = keycode ) window.send_event(event, propagate = True) display.sync()
def pushkey(keycode, shift, ctrl, alt): # Press Shift Alt Ctrl key if ctrl: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, ctrlkey) if shift: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, shiftkey) if alt: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, altkey) # Release Shift Alt Ctrl key Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) # Release special key if alt: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, altkey) if shift: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, shiftkey) if ctrl: Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, ctrlkey) # sync try: display.sync() except KeyboardInterrupt: pass
def display_manager(display): """Traps *X* errors and raises an :class:``X11Error`` at the end if any error occurred. This handler also ensures that the :class:`Xlib.display.Display` being managed is sync'd. :param Xlib.display.Display display: The *X* display. :return: the display :rtype: Xlib.display.Display """ errors = [] def handler(*args): errors.append(args) old_handler = display.set_error_handler(handler) try: yield display display.sync() finally: display.set_error_handler(old_handler) if errors: raise X11Error(errors)
def send_event(client_id, keystate, shiftstate, keycode): display = Xlib.display.Display() Xlib_root_window = display.screen().root window = display.create_resource_object('window', client_id) if keystate == 2: XEVENT = Xlib.protocol.event.KeyPress elif keystate == 3: XEVENT = Xlib.protocol.event.KeyRelease else: raise event = XEVENT(time=int(time.time()), root=Xlib_root_window, window=window, same_screen=0, child=Xlib.X.NONE, root_x=0, root_y=0, event_x=0, event_y=0, state=shiftstate, detail=keycode) display.sync() p = window.send_event(event, propagate=False) display.sync() display.close() return True
def update_strut(self): display = Xlib.display.Display() window = display.create_resource_object("window", self.get_window().get_xid()) window.change_property( display.intern_atom("_NET_WM_STRUT"), display.intern_atom("CARDINAL"), 32, [0, 0, 0, self.headerbar.get_allocated_height()]) display.sync()
def left_click(self): display = Xlib.display.Display() Xlib.ext.xtest.fake_input(display, Xlib.X.ButtonPress, 1) # 1 left, 2 middle, 3 right time.sleep(.05) Xlib.ext.xtest.fake_input(display, Xlib.X.ButtonRelease, 1) display.sync() time.sleep(.05)
def _press(keycode, down=None): if down == None: _press(keycode, True) _press(keycode, False) return xtest.fake_input(display, Xlib.X.KeyPress if down else Xlib.X.KeyRelease, keycode) display.sync()
def regrab_keys(): root = display.screen().root for key in bound.keys(): root.ungrab_key(key[0], key[1]) display.sync() bound.clear() grab_keys(all_keys)
def click(button, down=None): if down == None: click(button, True) click(button, False) return xtest.fake_input(display, Xlib.X.ButtonPress if down else Xlib.X.ButtonRelease, button) display.sync()
def onDeath(): say("oh no... Someone ate me!") window.configure(width=1500, height=900) display.sync() sleep(2) raw = window.get_image(0, 0, 1500, 900, Xlib.X.ZPixmap, 0xffffffff) image = Image.frombytes('RGB', (1500, 900), raw._data['data'], 'raw', 'BGRX') image.save('deaths/death-{}.png'.format( datetime.now())) # save screenshot of gameover screen
def mouse_click(window, mouse_button): old_focus = display.get_input_focus() #display.set_input_focus(window, Xlib.X.RevertToPointerRoot, Xlib.X.CurrentTime) #display.sync() fake_input(display, Xlib.X.ButtonPress, mouse_button) display.sync() fake_input(display, Xlib.X.ButtonRelease, mouse_button) display.sync()
def set_active_window_by_xwindow(window): ''' Function to set window as active based on xid ''' import Xlib from Xlib.display import Display from Xlib import X display = Display() window.set_input_focus(X.RevertToParent, X.CurrentTime) window.configure(stack_mode=X.Above) display.sync()
def send_string( str, display ): for ch in str : keycode, shift_mask = char_to_keycode(ch, display) if shift_mask != 0 : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, 50) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) if shift_mask != 0 : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, 50) display.sync()
def mouse_click(button): #button= 1 left, 2 middle, 3 right Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonPress, button) display.sync() Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonRelease, button) display.sync() # 移动 ds = display.Display() def goto_xy(x, y) : xtest.fake_input(ds, X.MotionNotify, x = x, y = y) #注意x,y参数的写法 ds.flush()
def send_key(display, window, keycodes): '''Send a KeyPress and KeyRelease event''' if not type(keycodes) in (tuple, list): keycodes = (keycodes, ) # send with modifier for keycode in keycodes: xtest.fake_input(window, X.KeyPress, display.keysym_to_keycode(keycode)) for keycode in reversed(keycodes): xtest.fake_input(window, X.KeyRelease, display.keysym_to_keycode(keycode)) display.sync()
def send_string(str) : for ch in str : #print "sending", ch, "=", display.keysym_to_keycode(Xlib.XK.string_to_keysym(ch)) keycode, shift_mask = char_to_keycode(ch) #print "Trying fake_input of", ch, ", shift_mask is", shift_mask if shift_mask != 0 : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, 50) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) if shift_mask != 0 : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, 50) display.sync()
def send_key(emulated_key): window = display.get_input_focus()._data["focus"]; # Generate the correct keycode keysym = Xlib.XK.string_to_keysym(emulated_key) keycode = display.keysym_to_keycode(keysym) # Send a fake keypress via xtest Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) display.sync() time.sleep(0.5) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) display.sync()
def resize_grace( ): windowIDs = root.get_full_property(display.intern_atom('_NET_CLIENT_LIST'), Xlib.X.AnyPropertyType).value for windowID in windowIDs: window = display.create_resource_object('window', windowID) name = window.get_wm_name() # Title pid = window.get_full_property(display.intern_atom('_NET_WM_PID'), Xlib.X.AnyPropertyType) # PID #print pid #print windowID #print name if "Grace" in name: #print name window.configure(width = WIDTH, height = HEIGHT) # for whatever reason, width and height are backwards but i don't care display.sync()
def remap_keys(remap): """ Remap keycode -> keysym for function keys The keycode -> keysym mapping is so that the keycode generated when the F1 key is pressed sends the F25 keysym. But to do that you need to know the keycodes for F1-10, for Linux on x86 the keycode for F1 is 67 but for other platforms it could be different. So the code looks up the keycode mapped to F1 keysym if that is non zero (i.e. F1 is mapped) I map that keycode to F1 or F25 as required, if F1 is not mapped I lookup the keycode for F25 and if that is non zero I map that keycode to F1 or F25 as required. If F1 and F25 are not mapped to any keycode I use 67 as the keycode for the F1 key. The code assumes that the keycodes for F1-10 are sequential which may not be true for all platforms. A more robust solution may be found by accessing the keyboard layout somehow but I haven't had time to look yet. """ display = Xlib.display.Display() # only keycodes 8 to 255 are defined first_keycode = 8 keymaps_orig = display.get_keyboard_mapping(first_keycode, 255 - first_keycode) keymaps = list(keymaps_orig) if remap == True: keysym = 65494 else: keysym = 65470 keycode_F1 = display.keysym_to_keycode(65470) keycode_F25 = display.keysym_to_keycode(65494) # set first_fkeycode to a sensible default incase F1 and F25 are not mapped first_fkeycode = 67 if keycode_F1 > 0: first_fkeycode = keycode_F1 elif keycode_F25 > 0: first_fkeycode = keycode_F25 for i in range(0, int(number_of_suggestions)): keymaps[(i + first_fkeycode - first_keycode)][0] = keysym + i keymaps[(i + first_fkeycode - first_keycode)][2] = keysym + i keymaps[(i + first_fkeycode - first_keycode)][4] = keysym + i display.change_keyboard_mapping(first_keycode, keymaps, onerror = None) display.sync() display.close()
def grab_keys(keys): for (code, mask), func in keys.items(): code = display.keysym_to_keycode(code) if (code, mask) == (0, 0): continue bound[code, mask] = func bound[code, mask | X.Mod2Mask] = func bound[code, mask | X.LockMask] = func bound[code, mask | X.Mod2Mask | X.LockMask] = func root = display.screen().root for key in bound.keys(): root.grab_key(key[0], key[1], 1, X.GrabModeAsync, X.GrabModeAsync) display.sync()
def remap_keys(remap): """ Remap keycode -> keysym for function keys The keycode -> keysym mapping is so that the keycode generated when the F1 key is pressed sends the F25 keysym. But to do that you need to know the keycodes for F1-10, for Linux on x86 the keycode for F1 is 67 but for other platforms it could be different. So the code looks up the keycode mapped to F1 keysym if that is non zero (i.e. F1 is mapped) I map that keycode to F1 or F25 as required, if F1 is not mapped I lookup the keycode for F25 and if that is non zero I map that keycode to F1 or F25 as required. If F1 and F25 are not mapped to any keycode I use 67 as the keycode for the F1 key. The code assumes that the keycodes for F1-10 are sequential which may not be true for all platforms. A more robust solution may be found by accessing the keyboard layout somehow but I haven't had time to look yet. """ display = Xlib.display.Display() # only keycodes 8 to 255 are defined first_keycode = 8 keymaps_orig = display.get_keyboard_mapping(first_keycode, 255 - first_keycode) keymaps = list(keymaps_orig) if remap == True: keysym = 65494 else: keysym = 65470 keycode_F1 = display.keysym_to_keycode(65470) keycode_F25 = display.keysym_to_keycode(65494) # set first_fkeycode to a sensible default incase F1 and F25 are not mapped first_fkeycode = 67 if keycode_F1 > 0: first_fkeycode = keycode_F1 elif keycode_F25 > 0: first_fkeycode = keycode_F25 for i in range(0, 10): keymaps[(i + first_fkeycode - first_keycode)][0] = keysym + i keymaps[(i + first_fkeycode - first_keycode)][2] = keysym + i keymaps[(i + first_fkeycode - first_keycode)][4] = keysym + i display.change_keyboard_mapping(first_keycode, keymaps, onerror = None) display.sync() display.close()
def send_key(display, window, keycodes): '''Send a KeyPress and KeyRelease event''' if not type(keycodes) in (tuple, list): keycodes = (keycodes,) # send with modifier for keycode in keycodes: xtest.fake_input(window, X.KeyPress, display.keysym_to_keycode(keycode)) for keycode in reversed(keycodes): xtest.fake_input(window, X.KeyRelease, display.keysym_to_keycode(keycode)) display.sync()
def xtype(keysyms, keycode=KEYCODE): keysyms = list(enumerate(keysyms, keycode)) print(keysyms) import Xlib.display from Xlib import X display = Xlib.display.Display() display.change_keyboard_mapping(keycode, [(sym,) * 8 for code, sym in keysyms]) while 1: e = display.next_event() if e.type == X.MappingNotify: for code, sym in keysyms: display.xtest_fake_input(X.KeyPress, code) display.xtest_fake_input(X.KeyRelease, code) break display.sync()
def release_key(keysym, shift_mask = 0) : keycode = display.keysym_to_keycode(keysym) if (UseXTest) : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) if shift_mask != 0 : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, 50) else : event = Xlib.protocol.event.KeyRelease( time = int(time.time()), root = display.screen().root, window = window, same_screen = 0, child = Xlib.X.NONE, root_x = 0, root_y = 0, event_x = 0, event_y = 0, state = shift_mask, detail = keycode ) window.send_event(event, propagate = True) display.sync()
def change_workspace(name): display = Xlib.display.Display() screen = display.screen() root = screen.root w, h = screen.width_in_pixels, screen.height_in_pixels if (name, w, h) not in background_cache: background_cache[name, w, h] = gen_bg( root.create_pixmap(w, h, screen.root_depth), name) id = background_cache[name, w, h].id root.change_property(display.get_atom("_XROOTPMAP_ID"), Xatom.PIXMAP, 32, [id]) root.change_property(display.get_atom("ESETROOT_PMAP_ID"), Xatom.PIXMAP, 32, [id]) root.change_attributes(background_pixmap=id) root.clear_area() display.sync()
def openWindow(): global window, WIDTH, HEIGHT, display, LEFT, TOP os.system('clear') # Open the browser os.system('firefox "https://agar.io" -new-window -private &') sleep(2) # Get display display = Xlib.display.Display() root = display.screen().root # Get the x windows ID of the browser windowID = root.get_full_property( display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0] window = display.create_resource_object('window', windowID) window.configure(width=WIDTH, height=HEIGHT) display.sync() LEFT, TOP = window.get_geometry().x, window.get_geometry().y
def process_request(self, absolute_path, get_params): global CURSOR_X, CURSOR_Y fin = open("home.html","r") html_page = fin.read(); operation = int(get_params['operation'][0]) display = Xlib.display.Display() screen = display.screen() SMALL_MOVE = 20 BIG_MOVE = 70 root = screen.root if operation == 1: CURSOR_X = CURSOR_X - SMALL_MOVE elif operation == 2: CURSOR_Y = CURSOR_Y + SMALL_MOVE elif operation == 3: CURSOR_X = CURSOR_X + SMALL_MOVE elif operation == 4: CURSOR_Y = CURSOR_Y - SMALL_MOVE elif operation == 7: CURSOR_X = CURSOR_X - BIG_MOVE elif operation == 8: CURSOR_Y = CURSOR_Y + BIG_MOVE elif operation == 9: CURSOR_X = CURSOR_X + BIG_MOVE elif operation == 10: CURSOR_Y = CURSOR_Y - BIG_MOVE root.warp_pointer(CURSOR_X,CURSOR_Y) if operation == 5: Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonPress,1) Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonRelease,1) elif operation == 6: Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonPress,3) Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonRelease,3) display.sync() print str(CURSOR_X)+ ","+str(CURSOR_Y) return html_page
def set_active_window_by_pointer(): ''' Function to set window as active based on where the mouse pointer is located ''' import Xlib from Xlib.display import Display from Xlib import X display = Display() root = display.screen().root window = root.query_pointer().child window.set_input_focus(X.RevertToParent, X.CurrentTime) window.configure(stack_mode=X.Above) display.sync() # get active window NET_ACTIVE_WINDOW = display.intern_atom('_NET_ACTIVE_WINDOW') try: window_id = root.get_full_property(NET_ACTIVE_WINDOW, Xlib.X.AnyPropertyType).value[0] except Xlib.error.XError: #simplify dealing with BadWindow window_id = None return window_id
def perform_key_event(accelerator, press, delay): import Xlib from Xlib import X from Xlib.display import Display from Xlib.ext.xtest import fake_input from Xlib.protocol.event import KeyPress, KeyRelease import time import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk, GdkX11 keysym, modifiers = Gtk.accelerator_parse(accelerator) display = Display() # root = display.screen().root # window = root.query_pointer().child # window.set_input_focus(X.RevertToParent, X.CurrentTime) # window.configure(stack_mode=X.Above) # display.sync() keycode = display.keysym_to_keycode(keysym) if press: event_type = X.KeyPress else: event_type = X.KeyRelease if keycode != 0: if 'GDK_CONTROL_MASK' in modifiers.value_names: modcode = display.keysym_to_keycode(Gdk.KEY_Control_L) fake_input(display, event_type, modcode, delay) if 'GDK_SHIFT_MASK' in modifiers.value_names: modcode = display.keysym_to_keycode(Gdk.KEY_Shift_L) fake_input(display, event_type, modcode, delay) fake_input(display, event_type, keycode, delay) display.sync()
def setAsBar(self): self.setWindowFlags(Qt.FramelessWindowHint) self.setWindowFlags(Qt.X11BypassWindowManagerHint) self.setGeometry(QRect(self._xpos, self._ypos, self._width, self._height)) x11window = display.create_resource_object('window', int(self.winId())) if self._ypos < 100: x11window.change_property(atom_strut_partial, atom_cardinal, 32, [0, 0, self._height, 0, 0, 0, 0, 0, self._xpos, self._xpos + self._width, 0, 0]) x11window.change_property(atom_strut, atom_cardinal, 32, [0, 0, self._height, 0]) else: # bottom x11window.change_property(atom_strut_partial, atom_cardinal, 32, [0, 0, 0, self._height, 0, 0, 0, 0, self._xpos, self._xpos + self._width, self._ypos, self._ypos + self._height]) x11window.change_property(atom_strut, atom_cardinal, 32, [ 0, 0, 0, self._height]) display.sync()
def send_string(window, text): #{{{ """ Send a string to a window """ display = Xlib.display.Display() Xlib.XK.load_keysym_group("xkb") keysym_map = dict((Xlib.XK.keysym_to_string(x), x) for x in list(latin1.__dict__.values()) if type(x) is int) for char_index in range(len(text)): char = text[char_index] if char in keysym_map: key_sym = keysym_map[char] else: if char == "\t": char = "Tab" elif char == "\n": char = "Return" key_sym = Xlib.XK.string_to_keysym(char) if not key_sym: continue key_codes = list(display.keysym_to_keycodes(key_sym)) if not key_codes: continue key_code, index = key_codes[0] m_state = 0 if index & 1: # Shift m_state = 1 if index & 4 | index & 2: # Alt Grid m_state = m_state | 0x80 for m_type in (Xlib.protocol.event.KeyPress, Xlib.protocol.event.KeyRelease): ev = m_type(time=0, child=0, state=m_state, root=window.query_tree().root, window=window, same_screen=1, \ root_x=0, root_y=0, event_x=0, event_y=1, detail=key_code) window.send_event(ev) display.sync()
def main(): offHeight = 0; offWidth = 0; offX = 0; offY = 0; dir = Direction.NONE try: opts, args = getopt.getopt(sys.argv[1:],"hx:y:d:a:b",["offHeight=","offWidth=", "offX=", "offY=","direction=", "help"]) except getopt.GetoptError as err: # print help information and exit: print str(err) usage() sys.exit(2) for opt, arg in opts: if opt in ('-h', "--help"): help() sys.exit() elif opt in ('-a', "--offHeight"): offHeight = int(arg) elif opt in ('-b', "--offWidth"): offWidth = int(arg) elif opt in ('-x', "--offX"): offX = int(arg) elif opt in ('-y', "--offY"): offY = int(arg) elif opt in ('-d', "--direction"): dir = int(arg) window = gtk.Window() # the screen contains all monitors screen = window.get_screen() # collect data about each monitor curMon = screen.get_monitor_at_window(screen.get_active_window()) currMonX, currMonY, currMonWidth, currMonHeight = screen.get_monitor_geometry(curMon) display = Xlib.display.Display() fcsWin = display.get_input_focus().focus fcsWinNude = None fcsWinNudeGeo = None fcsWinHolder = fcsWin # find the window with decoration and without while fcsWinNude is None: fcsWinHolderGeo = fcsWinHolder.get_geometry() if fcsWinHolderGeo.width <= 1 or fcsWinHolderGeo.height <= 1: fcsWinHolder = fcsWinHolder.query_tree().parent continue fcsWinNude = fcsWinHolder fcsWinNudeGeo = fcsWinHolderGeo fcsWinClthd = fcsWinNude.query_tree().parent fcsWinClthdGeo = fcsWinClthd.get_geometry() # print "screen.get_monitor_geometry(curMon): %s" % screen.get_monitor_geometry(curMon) # print "fcsWinGeo: %s" % fcsWin.get_geometry() # print "fcsWinNudeGeo: %s" % fcsWinNudeGeo # print "fcsWinClthdGeo: %s" % fcsWinClthdGeo clthHeight = fcsWinClthdGeo.height - fcsWinNudeGeo.height clthWidth = fcsWinClthdGeo.width - fcsWinNudeGeo.width # print "--------------------------------" # print "clthWidth: %s" % clthWidth # print "clthHeight: %s" % clthHeight if dir == Direction.NONE: print "no direction given" sys.exit(2) if dir > 8 or dir < 0: print "not acceptable direction given" sys.exit(2) # halfs if dir == Direction.UP: newX = currMonX newY = currMonY newWidth = currMonWidth + offWidth newHeight = currMonHeight/2 - clthHeight + offHeight/2 elif dir == Direction.LEFT: newX = currMonX newY = currMonY newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight - clthHeight + offHeight elif dir == Direction.RIGHT: newX = currMonX + currMonWidth/2 + offWidth/2 newY = currMonY newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight - clthHeight + offHeight elif dir == Direction.DOWN: newX = currMonX newY = currMonY +currMonHeight/2 +offHeight/2 newWidth = currMonWidth + offWidth newHeight = currMonHeight/2 - clthHeight + offHeight/2 # quarters elif dir == Direction.UP_RIGHT: newX = currMonX + currMonWidth/2 + offWidth/2 newY = currMonY newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight/2 - clthHeight + offHeight/2 elif dir == Direction.DOWN_RIGHT: newX = currMonX + currMonWidth/2 + offWidth/2 newY = currMonY +currMonHeight/2 +offHeight/2 newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight/2 - clthHeight + offHeight/2 elif dir == Direction.DOWN_LEFT: newX = currMonX newY = currMonY +currMonHeight/2 +offHeight/2 newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight/2 - clthHeight + offHeight/2 elif dir == Direction.UP_LEFT: newX = currMonX newY = currMonY newWidth = currMonWidth/2 + offWidth/2 newHeight = currMonHeight/2 - clthHeight + offHeight/2 # max size elif dir == Direction.ALL: newX = currMonX newY = currMonY newWidth = currMonWidth + offWidth newHeight = currMonHeight - clthHeight + offHeight # default else: # should not be possible sys.exit(2) newX += offX newY += offY # print "--------------------------------" # print "newWidth: %s" % newWidth # print "newHeight: %s" % newHeight # print "newX: %s" % newX # print "newY: %s" % newY fcsWinNude.configure(width = newWidth, height = newHeight, x = newX, y = newY) display.sync()
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, 50) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode) if shift_mask != 0 : Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, 50) else : event = Xlib.protocol.event.KeyPress( time = int(time.time()), root = display.screen().root, window = window, same_screen = 0, child = Xlib.X.NONE, root_x = 0, root_y = 0, event_x = 0, event_y = 0, state = shift_mask, detail = keycode ) window.send_event(event, propagate = True) event = Xlib.protocol.event.KeyRelease( time = int(time.time()), root = display.screen().root, window = window, same_screen = 0, child = Xlib.X.NONE, root_x = 0, root_y = 0, event_x = 0, event_y = 0, state = shift_mask, detail = keycode ) window.send_event(event, propagate = True) for argp in range(1, len(sys.argv)) : send_string(sys.argv[argp]) display.sync()
def press(button=1): Xlib.X.ButtonPress Xlib.ext.xtest.fake_input(display, Xlib.X.ButtonPress, button) display.sync()
def lock(display: display.Display): for screen in range(display.screen_count()): lock_screen(display, screen) display.sync()
def mouse_up(): display = Xlib.display.Display() Xlib.ext.xtest.fake_input(display, Xlib.X.ButtonRelease, 1) display.sync()
def mouse_down(): display = Xlib.display.Display() Xlib.ext.xtest.fake_input(display, Xlib.X.ButtonPress, 1) display.sync()
def send_string(s): """Generate fake keypresses to send the given string, using XTest if possible, otherwise Xlib. \C means press ctrl, \c means release it; same for s (shift), a (alt) and m or w (meta/windows). All modifiers will be released at the end of a string if the release code isn't already in the specifier. """ init_display() # Make lists of all the keycodes and whether they're shifted: keycodes = [] modifiers = [] shift_down = False ctrl_down = False alt_down = False meta_down = False backslash = False for ch in s: # print ("\n============ Considering '%s'" % ch) if ch == '\\': backslash = True continue if backslash: backslash = False # Most backslash escapes will be modifiers: if ch == 'C': ctrl_down = True send_modifier(CTRL_KEYCODE, True) continue elif ch == 'A': alt_down = True send_modifier(ALT_KEYCODE, True) continue elif ch == 'M' or ch == 'W': meta_down = True send_modifier(META_KEYCODE, True) continue elif ch == 'c': ctrl_down = False send_modifier(CTRL_KEYCODE, False) continue elif ch == 'a': alt_down = False send_modifier(ALT_KEYCODE, False) continue elif ch == 'm' or ch == 'w': meta_down = False send_modifier(META_KEYCODE, False) continue # but a few may be other characters: elif ch in special_X_keycodes: keycode = special_X_keycodes[ch] send_key(keycode, shift_down, ctrl_down, alt_down, meta_down) continue # Else just ignore the backslash and use the letter. ch_unshifted = is_shifted(ch) if ch_unshifted: if not shift_down: send_modifier(SHIFT_KEYCODE, True) shift_down = True ch = ch_unshifted else: if shift_down: send_modifier(SHIFT_KEYCODE, False) shift_down = False keycode = char_to_keycode(ch) send_key(keycode, shift_down, ctrl_down, alt_down, meta_down) # Is anything still pressed? if shift_down: send_modifier(SHIFT_KEYCODE, False) if ctrl_down: send_modifier(CTRL_KEYCODE, False) if alt_down: send_modifier(ALT_KEYCODE, False) if meta_down: send_modifier(META_KEYCODE, False) display.sync()
def mouse_click(button): #button= 1 left, 2 middle, 3 right Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonPress, button) display.sync() Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonRelease, button) display.sync()
def send_keycode( key, display ): Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, key) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, key) display.sync()
def set_size(display, window_id, width, height): window = display.create_resource_object('window', window_id) window.configure(width=width, height=height) display.sync()
def set_mouse(x, y): root.warp_pointer(x, y) display.sync()
def move_mouse(self, posxy): display = Xlib.display.Display() root = display.screen().root root.warp_pointer(posxy[0] + Glob.window_topleft[0], posxy[1] + Glob.window_topleft[1]) display.sync()
def press_button(button): Xlib.ext.xtest.fake_input(display, Xlib.X.ButtonPress, button) display.sync() Xlib.ext.xtest.fake_input(display, Xlib.X.ButtonRelease, button) display.sync()
def resize_this(): windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0] window = display.create_resource_object('window', windowID) window.configure(width = WIDTH, height = HEIGHT) display.sync()
else: event = Xlib.protocol.event.KeyPress(time=int(time.time()), root=display.screen().root, window=window, same_screen=0, child=Xlib.X.NONE, root_x=0, root_y=0, event_x=0, event_y=0, state=shift_mask, detail=keycode) window.send_event(event, propagate=True) event = Xlib.protocol.event.KeyRelease(time=int(time.time()), root=display.screen().root, window=window, same_screen=0, child=Xlib.X.NONE, root_x=0, root_y=0, event_x=0, event_y=0, state=shift_mask, detail=keycode) window.send_event(event, propagate=True) for argp in range(1, len(sys.argv)): send_string(sys.argv[argp]) display.sync()
def send_keysym( keysym, display ): key = display.keysym_to_keycode( keysym ) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, key) Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, key) display.sync()