-
Notifications
You must be signed in to change notification settings - Fork 10
/
emily-modifiers.py
210 lines (182 loc) · 6.78 KB
/
emily-modifiers.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Emily's Modifier Dictionary
import re
# define your ender here
uniqueEnders = ["LTZ"]
LONGEST_KEY = 1
# fingerspelling dictionary entries for relevant theories
spelling = {
"A" : "a",
"PW" : "b",
"KR" : "c",
"TK" : "d",
"E" : "e",
"TP" : "f",
"TKPW" : "g",
"H" : "h",
"EU" : "i",
"AOEU" : "i", # magnum
"SKWR" : "j",
"SKWRAEU" : "j", # magnum
"K" : "k",
"HR" : "l",
"PH" : "m",
"TPH" : "n",
"O" : "o",
"P" : "p",
"KW" : "q",
"R" : "r",
"S" : "s",
"T" : "t",
"U" : "u",
"SR" : "v",
"W" : "w",
"KP" : "x",
"KWR" : "y",
"STKPW" : "z",
"STKPWHR" : "z", # magnum
}
# same as emily-symbols format, but modified for use on the left hand
symbols = {
"TR" : ["tab", "delete", "backspace", "escape"],
"KPWR" : ["up", "left", "right", "down"],
"KPWHR" : ["page_up", "home", "end", "page_down"],
"" : ["", "tab", "return", "space"],
# typable symbols
"HR" : ["exclam", "", "notsign", "exclamdown"],
"PH" : ["quotedbl", "", "", ""],
"TKHR" : ["numbersign", "registered", "copyright", ""],
"KPWH" : ["dollar", "euro", "yen", "sterling"],
"PWHR" : ["percent", "", "", ""],
"SKP" : ["ampersand", "", "", ""],
"H" : ["apostrophe", "", "", ""],
"TPH" : ["parenleft", "less", "bracketleft", "braceleft"],
"KWR" : ["parenright", "greater", "bracketright", "braceright"],
"T" : ["asterisk", "section", "", "multiply"],
"K" : ["plus", "paragraph", "", "plusminus"],
"W" : ["comma", "", "", ""],
"TP" : ["minus", "", "", ""],
"R" : ["period", "periodcentered", "", ""],
"WH" : ["slash", "", "", "division"],
"TK" : ["colon", "", "", ""],
"WR" : ["semicolon", "", "", ""],
"TKPW" : ["equal", "", "", ""],
"TPW" : ["question", "", "questiondown", ""],
"TKPWHR" : ["at", "", "", ""],
"PR" : ["backslash", "", "", ""],
"KPR" : ["asciicircum", "guillemotleft", "guillemotright", "degree"],
"KW" : ["underscore", "", "", "mu"],
"P" : ["grave", "", "", ""],
"PW" : ["bar", "", "", "brokenbar"],
"TPWR" : ["asciitilde", "", "", ""]
}
def lookup(chord):
# extract the chord for easy use
stroke = chord[0]
# quick tests to avoid regex if non-relevant stroke is sent
if len(chord) != 1:
raise KeyError
assert len(chord) <= LONGEST_KEY
# extract relevant parts of the stroke
firstMatch = re.fullmatch(r'([#STKPWHR]*)([AO]*)([*-]*)([EU]*)([FRPB]*)([LGTSDZ]*)', stroke)
# error out if there are no matches found
if firstMatch is None:
raise KeyError
# name the relevant extracted parts of the regex
(key, vowel1, seperator, vowel2, modifiers, ender) = firstMatch.groups()
# if the user doesn't specify a modifier, then error out as the dictionary has no use otherwise
if modifiers is None:
raise KeyError
# combine the relevant parts of the stroke into a nice name
pattern = key + vowel1 + vowel2
if ender not in uniqueEnders:
raise KeyError
if pattern == "":
# just modifiers pressed on their own, used a lot in windows apparently
character = ""
else:
# use * to distinguish symbol input from numerical or character input
if "*" in seperator:
# symbol input
# extract the part of the symbol input
secondMatch = re.fullmatch(r'([STKPWHR]*)([AO]*)([EU]*)', pattern)
# into variables
(pattern, variants, vowel2) = secondMatch.groups()
# if the pattern is not recognised, error out
if pattern not in symbols:
raise KeyError
# calculate the variant count
variant = 0
if 'A' in variants:
variant = variant + 1
if 'O' in variants:
variant = variant + 2
# get the entry
entry = symbols[pattern]
if type(entry) == list:
extract = entry[variant]
# error out if the entry isn't applicable
if extract == "":
raise KeyError
character = extract
else:
character = entry
else:
# numbers or letters
# extract relevant parts of the stroke
secondMatch = re.fullmatch(r'([STKPWHR]*)([AO]*)([-EU]*)', pattern)
(shape, number, vowel2) = secondMatch.groups()
# AO is unused in finger spelling, thus used to disginguish numerical input
if number == "AO" and vowel2 == "":
# left-hand bottom row counts in binary for numbers 0-9
count = 0
if "R" in shape:
count = count + 1
if "W" in shape:
count = count + 2
if "K" in shape:
count = count + 4
if "S" in shape:
count = count + 8
# if TP is being held as well, then user is inputting a Fx key - like alt+F4
function = False
if "T" in shape and "P" in shape:
function = True
# add the 'F' if F number
if function:
character = "F" + str(count)
if count > 12:
raise KeyError
else:
if count > 9:
raise KeyError
character = str(count)
else:
# finger spelling input
entry = shape + number + vowel2
# check for entry in dictionary
if entry not in spelling:
raise KeyError
character = spelling[entry]
# if there is no entry, pass the error
if character == "":
raise KeyError
# accumulate list of modifiers to be added to the character
# may need to reorder?
modKeys = modifiers
mods = []
if "R" in modKeys:
mods.append("shift")
if "F" in modKeys:
mods.append("control")
if "B" in modKeys:
mods.append("alt")
if "P" in modKeys:
mods.append("super")
# apply those modifiers
combo = character
for mod in mods:
combo = mod + "(" + combo + ")"
# package it up with the syntax
ret = "{#" + combo + "}"
# all done! :D
return ret