Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ctypes
- import time
- from Tkinter import Tk
- import subprocess
- UPPER = frozenset('~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?')
- LOWER = frozenset("`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./")
- ALTER = dict(zip('!@#$%^&*()', '1234567890'))
- Key = {
- 'MouseL' : 0x01 ,
- 'MouseR' : 0x02 ,
- 'Cancel' : 0x03 ,
- 'MouseM' : 0x04 ,
- 'MouseX1' : 0x05 ,
- 'MouseX2' : 0x06 ,
- 'Backspace' : 0x08 ,
- 'Tab' : 0x09 ,
- 'Clear' : 0x0C ,
- 'Enter' : 0x0D ,
- 'Shift' : 0x10 ,
- 'Ctrl' : 0x11 ,
- 'Alt' : 0x12 ,
- 'Pause' : 0x13 ,
- 'Caps' : 0x14 ,
- 'Esc' : 0x1B ,
- 'Accept' : 0x1E ,
- 'Space' : 0x20 ,
- ' ' : 0x20 ,
- 'PageUp' : 0x21 ,
- 'PageDown' : 0x22 ,
- 'End' : 0x23 ,
- 'Home' : 0x24 ,
- 'Left' : 0x25 ,
- 'Up' : 0x26 ,
- 'Right' : 0x27 ,
- 'Down' : 0x28 ,
- 'Select' : 0x29 ,
- 'Print' : 0x2A ,
- 'Execute' : 0x2B ,
- 'PrintScreen' : 0x2C ,
- 'Ins' : 0x2D ,
- 'Del' : 0x2E ,
- 'Help' : 0x2F ,
- ',' : 0x2C ,
- '0' : 0x30 ,
- '1' : 0x31 ,
- '2' : 0x32 ,
- '3' : 0x33 ,
- '4' : 0x34 ,
- '5' : 0x35 ,
- '6' : 0x36 ,
- '7' : 0x37 ,
- '8' : 0x38 ,
- '9' : 0x39 ,
- 'A' : 0x41 ,
- 'B' : 0x42 ,
- 'C' : 0x43 ,
- 'D' : 0x44 ,
- 'E' : 0x45 ,
- 'F' : 0x46 ,
- 'G' : 0x47 ,
- 'H' : 0x48 ,
- 'I' : 0x49 ,
- 'J' : 0x4A ,
- 'K' : 0x4B ,
- 'L' : 0x4C ,
- 'M' : 0x4D ,
- 'N' : 0x4E ,
- 'O' : 0x4F ,
- 'P' : 0x50 ,
- 'Q' : 0x51 ,
- 'R' : 0x52 ,
- 'S' : 0x53 ,
- 'T' : 0x54 ,
- 'U' : 0x55 ,
- 'V' : 0x56 ,
- 'W' : 0x57 ,
- 'X' : 0x58 ,
- 'Y' : 0x59 ,
- 'Z' : 0x5A ,
- 'a' : 0x41 ,
- 'b' : 0x42 ,
- 'c' : 0x43 ,
- 'd' : 0x44 ,
- 'e' : 0x45 ,
- 'f' : 0x46 ,
- 'g' : 0x47 ,
- 'h' : 0x48 ,
- 'i' : 0x49 ,
- 'j' : 0x4A ,
- 'k' : 0x4B ,
- 'l' : 0x4C ,
- 'm' : 0x4D ,
- 'n' : 0x4E ,
- 'o' : 0x4F ,
- 'p' : 0x50 ,
- 'q' : 0x51 ,
- 'r' : 0x52 ,
- 's' : 0x53 ,
- 't' : 0x54 ,
- 'u' : 0x55 ,
- 'v' : 0x56 ,
- 'w' : 0x57 ,
- 'x' : 0x58 ,
- 'y' : 0x59 ,
- 'z' : 0x5A ,
- 'LeftWin' : 0x5B ,
- 'RightWin' : 0x5C ,
- 'Applications' : 0x5D ,
- 'Reserved' : 0x5E ,
- 'Sleep' : 0x5F ,
- 'Num0' : 0x60 ,
- 'Num1' : 0x61 ,
- 'Num2' : 0x62 ,
- 'Num3' : 0x63 ,
- 'Num4' : 0x64 ,
- 'Num5' : 0x65 ,
- 'Num6' : 0x66 ,
- 'Num7' : 0x67 ,
- 'Num8' : 0x68 ,
- 'Num9' : 0x69 ,
- 'Multiply' : 0x6A ,
- 'Add' : 0x6B ,
- 'Separator' : 0x6C ,
- 'Subtract' : 0x6D ,
- 'Decimal' : 0x6E ,
- 'Divide' : 0x6F ,
- 'F1' : 0x70 ,
- 'F2' : 0x71 ,
- 'F3' : 0x72 ,
- 'F4' : 0x73 ,
- 'F5' : 0x74 ,
- 'F6' : 0x75 ,
- 'F7' : 0x76 ,
- 'F8' : 0x77 ,
- 'F9' : 0x78 ,
- 'F10' : 0x79 ,
- 'F11' : 0x7A ,
- 'F12' : 0x7B ,
- 'F13' : 0x7C ,
- 'F14' : 0x7D ,
- 'F15' : 0x7E ,
- 'F16' : 0x7F ,
- 'F17' : 0x80 ,
- 'F18' : 0x81 ,
- 'F19' : 0x82 ,
- 'F20' : 0x83 ,
- 'F21' : 0x84 ,
- 'F22' : 0x85 ,
- 'F23' : 0x86 ,
- 'F24' : 0x87 ,
- 'NumLock' : 0x90 ,
- 'ScrollLock' : 0x91 ,
- 'LShift' : 0xA0 ,
- 'RShift' : 0xA1 ,
- 'LCtrl' : 0xA2 ,
- 'RCrrl' : 0xA3 ,
- 'LMenu' : 0xA4 ,
- 'RMenu' : 0xA5 ,
- 'BrowserBack' : 0xA6 ,
- 'BrowserForward' : 0xA7 ,
- 'BrowserRefresh' : 0xA8 ,
- 'BrowserStop' : 0xA9 ,
- 'BrowserSearch' : 0xAA ,
- 'BrowserFavorites' : 0xAB ,
- 'BrowserStartandHome' : 0xAC ,
- 'VolumeMute' : 0xAD ,
- 'VolumeDown' : 0xAE ,
- 'VolumeUp' : 0xAF ,
- 'NextTrack' : 0xB0 ,
- 'PreviousTrack' : 0xB1 ,
- 'StopMedia' : 0xB2 ,
- 'PlayPauseMedia' : 0xB3 ,
- 'StartMail' : 0xB4 ,
- 'SelectMedia' : 0xB5 ,
- 'StartApplication1' : 0xB6 ,
- 'StartApplication2' : 0xB7 ,
- '`': 0xC0,
- '~': 0xC0,
- '-': 0xBD,
- '_': 0xBD,
- '=': 0xBB,
- '+': 0xBB,
- '[': 0xDB,
- '{': 0xDB,
- ']': 0xDD,
- '}': 0xDD,
- '\\': 0xDC,
- '|': 0xDC,
- ';': 0xBA,
- ':': 0xBA,
- "'": 0xDE,
- '"': 0xDE,
- ',': 0xBC,
- '<': 0xBC,
- '.': 0xBE,
- '>': 0xBE,
- '/': 0xBF,
- '?': 0xBF,
- '(': 0x39,
- ')': 0x30
- }
- SendInput = ctypes.windll.user32.SendInput
- # C struct redefinitions
- PUL = ctypes.POINTER(ctypes.c_ulong)
- class KeyBdInput(ctypes.Structure):
- _fields_ = [("wVk", ctypes.c_ushort),
- ("wScan", ctypes.c_ushort),
- ("dwFlags", ctypes.c_ulong),
- ("time", ctypes.c_ulong),
- ("dwExtraInfo", PUL)]
- class HardwareInput(ctypes.Structure):
- _fields_ = [("uMsg", ctypes.c_ulong),
- ("wParamL", ctypes.c_short),
- ("wParamH", ctypes.c_ushort)]
- 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", PUL)]
- class Input_I(ctypes.Union):
- _fields_ = [("ki", KeyBdInput),
- ("mi", MouseInput),
- ("hi", HardwareInput)]
- class Input(ctypes.Structure):
- _fields_ = [("type", ctypes.c_ulong),
- ("ii", Input_I)]
- # Actual Functions
- def press_key(key):
- """
- Simulates pressing a key
- Args:
- key: name of the key - keyCodes.py
- """
- hex_key_code = Key[key]
- extra = ctypes.c_ulong(0)
- ii_ = Input_I()
- ii_.ki = KeyBdInput( hex_key_code, 0x48, 0, 0, ctypes.pointer(extra) )
- x = Input( ctypes.c_ulong(1), ii_ )
- SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
- def release_key(key):
- """
- Simulates releasing a key
- Args:
- key: name of the key - keyCodes.py
- """
- hex_key_code = Key[key]
- extra = ctypes.c_ulong(0)
- ii_ = Input_I()
- ii_.ki = KeyBdInput( hex_key_code, 0x48, 0x0002, 0, ctypes.pointer(extra) )
- x = Input( ctypes.c_ulong(1), ii_ )
- SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
- def tap_key(key, number_of_taps=1, sleep_time=0.01):
- """
- Simulates press->release action for a key
- Args:
- key: name of the key - keyCodes.py
- number_of_taps: nomuber of times action will be repeated
- sleep_time: time between taps
- """
- for i in range (number_of_taps):
- press_key(key)
- time.sleep(sleep_time)
- release_key(key)
- def enter(number_of_taps=1, sleep_time=0.01):
- """
- Simulates press->release action for "Enter" key
- Args:
- number_of_taps: nomuber of times action will be repeated
- sleep_time: time between taps
- """
- tap_key('Enter', number_of_taps, sleep_time)
- def alt_tab(number_of_windows=2):
- """
- Simulates using Alt+Tab shortcut to change windows
- Args:
- number_of_windows: nomuber windows to change
- """
- press_key('Alt')
- tap_key('Tab',number_of_windows)
- release_key('Alt')
- time.sleep(0.1)
- def ctrl_v():
- """
- Simulates using Ctrl+V shortcut
- """
- press_key('LCtrl')
- tap_key('V',1,0.05)
- release_key('LCtrl')
- def ctrl_c():
- """
- Simulates using Ctrl+C shortcut
- """
- press_key('LCtrl')
- tap_key('C',1,0.05)
- release_key('LCtrl')
- def ctrl_a():
- """
- Simulates using Ctrl+A shortcut
- """
- press_key('LCtrl')
- tap_key('A',1,0.05)
- release_key('LCtrl')
- def write(string):
- """
- Writes a string using keyboard input
- Args:
- string: text to type using keyboard
- """
- mode = False
- for character in string.replace('\r\n', '\r').replace('\n', '\r'):
- if mode and character in LOWER:
- release_key('RShift')
- mode = False
- elif not mode and character in UPPER:
- press_key('RShift')
- mode = True
- character = ALTER.get(character, character)
- tap_key(character, sleep_time=0.005)
- if mode:
- release_key('RShift')
- def copy():
- """
- Simulates using Ctrl+C shortcut and returns copied value
- """
- r=Tk()
- r.withdraw()
- CREATE_NO_WINDOW = 0x08000000
- subprocess.Popen('clip',
- stdin=subprocess.PIPE,
- creationflags=CREATE_NO_WINDOW).communicate(input='')
- ctrl_c()
- copied_value = r.clipboard_get()
- r.destroy()
- return copied_value
- def tap_sequence(key_sequence, sleep_time = 0.01):
- """
- Simulates tapping a sequence of keys
- Args:
- key_sequence: - keyCodes.py
- sleep_time: time between keys
- """
- for key in key_sequence:
- tap_key(key,1,sleep_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement