Advertisement
Guest User

Works with minecraft i tested.jpg

a guest
Mar 23rd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. # direct inputs
  2. # source to this solution and code:
  3. # http://stackoverflow.com/questions/14489013/simulate-python-keypresses-for-controlling-a-game
  4. # http://www.gamespp.com/directx/directInputKeyboardScanCodes.html
  5.  
  6. import ctypes
  7. import time
  8.  
  9. SendInput = ctypes.windll.user32.SendInput
  10.  
  11.  
  12. W = 0x11
  13. A = 0x1E
  14. S = 0x1F
  15. D = 0x20
  16.  
  17. # C struct redefinitions
  18. PUL = ctypes.POINTER(ctypes.c_ulong)
  19. class KeyBdInput(ctypes.Structure):
  20.     _fields_ = [("wVk", ctypes.c_ushort),
  21.                 ("wScan", ctypes.c_ushort),
  22.                 ("dwFlags", ctypes.c_ulong),
  23.                 ("time", ctypes.c_ulong),
  24.                 ("dwExtraInfo", PUL)]
  25.  
  26. class HardwareInput(ctypes.Structure):
  27.     _fields_ = [("uMsg", ctypes.c_ulong),
  28.                 ("wParamL", ctypes.c_short),
  29.                 ("wParamH", ctypes.c_ushort)]
  30.  
  31. class MouseInput(ctypes.Structure):
  32.     _fields_ = [("dx", ctypes.c_long),
  33.                 ("dy", ctypes.c_long),
  34.                 ("mouseData", ctypes.c_ulong),
  35.                 ("dwFlags", ctypes.c_ulong),
  36.                 ("time",ctypes.c_ulong),
  37.                 ("dwExtraInfo", PUL)]
  38.  
  39. class Input_I(ctypes.Union):
  40.     _fields_ = [("ki", KeyBdInput),
  41.                  ("mi", MouseInput),
  42.                  ("hi", HardwareInput)]
  43.  
  44. class Input(ctypes.Structure):
  45.     _fields_ = [("type", ctypes.c_ulong),
  46.                 ("ii", Input_I)]
  47.  
  48. # Actuals Functions
  49.  
  50. def PressKey(hexKeyCode):
  51.     extra = ctypes.c_ulong(0)
  52.     ii_ = Input_I()
  53.     ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
  54.     x = Input( ctypes.c_ulong(1), ii_ )
  55.     ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
  56.  
  57. def ReleaseKey(hexKeyCode):
  58.     extra = ctypes.c_ulong(0)
  59.     ii_ = Input_I()
  60.     ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
  61.     x = Input( ctypes.c_ulong(1), ii_ )
  62.     ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
  63.  
  64. if __name__ == '__main__':
  65.     time.sleep(5)
  66.     print("started")
  67.     while True:
  68.         PressKey(0x11)
  69.         time.sleep(2)
  70.         ReleaseKey(0x11)
  71.         time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement