direct input keys in opencv

Solutions on MaxInterview for direct input keys in opencv by the best coders in the world

showing results for - "direct input keys in opencv"
Oscar
14 Oct 2020
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
6import ctypes
7import time
8
9SendInput = ctypes.windll.user32.SendInput
10
11
12W = 0x11
13A = 0x1E
14S = 0x1F
15D = 0x20
16
17# C struct redefinitions 
18PUL = ctypes.POINTER(ctypes.c_ulong)
19class 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
26class HardwareInput(ctypes.Structure):
27    _fields_ = [("uMsg", ctypes.c_ulong),
28                ("wParamL", ctypes.c_short),
29                ("wParamH", ctypes.c_ushort)]
30
31class 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
39class Input_I(ctypes.Union):
40    _fields_ = [("ki", KeyBdInput),
41                 ("mi", MouseInput),
42                 ("hi", HardwareInput)]
43
44class Input(ctypes.Structure):
45    _fields_ = [("type", ctypes.c_ulong),
46                ("ii", Input_I)]
47
48# Actuals Functions
49
50def 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
57def 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
64if __name__ == '__main__':
65    PressKey(0x11)
66    time.sleep(1)
67    ReleaseKey(0x11)
68    time.sleep(1)
69