TwitchPlays_KeyCodes.py
for Python 3.x
[ published on 2021-12-16 ]

  1. import time

  2. import ctypes

  3. import pynput

  4.  

  5. #############################################################

  6. #################### DIRECT X KEY CODES #####################

  7. #############################################################

  8.  

  9. # Key Codes found at: https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa299374(v=vs.60)

  10. Q = 0x10

  11. W = 0x11

  12. E = 0x12

  13. R = 0x13

  14. T = 0x14

  15. Y = 0x15

  16. U = 0x16

  17. I = 0x17

  18. O = 0x18

  19. P = 0x19

  20. A = 0x1E

  21. S = 0x1F

  22. D = 0x20

  23. F = 0x21

  24. G = 0x22

  25. H = 0x23

  26. J = 0x24

  27. K = 0x25

  28. L = 0x26

  29. Z = 0x2C

  30. X = 0x2D

  31. C = 0x2E

  32. V = 0x2F

  33. B = 0x30

  34. N = 0x31

  35. M = 0x32

  36. LEFT_ARROW = 0xCB

  37. RIGHT_ARROW = 0xCD

  38. UP_ARROW = 0xC8

  39. DOWN_ARROW = 0xD0

  40. ESC = 0x01

  41. ONE = 0x02

  42. TWO = 0x03

  43. THREE = 0x04

  44. FOUR = 0x05

  45. FIVE = 0x06

  46. SIX = 0x07

  47. SEVEN = 0x08

  48. EIGHT = 0x09

  49. NINE = 0x0A

  50. ZERO = 0x0B

  51. MINUS = 0x0C

  52. EQUALS = 0x0D

  53. BACKSPACE = 0x0E

  54. APOSTROPHE = 0x28

  55. SEMICOLON = 0x27

  56. TAB = 0x0F

  57. CAPSLOCK = 0x3A

  58. ENTER = 0x1C

  59. LEFT_CONTROL = 0x1D

  60. LEFT_ALT = 0x38

  61. LEFT_SHIFT = 0x2A

  62. RIGHT_SHIFT = 0x36

  63. TILDE = 0x29

  64. PRINTSCREEN = 0x37

  65. NUM_LOCK = 0x45

  66. SPACE = 0x39

  67. DELETE = 0x53

  68. COMMA = 0x33

  69. PERIOD = 0x34

  70. BACKSLASH = 0x35

  71. FORWARDSLASH = 0x2B

  72. LEFT_BRACKET = 0x1A

  73. RIGHT_BRACKET = 0x1B

  74. F1 = 0x3B

  75. F2 = 0x3C

  76. F3 = 0x3D

  77. F4 = 0x3E

  78. F5 = 0x3F

  79. F6 = 0x40

  80. F7 = 0x41

  81. F8 = 0x42

  82. F9 = 0x43

  83. F10 = 0x44

  84. F11 = 0x57

  85. F12 = 0x58

  86. NUMPAD_0 = 0x52

  87. NUMPAD_1 = 0x4F

  88. NUMPAD_2 = 0x50

  89. NUMPAD_3 = 0x51

  90. NUMPAD_4 = 0x4B

  91. NUMPAD_5 = 0x4C

  92. NUMPAD_6 = 0x4D

  93. NUMPAD_7 = 0x47

  94. NUMPAD_8 = 0x48

  95. NUMPAD_9 = 0x49

  96. NUMPAD_PLUS = 0x4E

  97. NUMPAD_MINUS = 0x4A

  98. NUMPAD_PERIOD = 0x53

  99. NUMPAD_ENTER = 0x9C

  100. NUMPAD_BACKSLASH = 0xB5

  101. LEFT_MOUSE = 0x100

  102. RIGHT_MOUSE = 0x101

  103. MIDDLE_MOUSE = 0x102

  104. MOUSE3 = 0x103

  105. MOUSE4 = 0x104

  106. MOUSE5 = 0x105

  107. MOUSE6 = 0x106

  108. MOUSE7 = 0x107

  109. MOUSE_WHEEL_UP = 0x108

  110. MOUSE_WHEEL_DOWN = 0x109

  111. #############################################################

  112. ################## DIRECT INPUT FUNCTIONS ###################

  113. #############################################################

  114.  

  115. # Direct Input functions found at: https://stackoverflow.com/questions/53643273/how-to-keep-pynput-and-ctypes-from-clashing

  116. # Use these to prevent conflict errors with pynput.

  117. SendInput = ctypes.windll.user32.SendInput

  118. def HoldKey(hexKeyCode):

  119.     extra = ctypes.c_ulong(0)

  120.     ii_ = pynput._util.win32.INPUT_union()

  121.     ii_.ki = pynput._util.win32.KEYBDINPUT(0, hexKeyCode, 0x0008, 0, ctypes.cast(ctypes.pointer(extra), ctypes.c_void_p))

  122.     x = pynput._util.win32.INPUT(ctypes.c_ulong(1), ii_)

  123.     SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

  124.  

  125. def ReleaseKey(hexKeyCode):

  126.     extra = ctypes.c_ulong(0)

  127.     ii_ = pynput._util.win32.INPUT_union()

  128.     ii_.ki = pynput._util.win32.KEYBDINPUT(0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.cast(ctypes.pointer(extra), ctypes.c_void_p))

  129.     x = pynput._util.win32.INPUT(ctypes.c_ulong(1), ii_)

  130.     SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

  131.  

  132. # Holds down a key for the specified number of seconds

  133. def HoldAndReleaseKey(hexKeyCode, seconds):

  134.     HoldKey(hexKeyCode)

  135.     time.sleep(seconds)

  136.     ReleaseKey(hexKeyCode)

End of Document