Last active
April 5, 2018 14:38
-
-
Save Avlyssna/f1ce014f3a6081d543a71c08729972da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Standard library imports | |
import ctypes | |
# Keyboard scan codes | |
SCANCODE_ESCAPE = 0x01 | |
SCANCODE_1 = 0x02 | |
SCANCODE_2 = 0x03 | |
SCANCODE_3 = 0x04 | |
SCANCODE_4 = 0x05 | |
SCANCODE_5 = 0x06 | |
SCANCODE_6 = 0x07 | |
SCANCODE_7 = 0x08 | |
SCANCODE_8 = 0x09 | |
SCANCODE_9 = 0x0A | |
SCANCODE_0 = 0x0B | |
SCANCODE_MINUS = 0x0C | |
SCANCODE_EQUALS = 0x0D | |
SCANCODE_BACK = 0x0E | |
SCANCODE_TAB = 0x0F | |
SCANCODE_Q = 0x10 | |
SCANCODE_W = 0x11 | |
SCANCODE_E = 0x12 | |
SCANCODE_R = 0x13 | |
SCANCODE_T = 0x14 | |
SCANCODE_Y = 0x15 | |
SCANCODE_U = 0x16 | |
SCANCODE_I = 0x17 | |
SCANCODE_O = 0x18 | |
SCANCODE_P = 0x19 | |
SCANCODE_LBRACKET = 0x1A | |
SCANCODE_RBRACKET = 0x1B | |
SCANCODE_RETURN = 0x1C | |
SCANCODE_LCONTROL = 0x1D | |
SCANCODE_A = 0x1E | |
SCANCODE_S = 0x1F | |
SCANCODE_D = 0x20 | |
SCANCODE_F = 0x21 | |
SCANCODE_G = 0x22 | |
SCANCODE_H = 0x23 | |
SCANCODE_J = 0x24 | |
SCANCODE_K = 0x25 | |
SCANCODE_L = 0x26 | |
SCANCODE_SEMICOLON = 0x27 | |
SCANCODE_APOSTROPHE = 0x28 | |
SCANCODE_GRAVE = 0x29 | |
SCANCODE_LSHIFT = 0x2A | |
SCANCODE_BACKSLASH = 0x2B | |
SCANCODE_Z = 0x2C | |
SCANCODE_X = 0x2D | |
SCANCODE_C = 0x2E | |
SCANCODE_V = 0x2F | |
SCANCODE_B = 0x30 | |
SCANCODE_N = 0x31 | |
SCANCODE_M = 0x32 | |
SCANCODE_COMMA = 0x33 | |
SCANCODE_PERIOD = 0x34 | |
SCANCODE_SLASH = 0x35 | |
SCANCODE_RSHIFT = 0x36 | |
SCANCODE_MULTIPLY = 0x37 | |
SCANCODE_LMENU = 0x38 | |
SCANCODE_SPACE = 0x39 | |
SCANCODE_CAPITAL = 0x3A | |
SCANCODE_F1 = 0x3B | |
SCANCODE_F2 = 0x3C | |
SCANCODE_F3 = 0x3D | |
SCANCODE_F4 = 0x3E | |
SCANCODE_F5 = 0x3F | |
SCANCODE_F6 = 0x40 | |
SCANCODE_F7 = 0x41 | |
SCANCODE_F8 = 0x42 | |
SCANCODE_F9 = 0x43 | |
SCANCODE_F10 = 0x44 | |
SCANCODE_NUMLOCK = 0x45 | |
SCANCODE_SCROLL = 0x46 | |
SCANCODE_NUMPAD7 = 0x47 | |
SCANCODE_NUMPAD8 = 0x48 | |
SCANCODE_NUMPAD9 = 0x49 | |
SCANCODE_SUBTRACT = 0x4A | |
SCANCODE_NUMPAD4 = 0x4B | |
SCANCODE_NUMPAD5 = 0x4C | |
SCANCODE_NUMPAD6 = 0x4D | |
SCANCODE_ADD = 0x4E | |
SCANCODE_NUMPAD1 = 0x4F | |
SCANCODE_NUMPAD2 = 0x50 | |
SCANCODE_NUMPAD3 = 0x51 | |
SCANCODE_NUMPAD0 = 0x52 | |
SCANCODE_DECIMAL = 0x53 | |
SCANCODE_F11 = 0x57 | |
SCANCODE_F12 = 0x58 | |
SCANCODE_F13 = 0x64 | |
SCANCODE_F14 = 0x65 | |
SCANCODE_F15 = 0x66 | |
SCANCODE_KANA = 0x70 | |
SCANCODE_CONVERT = 0x79 | |
SCANCODE_NOCONVERT = 0x7B | |
SCANCODE_YEN = 0x7D | |
SCANCODE_NUMPADEQUALS = 0x8D | |
SCANCODE_CIRCUMFLEX = 0x90 | |
SCANCODE_AT = 0x91 | |
SCANCODE_COLON = 0x92 | |
SCANCODE_UNDERLINE = 0x93 | |
SCANCODE_KANJI = 0x94 | |
SCANCODE_STOP = 0x95 | |
SCANCODE_AX = 0x96 | |
SCANCODE_UNLABELED = 0x97 | |
SCANCODE_NUMPADENTER = 0x9C | |
SCANCODE_RCONTROL = 0x9D | |
SCANCODE_NUMPADCOMMA = 0xB3 | |
SCANCODE_DIVIDE = 0xB5 | |
SCANCODE_SYSRQ = 0xB7 | |
SCANCODE_RMENU = 0xB8 | |
SCANCODE_HOME = 0xC7 | |
SCANCODE_UP = 0xC8 | |
SCANCODE_PRIOR = 0xC9 | |
SCANCODE_LEFT = 0xCB | |
SCANCODE_RIGHT = 0xCD | |
SCANCODE_END = 0xCF | |
SCANCODE_DOWN = 0xD0 | |
SCANCODE_NEXT = 0xD1 | |
SCANCODE_INSERT = 0xD2 | |
SCANCODE_DELETE = 0xD3 | |
SCANCODE_LWIN = 0xDB | |
SCANCODE_RWIN = 0xDC | |
SCANCODE_APPS = 0xDD | |
SCANCODE_BACKSPACE = SCANCODE_BACK | |
SCANCODE_NUMPADSTAR = SCANCODE_MULTIPLY | |
SCANCODE_LALT = SCANCODE_LMENU | |
SCANCODE_CAPSLOCK = SCANCODE_CAPITAL | |
SCANCODE_NUMPADMINUS = SCANCODE_SUBTRACT | |
SCANCODE_NUMPADPLUS = SCANCODE_ADD | |
SCANCODE_NUMPADPERIOD = SCANCODE_DECIMAL | |
SCANCODE_NUMPADSLASH = SCANCODE_DIVIDE | |
SCANCODE_RALT = SCANCODE_RMENU | |
SCANCODE_UPARROW = SCANCODE_UP | |
SCANCODE_PGUP = SCANCODE_PRIOR | |
SCANCODE_LEFTARROW = SCANCODE_LEFT | |
SCANCODE_RIGHTARROW = SCANCODE_RIGHT | |
SCANCODE_DOWNARROW = SCANCODE_DOWN | |
SCANCODE_PGDN = SCANCODE_NEXT | |
class MouseInput(ctypes.Structure): | |
_fields_ = [ | |
('dx', ctypes.c_long), | |
('dy', ctypes.c_long), | |
('mouseData', ctypes.c_ulong), | |
('dwFlags', ctypes.c_ulong), | |
('time', ctypes.c_ulong), | |
('dwExtraInfo', ctypes.POINTER(ctypes.c_ulong)) | |
] | |
class KeyboardInput(ctypes.Structure): | |
_fields_ = [ | |
('wVk', ctypes.c_ushort), | |
('wScan', ctypes.c_ushort), | |
('dwFlags', ctypes.c_ulong), | |
('time', ctypes.c_ulong), | |
('dwExtraInfo', ctypes.POINTER(ctypes.c_ulong)) | |
] | |
class HardwareInput(ctypes.Structure): | |
_fields_ = [ | |
('uMsg', ctypes.c_ulong), | |
('wParamL', ctypes.c_short), | |
('wParamH', ctypes.c_ushort) | |
] | |
class InputUnion(ctypes.Union): | |
_fields_ = [ | |
('ki', KeyboardInput), | |
('mi', MouseInput), | |
('hi', HardwareInput) | |
] | |
class Input(ctypes.Structure): | |
_fields_ = [ | |
('type', ctypes.c_ulong), | |
('iu', InputUnion) | |
] | |
def press_key(key_scancode): | |
input_union = InputUnion() | |
input_union.ki = KeyboardInput(0, key_scancode, 0x8, 0, ctypes.pointer(ctypes.c_ulong())) | |
input_structure = Input(1, input_union) | |
ctypes.windll.user32.SendInput(1, ctypes.pointer(input_structure), ctypes.sizeof(input_structure)) | |
def release_key(key_scancode): | |
input_union = InputUnion() | |
input_union.ki = KeyboardInput(0, key_scancode, 0x8 | 0x2, 0, ctypes.pointer(ctypes.c_ulong())) | |
input_structure = Input(1, input_union) | |
ctypes.windll.user32.SendInput(1, ctypes.pointer(input_structure), ctypes.sizeof(input_structure)) | |
def mouse_down(): | |
input_union = InputUnion() | |
input_union.mi = MouseInput(0, 0, 0, 0x2, 0, ctypes.pointer(ctypes.c_ulong())) | |
input_structure = Input(0, input_union) | |
ctypes.windll.user32.SendInput(1, ctypes.pointer(input_structure), ctypes.sizeof(input_structure)) | |
def mouse_up(): | |
input_union = InputUnion() | |
input_union.mi = MouseInput(0, 0, 0, 0x4, 0, ctypes.pointer(ctypes.c_ulong())) | |
input_structure = Input(0, input_union) | |
ctypes.windll.user32.SendInput(1, ctypes.pointer(input_structure), ctypes.sizeof(input_structure)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment