SHARE
TWEET

send_input.py

a guest Feb 27th, 2018 401 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding: utf-8
  2. """
  3. Simple unicode keyboard automation for windows
  4. Based off of http://stackoverflow.com/questions/11906925/python-simulate-keydown
  5. """
  6.  
  7. import ctypes
  8. import time
  9. import sys
  10.  
  11. LONG = ctypes.c_long
  12. DWORD = ctypes.c_ulong
  13. ULONG_PTR = ctypes.POINTER(DWORD)
  14. WORD = ctypes.c_ushort
  15.  
  16. INPUT_MOUSE = 0
  17. INPUT_KEYBOARD = 1
  18. INPUT_HARDWARE = 2
  19.  
  20. KEYEVENTF_EXTENDEDKEY = 0x0001
  21. KEYEVENTF_KEYUP = 0x0002
  22. KEYEVENTF_SCANCODE = 0x0008
  23. KEYEVENTF_UNICODE = 0x0004
  24.  
  25.  
  26. class MOUSEINPUT(ctypes.Structure):
  27.     _fields_ = (('dx', LONG),
  28.                 ('dy', LONG),
  29.                 ('mouseData', DWORD),
  30.                 ('dwFlags', DWORD),
  31.                 ('time', DWORD),
  32.                 ('dwExtraInfo', ULONG_PTR))
  33.  
  34.  
  35. class KEYBDINPUT(ctypes.Structure):
  36.     _fields_ = (('wVk', WORD),
  37.                 ('wScan', WORD),
  38.                 ('dwFlags', DWORD),
  39.                 ('time', DWORD),
  40.                 ('dwExtraInfo', ULONG_PTR))
  41.  
  42.  
  43. class HARDWAREINPUT(ctypes.Structure):
  44.     _fields_ = (('uMsg', DWORD),
  45.                 ('wParamL', WORD),
  46.                 ('wParamH', WORD))
  47.  
  48.  
  49. class _INPUTunion(ctypes.Union):
  50.     _fields_ = (('mi', MOUSEINPUT),
  51.                 ('ki', KEYBDINPUT),
  52.                 ('hi', HARDWAREINPUT))
  53.  
  54.  
  55. class INPUT(ctypes.Structure):
  56.     _fields_ = (('type', DWORD),
  57.                 ('union', _INPUTunion))
  58.  
  59.  
  60. def send_input(*inputs):
  61.     nInputs = len(inputs)
  62.     LPINPUT = INPUT * nInputs
  63.     pInputs = LPINPUT(*inputs)
  64.     cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
  65.     return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
  66.  
  67.  
  68. def input_structure(structure):
  69.     if isinstance(structure, MOUSEINPUT):
  70.         return INPUT(INPUT_MOUSE, _INPUTunion(mi=structure))
  71.     if isinstance(structure, KEYBDINPUT):
  72.         return INPUT(INPUT_KEYBOARD, _INPUTunion(ki=structure))
  73.     if isinstance(structure, HARDWAREINPUT):
  74.         return INPUT(INPUT_HARDWARE, _INPUTunion(hi=structure))
  75.     raise TypeError('Cannot create INPUT structure!')
  76.  
  77.  
  78. def keyboard_input(code, flags):
  79.     return KEYBDINPUT(0, code, flags, 0, None)
  80.  
  81.  
  82. def keyboard_event(code, flags=KEYEVENTF_UNICODE):
  83.     return input_structure(keyboard_input(code, flags))
  84.  
  85.  
  86. def press(character):
  87.     code = ord(character)
  88.     code = ctypes.windll.user32.MapVirtualKeyW(code, 0)
  89.     send_input(keyboard_event(code, KEYEVENTF_SCANCODE))
  90.     send_input(keyboard_event(code, KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP))
  91.  
  92.  
  93. s = u'529367770827761809281925381945823314415654883200956546562119403749694799090814703264634710691547582753945986231555502753441533398291527365851914648618152088022990467703916209393932319280164651806605296243521499048284875309EINFGEXCRWIYJFDVTMPABOVJKUQVTFZTQKNCLBAPYDFRONCEFXEZHXDERFKGQDHCHZRJUHIMIKOLAVOVTCEFKUPTXXJSFWBDVWVVCPZZODXLGSPJTFDCHPHXBBBDCZRFLWQJIXEJNQOHZIHWVINZWFNFHDSXEVPJAGCEWLEMCMKETQAAUHUXBLCNUDQOOVGNZARDBNMGJYBLHBYAYWDABDYLDXJRBY5PZNCGY0JH442PAYQVL0EN97NZ6P8LKIL3XTZT5R7BPUBHR8PFUSQ3EQO0WY4CN58XB6A9XWPUEM5TA4FXF4F17SFKEMI93TWWXRBJY6E2K4GG8FM566NVZMRMMY18NSITHSRMJRYZ2DBRZVD8KPXXR645R5QOQEGM3PMXZJ5OIXCOIOOMSPOP9GDL7079AQB4C6WQ9P6E9PPF7Q9P5Z7CDEHM4E2O'
  94.  
  95. def main():
  96.     time.sleep(3)
  97.     for char in s:
  98.         press(char)
  99.         #time.sleep(0.05)
  100.  
  101. if __name__ == '__main__':
  102.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top