-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard_events.py
107 lines (89 loc) · 3.03 KB
/
keyboard_events.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import time
import threading
# Install readchar with: <-- not used anymore
# pip install readchar
# https://github.com/magmax/python-readchar
#import readchar
from events_base import EventsBase
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
from select import select
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
[i, o, e] = select([sys.stdin.fileno()], [], [], 2)
if i: ch=sys.stdin.read(1)
else: ch=''
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
class KeyboardEvents(EventsBase):
def __init__(self):
print "launching keyboard listener thread"
self.terminate = threading.Event()
self.thread = threading.Thread(name='monitor', target=self.monitor_events)
self.thread.start()
self.volume = 8
def monitor_events(self):
while not self.terminate.isSet():
c = getch()
#c = readchar.readchar()
if c == '0':
self.add_event( (self.KEY, self.KEY_RESET) )
elif c == 'a':
self.add_event( (self.KEY, self.KEY_LEFT) )
elif c == 'd':
self.add_event( (self.KEY, self.KEY_RIGHT) )
elif c == 'w':
self.add_event( (self.KEY, self.KEY_UP) )
elif c == 'x':
self.add_event( (self.KEY, self.KEY_DOWN) )
elif c == 's':
self.add_event( (self.KEY, self.KEY_SELECT) )
elif c == '1':
self.add_event( (self.MODE, 3) )
self.mode = 3
elif c == '2':
self.add_event( (self.MODE, 2) )
self.mode = 2
elif c == '3':
self.add_event( (self.MODE, 0) )
self.mode = 0
elif c == '4':
self.add_event( (self.MODE, 1) )
self.mode = 1
elif c== '+' :
self.volume = self.volume+1
if self.volume > 10:
self.volume = 10
self.add_event( (self.VOLUME, self.volume) )
elif c== '-' :
if self.volume < 0:
self.volume = 0
self.volume = self.volume-1
self.add_event( (self.VOLUME, self.volume) )
def stop(self):
self.terminate.set()
self.thread.join()
print "keyboard listener stopped."