Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.21 KB | None | 0 0
  1. import ctypes
  2. import time
  3. from Tkinter import Tk
  4. import subprocess
  5.  
  6. UPPER = frozenset('~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?')
  7. LOWER = frozenset("`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./")
  8. ALTER = dict(zip('!@#$%^&*()', '1234567890'))
  9.  
  10. Key = {
  11. 'MouseL' : 0x01 ,
  12. 'MouseR' : 0x02 ,
  13. 'Cancel' : 0x03 ,
  14. 'MouseM' : 0x04 ,
  15. 'MouseX1' : 0x05 ,
  16. 'MouseX2' : 0x06 ,
  17. 'Backspace' : 0x08 ,
  18. 'Tab' : 0x09 ,
  19. 'Clear' : 0x0C ,
  20. 'Enter' : 0x0D ,
  21. 'Shift' : 0x10 ,
  22. 'Ctrl' : 0x11 ,
  23. 'Alt' : 0x12 ,
  24. 'Pause' : 0x13 ,
  25. 'Caps' : 0x14 ,
  26. 'Esc' : 0x1B ,
  27. 'Accept' : 0x1E ,
  28. 'Space' : 0x20 ,
  29. ' ' : 0x20 ,
  30. 'PageUp' : 0x21 ,
  31. 'PageDown' : 0x22 ,
  32. 'End' : 0x23 ,
  33. 'Home' : 0x24 ,
  34. 'Left' : 0x25 ,
  35. 'Up' : 0x26 ,
  36. 'Right' : 0x27 ,
  37. 'Down' : 0x28 ,
  38. 'Select' : 0x29 ,
  39. 'Print' : 0x2A ,
  40. 'Execute' : 0x2B ,
  41. 'PrintScreen' : 0x2C ,
  42. 'Ins' : 0x2D ,
  43. 'Del' : 0x2E ,
  44. 'Help' : 0x2F ,
  45. ',' : 0x2C ,
  46. '0' : 0x30 ,
  47. '1' : 0x31 ,
  48. '2' : 0x32 ,
  49. '3' : 0x33 ,
  50. '4' : 0x34 ,
  51. '5' : 0x35 ,
  52. '6' : 0x36 ,
  53. '7' : 0x37 ,
  54. '8' : 0x38 ,
  55. '9' : 0x39 ,
  56. 'A' : 0x41 ,
  57. 'B' : 0x42 ,
  58. 'C' : 0x43 ,
  59. 'D' : 0x44 ,
  60. 'E' : 0x45 ,
  61. 'F' : 0x46 ,
  62. 'G' : 0x47 ,
  63. 'H' : 0x48 ,
  64. 'I' : 0x49 ,
  65. 'J' : 0x4A ,
  66. 'K' : 0x4B ,
  67. 'L' : 0x4C ,
  68. 'M' : 0x4D ,
  69. 'N' : 0x4E ,
  70. 'O' : 0x4F ,
  71. 'P' : 0x50 ,
  72. 'Q' : 0x51 ,
  73. 'R' : 0x52 ,
  74. 'S' : 0x53 ,
  75. 'T' : 0x54 ,
  76. 'U' : 0x55 ,
  77. 'V' : 0x56 ,
  78. 'W' : 0x57 ,
  79. 'X' : 0x58 ,
  80. 'Y' : 0x59 ,
  81. 'Z' : 0x5A ,
  82. 'a' : 0x41 ,
  83. 'b' : 0x42 ,
  84. 'c' : 0x43 ,
  85. 'd' : 0x44 ,
  86. 'e' : 0x45 ,
  87. 'f' : 0x46 ,
  88. 'g' : 0x47 ,
  89. 'h' : 0x48 ,
  90. 'i' : 0x49 ,
  91. 'j' : 0x4A ,
  92. 'k' : 0x4B ,
  93. 'l' : 0x4C ,
  94. 'm' : 0x4D ,
  95. 'n' : 0x4E ,
  96. 'o' : 0x4F ,
  97. 'p' : 0x50 ,
  98. 'q' : 0x51 ,
  99. 'r' : 0x52 ,
  100. 's' : 0x53 ,
  101. 't' : 0x54 ,
  102. 'u' : 0x55 ,
  103. 'v' : 0x56 ,
  104. 'w' : 0x57 ,
  105. 'x' : 0x58 ,
  106. 'y' : 0x59 ,
  107. 'z' : 0x5A ,
  108. 'LeftWin' : 0x5B ,
  109. 'RightWin' : 0x5C ,
  110. 'Applications' : 0x5D ,
  111. 'Reserved' : 0x5E ,
  112. 'Sleep' : 0x5F ,
  113. 'Num0' : 0x60 ,
  114. 'Num1' : 0x61 ,
  115. 'Num2' : 0x62 ,
  116. 'Num3' : 0x63 ,
  117. 'Num4' : 0x64 ,
  118. 'Num5' : 0x65 ,
  119. 'Num6' : 0x66 ,
  120. 'Num7' : 0x67 ,
  121. 'Num8' : 0x68 ,
  122. 'Num9' : 0x69 ,
  123. 'Multiply' : 0x6A ,
  124. 'Add' : 0x6B ,
  125. 'Separator' : 0x6C ,
  126. 'Subtract' : 0x6D ,
  127. 'Decimal' : 0x6E ,
  128. 'Divide' : 0x6F ,
  129. 'F1' : 0x70 ,
  130. 'F2' : 0x71 ,
  131. 'F3' : 0x72 ,
  132. 'F4' : 0x73 ,
  133. 'F5' : 0x74 ,
  134. 'F6' : 0x75 ,
  135. 'F7' : 0x76 ,
  136. 'F8' : 0x77 ,
  137. 'F9' : 0x78 ,
  138. 'F10' : 0x79 ,
  139. 'F11' : 0x7A ,
  140. 'F12' : 0x7B ,
  141. 'F13' : 0x7C ,
  142. 'F14' : 0x7D ,
  143. 'F15' : 0x7E ,
  144. 'F16' : 0x7F ,
  145. 'F17' : 0x80 ,
  146. 'F18' : 0x81 ,
  147. 'F19' : 0x82 ,
  148. 'F20' : 0x83 ,
  149. 'F21' : 0x84 ,
  150. 'F22' : 0x85 ,
  151. 'F23' : 0x86 ,
  152. 'F24' : 0x87 ,
  153. 'NumLock' : 0x90 ,
  154. 'ScrollLock' : 0x91 ,
  155. 'LShift' : 0xA0 ,
  156. 'RShift' : 0xA1 ,
  157. 'LCtrl' : 0xA2 ,
  158. 'RCrrl' : 0xA3 ,
  159. 'LMenu' : 0xA4 ,
  160. 'RMenu' : 0xA5 ,
  161. 'BrowserBack' : 0xA6 ,
  162. 'BrowserForward' : 0xA7 ,
  163. 'BrowserRefresh' : 0xA8 ,
  164. 'BrowserStop' : 0xA9 ,
  165. 'BrowserSearch' : 0xAA ,
  166. 'BrowserFavorites' : 0xAB ,
  167. 'BrowserStartandHome' : 0xAC ,
  168. 'VolumeMute' : 0xAD ,
  169. 'VolumeDown' : 0xAE ,
  170. 'VolumeUp' : 0xAF ,
  171. 'NextTrack' : 0xB0 ,
  172. 'PreviousTrack' : 0xB1 ,
  173. 'StopMedia' : 0xB2 ,
  174. 'PlayPauseMedia' : 0xB3 ,
  175. 'StartMail' : 0xB4 ,
  176. 'SelectMedia' : 0xB5 ,
  177. 'StartApplication1' : 0xB6 ,
  178. 'StartApplication2' : 0xB7 ,
  179. '`': 0xC0,
  180. '~': 0xC0,
  181. '-': 0xBD,
  182. '_': 0xBD,
  183. '=': 0xBB,
  184. '+': 0xBB,
  185. '[': 0xDB,
  186. '{': 0xDB,
  187. ']': 0xDD,
  188. '}': 0xDD,
  189. '\\': 0xDC,
  190. '|': 0xDC,
  191. ';': 0xBA,
  192. ':': 0xBA,
  193. "'": 0xDE,
  194. '"': 0xDE,
  195. ',': 0xBC,
  196. '<': 0xBC,
  197. '.': 0xBE,
  198. '>': 0xBE,
  199. '/': 0xBF,
  200. '?': 0xBF,
  201. '(': 0x39,
  202. ')': 0x30
  203. }
  204.  
  205.  
  206. SendInput = ctypes.windll.user32.SendInput
  207.  
  208. # C struct redefinitions
  209. PUL = ctypes.POINTER(ctypes.c_ulong)
  210. class KeyBdInput(ctypes.Structure):
  211.         _fields_ = [("wVk", ctypes.c_ushort),
  212.              ("wScan", ctypes.c_ushort),
  213.              ("dwFlags", ctypes.c_ulong),
  214.              ("time", ctypes.c_ulong),
  215.              ("dwExtraInfo", PUL)]
  216.  
  217. class HardwareInput(ctypes.Structure):
  218.     _fields_ = [("uMsg", ctypes.c_ulong),
  219.              ("wParamL", ctypes.c_short),
  220.              ("wParamH", ctypes.c_ushort)]
  221.  
  222. class MouseInput(ctypes.Structure):
  223.     _fields_ = [("dx", ctypes.c_long),
  224.              ("dy", ctypes.c_long),
  225.              ("mouseData", ctypes.c_ulong),
  226.              ("dwFlags", ctypes.c_ulong),
  227.              ("time",ctypes.c_ulong),
  228.              ("dwExtraInfo", PUL)]
  229.  
  230. class Input_I(ctypes.Union):
  231.     _fields_ = [("ki", KeyBdInput),
  232.               ("mi", MouseInput),
  233.               ("hi", HardwareInput)]
  234.  
  235. class Input(ctypes.Structure):
  236.     _fields_ = [("type", ctypes.c_ulong),
  237.              ("ii", Input_I)]
  238.  
  239. # Actual Functions
  240.  
  241. def press_key(key):
  242.         """
  243.        Simulates pressing a key
  244.             Args:
  245.                key: name of the key - keyCodes.py
  246.        """
  247.         hex_key_code = Key[key]
  248.         extra = ctypes.c_ulong(0)
  249.         ii_ = Input_I()
  250.         ii_.ki = KeyBdInput( hex_key_code, 0x48, 0, 0, ctypes.pointer(extra) )
  251.         x = Input( ctypes.c_ulong(1), ii_ )
  252.         SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
  253.  
  254. def release_key(key):
  255.         """
  256.        Simulates releasing a key
  257.             Args:
  258.                key: name of the key - keyCodes.py
  259.        """
  260.         hex_key_code = Key[key]
  261.         extra = ctypes.c_ulong(0)
  262.         ii_ = Input_I()
  263.         ii_.ki = KeyBdInput( hex_key_code, 0x48, 0x0002, 0, ctypes.pointer(extra) )
  264.         x = Input( ctypes.c_ulong(1), ii_ )
  265.         SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
  266.  
  267. def tap_key(key, number_of_taps=1, sleep_time=0.01):
  268.         """
  269.        Simulates press->release action for a key
  270.             Args:
  271.                key: name of the key - keyCodes.py
  272.                number_of_taps: nomuber of times action will be repeated
  273.                sleep_time: time between taps
  274.        """
  275.         for i in range (number_of_taps):
  276.                 press_key(key)
  277.                 time.sleep(sleep_time)
  278.                 release_key(key)
  279.  
  280. def enter(number_of_taps=1, sleep_time=0.01):
  281.         """
  282.        Simulates press->release action for "Enter" key
  283.             Args:
  284.                number_of_taps: nomuber of times action will be repeated
  285.                sleep_time: time between taps
  286.        """
  287.         tap_key('Enter', number_of_taps, sleep_time)
  288.  
  289.  
  290. def alt_tab(number_of_windows=2):
  291.         """
  292.        Simulates using Alt+Tab shortcut to change windows
  293.             Args:
  294.                number_of_windows: nomuber windows to change
  295.        """
  296.         press_key('Alt')
  297.         tap_key('Tab',number_of_windows)
  298.         release_key('Alt')
  299.         time.sleep(0.1)
  300.  
  301. def ctrl_v():
  302.         """
  303.        Simulates using Ctrl+V shortcut
  304.        """
  305.         press_key('LCtrl')
  306.         tap_key('V',1,0.05)
  307.         release_key('LCtrl')
  308.  
  309. def ctrl_c():
  310.         """
  311.        Simulates using Ctrl+C shortcut
  312.        """
  313.         press_key('LCtrl')
  314.         tap_key('C',1,0.05)
  315.         release_key('LCtrl')
  316.  
  317. def ctrl_a():
  318.         """
  319.        Simulates using Ctrl+A shortcut
  320.        """
  321.         press_key('LCtrl')
  322.         tap_key('A',1,0.05)
  323.         release_key('LCtrl')
  324.  
  325. def write(string):
  326.         """
  327.        Writes a string using keyboard input
  328.                Args:
  329.                        string: text to type using keyboard
  330.        """
  331.         mode = False
  332.         for character in string.replace('\r\n', '\r').replace('\n', '\r'):
  333.                 if mode and character in LOWER:
  334.                         release_key('RShift')
  335.                         mode = False
  336.                 elif not mode and character in UPPER:
  337.                         press_key('RShift')
  338.                         mode = True
  339.                 character = ALTER.get(character, character)
  340.                 tap_key(character, sleep_time=0.005)
  341.         if mode:
  342.                 release_key('RShift')
  343.        
  344. def copy():
  345.         """
  346.        Simulates using Ctrl+C shortcut and returns copied value
  347.        """
  348.         r=Tk()
  349.         r.withdraw()
  350.         CREATE_NO_WINDOW = 0x08000000
  351.         subprocess.Popen('clip',
  352.                      stdin=subprocess.PIPE,
  353.                      creationflags=CREATE_NO_WINDOW).communicate(input='')
  354.         ctrl_c()
  355.         copied_value = r.clipboard_get()
  356.         r.destroy()
  357.         return copied_value
  358.  
  359. def tap_sequence(key_sequence, sleep_time = 0.01):
  360.         """
  361.        Simulates tapping a sequence of keys
  362.                Args:
  363.                        key_sequence: - keyCodes.py
  364.                        sleep_time: time between keys
  365.        """
  366.         for key in key_sequence:
  367.                 tap_key(key,1,sleep_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement