-
Notifications
You must be signed in to change notification settings - Fork 22
/
key.py
110 lines (89 loc) · 2.55 KB
/
key.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
108
109
# Defines the functions needed for the translation of evlog.txt.
from keymap import keys, cap_keys, cap_shift_keys, shift_keys
# Accepts the loop iterator (num) and the key symbol
# to be counted (key_sym). Counts the number of
# occurences of the specified key_sym.
def counter(ev, num, key_sym):
if ev[num] != key_sym:
return 0
return 2 + counter(ev, num-1, key_sym)
# Accepts the count (c) and loop iterator (num),
# then deletes that number of items beginning with
# the list index determined by the iterator.
def del_count(ev, num, c):
if 0 == c:
return num
del ev[num]
del_count(ev, num-1, c-1)
def define_key(ev, num, km):
# For cases when a letter is depressed.
if 2 == ev[num][1]:
ev.insert(num+1, km[ev[num][0]])
ev[num] = km[ev[num][0]]
else:
ev[num] = km[ev[num][0]]
def which_keymap(ev, i, shift, caps):
if shift and caps:
define_key(ev, i, cap_shift_keys)
elif shift:
define_key(ev, i, shift_keys)
elif caps:
define_key(ev, i, cap_keys)
else:
define_key(ev, i, keys)
if '[ENTER]' == ev[i]:
ev[i] = ' [ENTER]\n'
def backspace(ev):
i = len(ev)-1
while i >= 0:
if '[BACKSPACE]' == ev[i]:
c = counter(ev, i, '[BACKSPACE]')
del_count(ev, i, c)
i -= c
i -= 1
def translate(f):
evlst = []
caps = False
shift = False
evlog = f.split('-')
# Splits the received keylog into a list.
for ev in evlog:
if len(ev) > 5:
evlst.append(ev)
continue
ev = ev.split(',')
try:
ev[0] = int(ev[0])
ev[1] = int(ev[1])
evlst.append(ev)
except:
continue
# Translates the keylog list (evlst).
for n in reversed(xrange(len(evlst))):
# Skips translating the date.
if len(evlst[n]) > 5:
continue
if '[SHIFT]' == keys[evlst[n][0]] and 0 == evlst[n][1]:
shift = True
del evlst[n]
continue
elif '[SHIFT]' == keys[evlst[n][0]] and 1 == evlst[n][1]:
shift = False
del evlst[n]
continue
# Only grabs key release.
if 1 != evlst[n][1]:
del evlst[n]
continue
if '[CAPSLOCK]' == keys[evlst[n][0]] and False == caps:
caps = True
del evlst[n]
continue
elif '[CAPSLOCK]' == keys[evlst[n][0]] and True == caps:
caps = False
del evlst[n]
continue
# Determines which keymap to use for translation.
which_keymap(evlst, n, shift, caps)
backspace(evlst)
return "".join(evlst)