top of page

TwitchPlays_KeyCodes.py
for Python 3.9.x
[ published on 2022-08-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. ​

  37. LEFT_ARROW = 0xCB

  38. RIGHT_ARROW = 0xCD

  39. UP_ARROW = 0xC8

  40. DOWN_ARROW = 0xD0

  41. ESC = 0x01

  42. ONE = 0x02

  43. TWO = 0x03

  44. THREE = 0x04

  45. FOUR = 0x05

  46. FIVE = 0x06

  47. SIX = 0x07

  48. SEVEN = 0x08

  49. EIGHT = 0x09

  50. NINE = 0x0A

  51. ZERO = 0x0B

  52. MINUS = 0x0C

  53. EQUALS = 0x0D

  54. BACKSPACE = 0x0E

  55. APOSTROPHE = 0x28

  56. SEMICOLON = 0x27

  57. TAB = 0x0F

  58. CAPSLOCK = 0x3A

  59. ENTER = 0x1C

  60. LEFT_CONTROL = 0x1D

  61. LEFT_ALT = 0x38

  62. LEFT_SHIFT = 0x2A

  63. RIGHT_SHIFT = 0x36

  64. TILDE = 0x29

  65. PRINTSCREEN = 0x37

  66. NUM_LOCK = 0x45

  67. SPACE = 0x39

  68. DELETE = 0x53

  69. COMMA = 0x33

  70. PERIOD = 0x34

  71. BACKSLASH = 0x35

  72. FORWARDSLASH = 0x2B

  73. LEFT_BRACKET = 0x1A

  74. RIGHT_BRACKET = 0x1B

  75. ​

  76. F1 = 0x3B

  77. F2 = 0x3C

  78. F3 = 0x3D

  79. F4 = 0x3E

  80. F5 = 0x3F

  81. F6 = 0x40

  82. F7 = 0x41

  83. F8 = 0x42

  84. F9 = 0x43

  85. F10 = 0x44

  86. F11 = 0x57

  87. F12 = 0x58

  88. ​

  89. NUMPAD_0 = 0x52

  90. NUMPAD_1 = 0x4F

  91. NUMPAD_2 = 0x50

  92. NUMPAD_3 = 0x51

  93. NUMPAD_4 = 0x4B

  94. NUMPAD_5 = 0x4C

  95. NUMPAD_6 = 0x4D

  96. NUMPAD_7 = 0x47

  97. NUMPAD_8 = 0x48

  98. NUMPAD_9 = 0x49

  99. NUMPAD_PLUS = 0x4E

  100. NUMPAD_MINUS = 0x4A

  101. NUMPAD_PERIOD = 0x53

  102. NUMPAD_ENTER = 0x9C

  103. NUMPAD_BACKSLASH = 0xB5

  104. ​

  105. LEFT_MOUSE = 0x100

  106. RIGHT_MOUSE = 0x101

  107. MIDDLE_MOUSE = 0x102

  108. MOUSE3 = 0x103

  109. MOUSE4 = 0x104

  110. MOUSE5 = 0x105

  111. MOUSE6 = 0x106

  112. MOUSE7 = 0x107

  113. MOUSE_WHEEL_UP = 0x108

  114. MOUSE_WHEEL_DOWN = 0x109

  115. ​

  116. ​

  117. #############################################################

  118. ################## DIRECT INPUT FUNCTIONS ###################

  119. #############################################################

  120.  

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

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

  123. SendInput = ctypes.windll.user32.SendInput

  124. ​

  125. def HoldKey(hexKeyCode):

  126.     extra = ctypes.c_ulong(0)

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

  128.     ii_.ki = pynput._util.win32.KEYBDINPUT(0, hexKeyCode, 0x0008, 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. def ReleaseKey(hexKeyCode):

  133.     extra = ctypes.c_ulong(0)

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

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

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

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

  138.  

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

  140. def HoldAndReleaseKey(hexKeyCode, seconds):

  141.     HoldKey(hexKeyCode)

  142.     time.sleep(seconds)

  143.     ReleaseKey(hexKeyCode)

End of Document

bottom of page