forked from dciets/Arcade-CS-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeypress_emu.py
More file actions
executable file
·37 lines (27 loc) · 790 Bytes
/
keypress_emu.py
File metadata and controls
executable file
·37 lines (27 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
import serial
from serial.tools import list_ports
from pykeyboard import PyKeyboard
import sys
import select
ports = list_ports.comports()
k = PyKeyboard()
keys = 'W A S D E U H J K I'.split()
if ports:
ser = serial.Serial(ports[0].device, 115200, timeout=0)
while True:
char = ser.read()
if char:
code = ord(char)
index = code >> 1
state = code & 1
if state == 1:
k.press_key(keys[index])
else:
k.release_key(keys[index])
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
command = sys.stdin.readline().strip()
if command:
ser.write(command[0])
else:
print 'No serial ports found'