Advertisement
Ritam_C

pytautogui_changed

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