keyboard_manager.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import win32api
  2. import win32con
  3. import time
  4. import pyautogui
  5. import pyperclip
  6. key_map={
  7. 'backspace':0x08,
  8. 'tab':0x09,
  9. 'clear':0x0C,
  10. 'enter':0x0D,
  11. 'shift':0x10,
  12. 'ctrl':0x11,
  13. 'alt':0x12,
  14. 'pause':0x13,
  15. 'caps_lock':0x14,
  16. 'esc':0x1B,
  17. 'spacebar':0x20,
  18. 'page_up':0x21,
  19. 'page_down':0x22,
  20. 'end':0x23,
  21. 'home':0x24,
  22. 'left_arrow':0x25,
  23. 'up_arrow':0x26,
  24. 'right_arrow':0x27,
  25. 'down_arrow':0x28,
  26. 'select':0x29,
  27. 'print':0x2A,
  28. 'execute':0x2B,
  29. 'print_screen':0x2C,
  30. 'ins':0x2D,
  31. 'del':0x2E,
  32. 'help':0x2F,
  33. '0':0x30,
  34. '1':0x31,
  35. '2':0x32,
  36. '3':0x33,
  37. '4':0x34,
  38. '5':0x35,
  39. '6':0x36,
  40. '7':0x37,
  41. '8':0x38,
  42. '9':0x39,
  43. 'a':0x41,
  44. 'b':0x42,
  45. 'c':0x43,
  46. 'd':0x44,
  47. 'e':0x45,
  48. 'f':0x46,
  49. 'g':0x47,
  50. 'h':0x48,
  51. 'i':0x49,
  52. 'j':0x4A,
  53. 'k':0x4B,
  54. 'l':0x4C,
  55. 'm':0x4D,
  56. 'n':0x4E,
  57. 'o':0x4F,
  58. 'p':0x50,
  59. 'q':0x51,
  60. 'r':0x52,
  61. 's':0x53,
  62. 't':0x54,
  63. 'u':0x55,
  64. 'v':0x56,
  65. 'w':0x57,
  66. 'x':0x58,
  67. 'y':0x59,
  68. 'z':0x5A,
  69. 'win':0x5B,
  70. 'numpad_0':0x60,
  71. 'numpad_1':0x61,
  72. 'numpad_2':0x62,
  73. 'numpad_3':0x63,
  74. 'numpad_4':0x64,
  75. 'numpad_5':0x65,
  76. 'numpad_6':0x66,
  77. 'numpad_7':0x67,
  78. 'numpad_8':0x68,
  79. 'numpad_9':0x69,
  80. 'multiply_key':0x6A,
  81. 'add_key':0x6B,
  82. 'separator_key':0x6C,
  83. 'subtract_key':0x6D,
  84. 'decimal_key':0x6E,
  85. 'divide_key':0x6F,
  86. 'F1':0x70,
  87. 'F2':0x71,
  88. 'F3':0x72,
  89. 'F4':0x73,
  90. 'F5':0x74,
  91. 'F6':0x75,
  92. 'F7':0x76,
  93. 'F8':0x77,
  94. 'F9':0x78,
  95. 'F10':0x79,
  96. 'F11':0x7A,
  97. 'F12':0x7B,
  98. 'F13':0x7C,
  99. 'F14':0x7D,
  100. 'F15':0x7E,
  101. 'F16':0x7F,
  102. 'F17':0x80,
  103. 'F18':0x81,
  104. 'F19':0x82,
  105. 'F20':0x83,
  106. 'F21':0x84,
  107. 'F22':0x85,
  108. 'F23':0x86,
  109. 'F24':0x87,
  110. 'num_lock':0x90,
  111. 'scroll_lock':0x91,
  112. 'left_shift':0xA0,
  113. 'right_shift ':0xA1,
  114. 'left_control':0xA2,
  115. 'right_control':0xA3,
  116. 'left_menu':0xA4,
  117. 'right_menu':0xA5,
  118. 'browser_back':0xA6,
  119. 'browser_forward':0xA7,
  120. 'browser_refresh':0xA8,
  121. 'browser_stop':0xA9,
  122. 'browser_search':0xAA,
  123. 'browser_favorites':0xAB,
  124. 'browser_start_and_home':0xAC,
  125. 'volume_mute':0xAD,
  126. 'volume_Down':0xAE,
  127. 'volume_up':0xAF,
  128. 'next_track':0xB0,
  129. 'previous_track':0xB1,
  130. 'stop_media':0xB2,
  131. 'play/pause_media':0xB3,
  132. 'start_mail':0xB4,
  133. 'select_media':0xB5,
  134. 'start_application_1':0xB6,
  135. 'start_application_2':0xB7,
  136. 'attn_key':0xF6,
  137. 'crsel_key':0xF7,
  138. 'exsel_key':0xF8,
  139. 'play_key':0xFA,
  140. 'zoom_key':0xFB,
  141. 'clear_key':0xFE,
  142. '+':0xBB,
  143. ',':0xBC,
  144. '-':0xBD,
  145. '.':0xBE,
  146. '/':0xBF,
  147. '`':0xC0,
  148. ';':0xBA,
  149. '[':0xDB,
  150. '\\':0xDC,
  151. ']':0xDD,
  152. "'":0xDE,
  153. '`':0xC0
  154. }
  155. def key_down(key):
  156. vk_code = key_map[key]
  157. win32api.keybd_event(vk_code, win32api.MapVirtualKey(vk_code, 0), 0, 0)
  158. def key_up(key):
  159. vk_code = key_map[key]
  160. win32api.keybd_event(vk_code, win32api.MapVirtualKey(vk_code, 0), win32con.KEYEVENTF_KEYUP, 0)
  161. def key_press(key, sleep_time=0.02):
  162. key_down(key)
  163. time.sleep(sleep_time)
  164. key_up(key)
  165. # time.sleep(3)
  166. # key_down("alt")
  167. # key_down("h")
  168. # key_press("f")
  169. # key_press("c")
  170. # key_up("alt")
  171. # key_up("h")
  172. # key_press("down_arrow",sleep_time=0)
  173. # key_press("down_arrow",sleep_time=0)
  174. # key_press("down_arrow",sleep_time=0)
  175. # key_press("down_arrow",sleep_time=0)
  176. # key_press("down_arrow",sleep_time=0)
  177. # key_press("down_arrow",sleep_time=0)
  178. # key_press("right_arrow",sleep_time=0)
  179. # key_press("enter")
  180. # # 输入 a
  181. # #key_press("a")
  182. # key_press('enter')
  183. # key_press('a')