SHARE
TWEET

Python keyboard controller for games

a guest Mar 25th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import ctypes
  2.  
  3. PUL = ctypes.POINTER(ctypes.c_ulong)
  4. class KeyBdInput(ctypes.Structure):
  5.     _fields_ = [("wVk", ctypes.c_ushort),
  6.                 ("wScan", ctypes.c_ushort),
  7.                 ("dwFlags", ctypes.c_ulong),
  8.                 ("time", ctypes.c_ulong),
  9.                 ("dwExtraInfo", PUL)]
  10.  
  11. class HardwareInput(ctypes.Structure):
  12.     _fields_ = [("uMsg", ctypes.c_ulong),
  13.                 ("wParamL", ctypes.c_short),
  14.                 ("wParamH", ctypes.c_ushort)]
  15.  
  16. class MouseInput(ctypes.Structure):
  17.     _fields_ = [("dx", ctypes.c_long),
  18.                 ("dy", ctypes.c_long),
  19.                 ("mouseData", ctypes.c_ulong),
  20.                 ("dwFlags", ctypes.c_ulong),
  21.                 ("time",ctypes.c_ulong),
  22.                 ("dwExtraInfo", PUL)]
  23.  
  24. class Input_I(ctypes.Union):
  25.     _fields_ = [("ki", KeyBdInput),
  26.                  ("mi", MouseInput),
  27.                  ("hi", HardwareInput)]
  28.  
  29. class Input(ctypes.Structure):
  30.     _fields_ = [("type", ctypes.c_ulong),
  31.                 ("ii", Input_I)]
  32.  
  33. def PressKey(hexKeyCode):
  34.     extra = ctypes.c_ulong(0)
  35.     ii_ = Input_I()
  36.     ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
  37.     x = Input( ctypes.c_ulong(1), ii_ )
  38.     ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
  39.  
  40. def ReleaseKey(hexKeyCode):
  41.     extra = ctypes.c_ulong(0)
  42.     ii_ = Input_I()
  43.     ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
  44.     x = Input( ctypes.c_ulong(1), ii_ )
  45.     ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top