_pyautogui_x11.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # NOTE - It is a known issue that the keyboard-related functions don't work on Ubuntu VMs in Virtualbox.
  2. import pyautogui
  3. import sys
  4. import os
  5. import subprocess
  6. from pyautogui import LEFT, MIDDLE, RIGHT
  7. from Xlib.display import Display
  8. from Xlib import X
  9. from Xlib.ext.xtest import fake_input
  10. import Xlib.XK
  11. BUTTON_NAME_MAPPING = {LEFT: 1, MIDDLE: 2, RIGHT: 3, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}
  12. if sys.platform in ('java', 'darwin', 'win32'):
  13. raise Exception('The pyautogui_x11 module should only be loaded on a Unix system that supports X11.')
  14. #from pyautogui import *
  15. """
  16. Much of this code is based on information gleaned from Paul Barton's PyKeyboard in PyUserInput from 2013, itself derived from Akkana Peck's pykey in 2008 ( http://www.shallowsky.com/software/crikey/pykey-0.1 ), itself derived from her "Crikey" lib.
  17. """
  18. def _position():
  19. """Returns the current xy coordinates of the mouse cursor as a two-integer
  20. tuple.
  21. Returns:
  22. (x, y) tuple of the current xy coordinates of the mouse cursor.
  23. """
  24. coord = _display.screen().root.query_pointer()._data
  25. return coord["root_x"], coord["root_y"]
  26. def _size():
  27. return _display.screen().width_in_pixels, _display.screen().height_in_pixels
  28. def _vscroll(clicks, x=None, y=None):
  29. clicks = int(clicks)
  30. if clicks == 0:
  31. return
  32. elif clicks > 0:
  33. button = 4 # scroll up
  34. else:
  35. button = 5 # scroll down
  36. for i in range(abs(clicks)):
  37. _click(x, y, button=button)
  38. def _hscroll(clicks, x=None, y=None):
  39. clicks = int(clicks)
  40. if clicks == 0:
  41. return
  42. elif clicks > 0:
  43. button = 7 # scroll right
  44. else:
  45. button = 6 # scroll left
  46. for i in range(abs(clicks)):
  47. _click(x, y, button=button)
  48. def _scroll(clicks, x=None, y=None):
  49. return _vscroll(clicks, x, y)
  50. def _click(x, y, button):
  51. assert button in BUTTON_NAME_MAPPING.keys(), "button argument not in ('left', 'middle', 'right', 4, 5, 6, 7)"
  52. button = BUTTON_NAME_MAPPING[button]
  53. _mouseDown(x, y, button)
  54. _mouseUp(x, y, button)
  55. _mouse_is_swapped_setting = None
  56. def _mouse_is_swapped():
  57. # TODO - for performance reasons, we only check the swapped mouse button
  58. # setting from the OS once at start up, rather than every mouse click.
  59. # Testing shows this takes about 0.02 seconds on my machine in a vm.
  60. # This may change in the future.
  61. global _mouse_is_swapped_setting
  62. if _mouse_is_swapped_setting is None:
  63. try:
  64. proc = subprocess.Popen(['dconf', 'read', '/org/gnome/desktop/peripherals/mouse/left-handed'], stdout=subprocess.PIPE)
  65. stdout_bytes, stderr_bytes = proc.communicate()
  66. _mouse_is_swapped_setting = stdout_bytes.decode('utf-8') == 'true\n'
  67. except FileNotFoundError:
  68. # The user is not running Gnome (the default window manager on Ubuntu) so just assume it's not swapped.
  69. _mouse_is_swapped_setting = False
  70. return _mouse_is_swapped_setting
  71. def _moveTo(x, y):
  72. fake_input(_display, X.MotionNotify, x=x, y=y)
  73. _display.sync()
  74. def _mouseDown(x, y, button):
  75. _moveTo(x, y)
  76. assert button in BUTTON_NAME_MAPPING.keys(), "button argument not in ('left', 'middle', 'right', 4, 5, 6, 7)"
  77. button = BUTTON_NAME_MAPPING[button]
  78. fake_input(_display, X.ButtonPress, button)
  79. _display.sync()
  80. def _mouseUp(x, y, button):
  81. _moveTo(x, y)
  82. assert button in BUTTON_NAME_MAPPING.keys(), "button argument not in ('left', 'middle', 'right', 4, 5, 6, 7)"
  83. button = BUTTON_NAME_MAPPING[button]
  84. fake_input(_display, X.ButtonRelease, button)
  85. _display.sync()
  86. def _keyDown(key):
  87. """Performs a keyboard key press without the release. This will put that
  88. key in a held down state.
  89. NOTE: For some reason, this does not seem to cause key repeats like would
  90. happen if a keyboard key was held down on a text field.
  91. Args:
  92. key (str): The key to be pressed down. The valid names are listed in
  93. pyautogui.KEY_NAMES.
  94. Returns:
  95. None
  96. """
  97. if key not in keyboardMapping or keyboardMapping[key] is None:
  98. return
  99. if type(key) == int:
  100. fake_input(_display, X.KeyPress, key)
  101. _display.sync()
  102. return
  103. needsShift = pyautogui.isShiftCharacter(key)
  104. if needsShift:
  105. fake_input(_display, X.KeyPress, keyboardMapping['shift'])
  106. fake_input(_display, X.KeyPress, keyboardMapping[key])
  107. if needsShift:
  108. fake_input(_display, X.KeyRelease, keyboardMapping['shift'])
  109. _display.sync()
  110. def _keyUp(key):
  111. """Performs a keyboard key release (without the press down beforehand).
  112. Args:
  113. key (str): The key to be released up. The valid names are listed in
  114. pyautogui.KEY_NAMES.
  115. Returns:
  116. None
  117. """
  118. """
  119. Release a given character key. Also works with character keycodes as
  120. integers, but not keysyms.
  121. """
  122. if key not in keyboardMapping or keyboardMapping[key] is None:
  123. return
  124. if type(key) == int:
  125. keycode = key
  126. else:
  127. keycode = keyboardMapping[key]
  128. fake_input(_display, X.KeyRelease, keycode)
  129. _display.sync()
  130. # Taken from PyKeyboard's ctor function.
  131. _display = Display(os.environ['DISPLAY'])
  132. """ Information for keyboardMapping derived from PyKeyboard's special_key_assignment() function.
  133. The *KB dictionaries in pyautogui map a string that can be passed to keyDown(),
  134. keyUp(), or press() into the code used for the OS-specific keyboard function.
  135. They should always be lowercase, and the same keys should be used across all OSes."""
  136. keyboardMapping = dict([(key, None) for key in pyautogui.KEY_NAMES])
  137. keyboardMapping.update({
  138. 'backspace': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('BackSpace')),
  139. '\b': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('BackSpace')),
  140. 'tab': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Tab')),
  141. 'enter': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Return')),
  142. 'return': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Return')),
  143. 'shift': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Shift_L')),
  144. 'ctrl': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Control_L')),
  145. 'alt': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Alt_L')),
  146. 'pause': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Pause')),
  147. 'capslock': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Caps_Lock')),
  148. 'esc': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Escape')),
  149. 'escape': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Escape')),
  150. 'pgup': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Page_Up')),
  151. 'pgdn': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Page_Down')),
  152. 'pageup': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Page_Up')),
  153. 'pagedown': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Page_Down')),
  154. 'end': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('End')),
  155. 'home': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Home')),
  156. 'left': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Left')),
  157. 'up': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Up')),
  158. 'right': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Right')),
  159. 'down': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Down')),
  160. 'select': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Select')),
  161. 'print': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Print')),
  162. 'execute': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Execute')),
  163. 'prtsc': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Print')),
  164. 'prtscr': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Print')),
  165. 'prntscrn': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Print')),
  166. 'printscreen': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Print')),
  167. 'insert': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Insert')),
  168. 'del': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Delete')),
  169. 'delete': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Delete')),
  170. 'help': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Help')),
  171. 'win': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Super_L')),
  172. 'winleft': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Super_L')),
  173. 'winright': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Super_R')),
  174. 'apps': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Menu')),
  175. 'num0': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_0')),
  176. 'num1': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_1')),
  177. 'num2': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_2')),
  178. 'num3': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_3')),
  179. 'num4': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_4')),
  180. 'num5': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_5')),
  181. 'num6': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_6')),
  182. 'num7': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_7')),
  183. 'num8': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_8')),
  184. 'num9': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_9')),
  185. 'multiply': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_Multiply')),
  186. 'add': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_Add')),
  187. 'separator': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_Separator')),
  188. 'subtract': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_Subtract')),
  189. 'decimal': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_Decimal')),
  190. 'divide': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('KP_Divide')),
  191. 'f1': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F1')),
  192. 'f2': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F2')),
  193. 'f3': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F3')),
  194. 'f4': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F4')),
  195. 'f5': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F5')),
  196. 'f6': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F6')),
  197. 'f7': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F7')),
  198. 'f8': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F8')),
  199. 'f9': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F9')),
  200. 'f10': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F10')),
  201. 'f11': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F11')),
  202. 'f12': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F12')),
  203. 'f13': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F13')),
  204. 'f14': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F14')),
  205. 'f15': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F15')),
  206. 'f16': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F16')),
  207. 'f17': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F17')),
  208. 'f18': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F18')),
  209. 'f19': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F19')),
  210. 'f20': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F20')),
  211. 'f21': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F21')),
  212. 'f22': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F22')),
  213. 'f23': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F23')),
  214. 'f24': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('F24')),
  215. 'numlock': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Num_Lock')),
  216. 'scrolllock': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Scroll_Lock')),
  217. 'shiftleft': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Shift_L')),
  218. 'shiftright': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Shift_R')),
  219. 'ctrlleft': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Control_L')),
  220. 'ctrlright': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Control_R')),
  221. 'altleft': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Alt_L')),
  222. 'altright': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Alt_R')),
  223. # These are added because unlike a-zA-Z0-9, the single characters do not have a
  224. ' ': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('space')),
  225. 'space': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('space')),
  226. '\t': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Tab')),
  227. '\n': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Return')), # for some reason this needs to be cr, not lf
  228. '\r': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Return')),
  229. '\e': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('Escape')),
  230. '!': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('exclam')),
  231. '#': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('numbersign')),
  232. '%': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('percent')),
  233. '$': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('dollar')),
  234. '&': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('ampersand')),
  235. '"': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('quotedbl')),
  236. "'": _display.keysym_to_keycode(Xlib.XK.string_to_keysym('apostrophe')),
  237. '(': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('parenleft')),
  238. ')': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('parenright')),
  239. '*': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('asterisk')),
  240. '=': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('equal')),
  241. '+': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('plus')),
  242. ',': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('comma')),
  243. '-': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('minus')),
  244. '.': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('period')),
  245. '/': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('slash')),
  246. ':': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('colon')),
  247. ';': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('semicolon')),
  248. '<': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('less')),
  249. '>': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('greater')),
  250. '?': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('question')),
  251. '@': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('at')),
  252. '[': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('bracketleft')),
  253. ']': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('bracketright')),
  254. '\\': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('backslash')),
  255. '^': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('asciicircum')),
  256. '_': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('underscore')),
  257. '`': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('grave')),
  258. '{': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('braceleft')),
  259. '|': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('bar')),
  260. '}': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('braceright')),
  261. '~': _display.keysym_to_keycode(Xlib.XK.string_to_keysym('asciitilde')),
  262. })
  263. # Trading memory for time" populate winKB so we don't have to call VkKeyScanA each time.
  264. for c in """abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890""":
  265. keyboardMapping[c] = _display.keysym_to_keycode(Xlib.XK.string_to_keysym(c))