-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.py
56 lines (50 loc) · 1.25 KB
/
keys.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
UPP = 'UPPER'
LOW = 'LOWER'
MID = 'MIDDLE'
NON = 'NEITHER'
def M(c):
return '{#Alt_L(' + c + ')}'
def C(c):
return '{#Control_L(' + c + ')}'
def keys_to_number(keys):
assert(len(keys) == 4)
m = 1
t = 0
for key in keys:
if key:
t += m
m *= 2
return t
def position_to_keys(position):
if position == LOW:
keys = (True, False)
elif position == UPP:
keys = (False, True)
elif position == MID:
keys = (True, True)
elif position == NON:
keys = (False, False)
else:
raise ValueError('Unknown position {}'.format(position))
return keys
def positions_to_keys(positions, reversed=False):
keys = []
for position in positions:
if reversed:
keys[0:0] = position_to_keys(position)
else:
keys += position_to_keys(position)
return tuple(keys)
def keys_to_positions(keys):
assert(len(keys) % 2 == 0)
positions = []
for index in range(0, len(keys), 2):
keyA = keys[index]
keyB = keys[index+1]
positions.append({
(True, True): MID,
(True, False): LOW,
(False, True): UPP,
(False, False): NON,
}[(keyA, keyB)])
return positions