pywinauto.hooks

Windows global hooks in pure Python

The implementation uses foreign function interface (FFI) provided by standard Python module ctypes and inspired by pyHook, pyhooked and other similar modules (the code was re-written from scratch). It tends to be a superset of pyHook but in pure Python only so it doesn’t require compilation.

Current set of hooks implemented:
  • WH_MOUSE_LL
  • WH_KEYBOARD_LL

More detailed documentation about Windows hooks can be found in MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms632589.aspx

This module can be used as a stand alone or along with pywinauto. The fork of this code (at some moment) was used in standalone library pyhooked 0.8 maintained by Ethan Smith.

class pywinauto.hooks.Hook

Hook for low level keyboard and mouse events

ID_TO_KEY = {8: 'Back', 9: 'Tab', 13: 'Return', 20: 'Capital', 27: 'Escape', 32: 'Space', 33: 'Prior', 34: 'Next', 35: 'End', 36: 'Home', 37: 'Left', 38: 'Up', 39: 'Right', 40: 'Down', 44: 'Snapshot', 46: 'Delete', 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5', 54: '6', 55: '7', 56: '8', 57: '9', 65: 'A', 66: 'B', 67: 'C', 68: 'D', 69: 'E', 70: 'F', 71: 'G', 72: 'H', 73: 'I', 74: 'J', 75: 'K', 76: 'L', 77: 'M', 78: 'N', 79: 'O', 80: 'P', 81: 'Q', 82: 'R', 83: 'S', 84: 'T', 85: 'U', 86: 'V', 87: 'W', 88: 'X', 89: 'Y', 90: 'Z', 91: 'Lwin', 92: 'Rwin', 93: 'App', 95: 'Sleep', 96: 'Numpad0', 97: 'Numpad1', 98: 'Numpad2', 99: 'Numpad3', 100: 'Numpad4', 101: 'Numpad5', 102: 'Numpad6', 103: 'Numpad7', 104: 'Numpad8', 105: 'Numpad9', 106: 'Multiply', 107: 'Add', 109: 'Subtract', 110: 'Decimal', 111: 'Divide', 112: 'F1', 113: 'F2', 114: 'F3', 115: 'F4', 116: 'F5', 117: 'F6', 118: 'F7', 119: 'F8', 120: 'F9', 121: 'F10', 122: 'F11', 123: 'F12', 144: 'Numlock', 160: 'Lshift', 161: 'Rshift', 162: 'Lcontrol', 163: 'Rcontrol', 164: 'Lmenu', 165: 'Rmenu', 186: 'Oem_1', 187: 'Oem_Plus', 188: 'Oem_Comma', 189: 'Oem_Minus', 190: 'Oem_Period', 191: 'Oem_2', 192: 'Oem_3', 219: 'Oem_4', 220: 'Oem_5', 221: 'Oem_6', 222: 'Oem_7', 1000: 'mouse move', 1001: 'mouse left', 1002: 'mouse right', 1003: 'mouse middle', 1004: 'mouse wheel up', 1005: 'mouse wheel down', 1010: 'Ctrl', 1011: 'Alt', 1012: 'Shift', 1013: 'Win'}
MOUSE_ID_TO_EVENT_TYPE = {512: None, 513: 'key down', 514: 'key up', 516: 'key down', 517: 'key up', 519: 'key down', 520: 'key up', 522: None}
MOUSE_ID_TO_KEY = {512: 'Move', 513: 'LButton', 514: 'LButton', 516: 'RButton', 517: 'RButton', 519: 'WheelButton', 520: 'WheelButton', 522: 'Wheel'}
WH_KEYBOARD_LL = 13
WH_MOUSE_LL = 14
WM_QUIT = 18
event_types = {256: 'key down', 257: 'key up', 260: 'key down', 261: 'key up'}
hook(keyboard=True, mouse=False)

Hook mouse and/or keyboard events

listen()

Listen events

unhook_keyboard()

Unhook keyboard events

unhook_mouse()

Unhook mouse events

class pywinauto.hooks.KeyboardEvent(current_key=None, event_type=None, pressed_key=None)

Created when a keyboard event happened

class pywinauto.hooks.MouseEvent(current_key=None, event_type=None, mouse_x=0, mouse_y=0)

Created when a mouse event happened