Stap 10: Programmering (Python)
seriële importeren
importeren van codecs
importeren van ctypes
importeren
importeren van tijd slaap
SendInput = ctypes.windll.user32.SendInput
PUL = ctypes. POINTER(ctypes.c_ulong)
klasse KeyBdInput(ctypes. Structuur):
_fields_ = [("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("tijd", ctypes.c_ulong),
("dwExtraInfo", PUL)]
klasse HardwareInput(ctypes. Structuur):
_fields_ = [("uMsg", ctypes.c_ulong),
("wParamL", ctypes.c_short),
("wParamH", ctypes.c_ushort)]
klasse MouseInput(ctypes. Structuur):
_fields_ = [("dx", ctypes.c_long),
("dy", ctypes.c_long),
("mouseData", ctypes.c_ulong),
("dwFlags", ctypes.c_ulong),
("time",ctypes.c_ulong),
("dwExtraInfo", PUL)]
klasse Input_I(ctypes. Unie):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("Hallo", HardwareInput)]
klasse Input (ctypes. Structuur):
_fields_ = [("type", ctypes.c_ulong)
("ii", Input_I)]
def PressKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.Ki = KeyBdInput (hexKeyCode, 0x48, 0, 0, ctypes.pointer(extra))
x = Input (ctypes.c_ulong(1), ii_)
ctypes.windll.user32.SendInput (1, ctypes.pointer(x), ctypes.sizeof(x))
def ReleaseKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.Ki = KeyBdInput (hexKeyCode, 0x48, 0x0002, 0, ctypes.pointer(extra))
x = Input (ctypes.c_ulong(1), ii_)
ctypes.windll.user32.SendInput (1, ctypes.pointer(x), ctypes.sizeof(x))
def PressW():
PressKey(0x57) #W
def ReleaseW():
ReleaseKey(0x57) #W
def PressA():
PressKey(0x41) #A
def ReleaseA():
ReleaseKey(0x41) #A
def PressS():
PressKey(0x53) #S
def ReleaseS():
ReleaseKey(0x53) #S
def PressD():
PressKey(0x44) #D
def ReleaseD():
ReleaseKey(0x44) #D
poort = "COM6"
ser seriële =. Seriële (poort, 9600, timeout = 0)
terwijl juist:
#data = ser.read(9999)
lijn = ser.readline()
Als regel:
afdrukken (' Got:', lijn)
Als regel == b '0\r\n':
Print('W_UP')
PressW()
Elif lijn == b '1\r\n':
Print('W_DOWN')
ReleaseW()
Als regel == b '2\r\n':
Print('S_UP')
PressS()
Elif lijn == b '3\r\n':
Print('S_DOWN')
ReleaseS()
Als regel == b '4\r\n':
Print('A_UP')
PressA()
Elif lijn == b '5\r\n':
Print('A_DOWN')
ReleaseA()
Als regel == b '6\r\n':
Print('D_UP')
PressD()
Elif lijn == b '7\r\n':
Print('D_DOWN')
ReleaseD()
Sleep(0.0125)
Print('0')
ser.Close()