forked from dwks/aenea-grammar-simple
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
keyboard.py
377 lines (343 loc) · 11 KB
/
keyboard.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# Low-level keyboard input module
#
# Based on the work done by the creators of the Dictation Toolbox
# https://github.com/dictation-toolbox/dragonfly-scripts
#
# and _multiedit-en.py found at:
# http://dragonfly-modules.googlecode.com/svn/trunk/command-modules/documentation/mod-_multiedit.html
#
# Modifications by: Tony Grosinger
#
# Licensed under LGPL
try:
from aenea import *
except:
from dragonfly import *
try:
from dragonfly.actions.keyboard import keyboard
from dragonfly.actions.typeables import typeables
if 'semicolon' not in typeables:
typeables["semicolon"] = keyboard.get_typeable(char=';')
except:
pass
from words import handle_word
release = Key("shift:up, ctrl:up, alt:up")
def cancel_and_sleep(text=None, text2=None):
"""Used to cancel an ongoing dictation and puts microphone to sleep.
This method notifies the user that the dictation was in fact canceled,
a message in the Natlink feedback window.
Then the the microphone is put to sleep.
Example:
"'random mumbling go to sleep'" => Microphone sleep.
"""
try:
from natlink import setMicState
setMicState("sleeping")
print("* Dictation canceled. Going to sleep. *")
except:
pass
# For repeating of characters.
specialCharMap = {
"(bar|vertical bar|pipe)": "|",
"(dash|minus|hyphen)": "-",
"dit": ".",
"comma": ",",
"backslash": "\\",
"underscore": "_",
"(star|asterisk)": "*",
"colon": ":",
"(semicolon|semi colon)": ";",
#"at": "@",
"location": "@",
"[double] quote": '"',
"single quote": "'",
"hash": "#",
"dollar": "$",
"percent": "%",
"ampersand": "&",
"slash": "/",
"equal": "=",
"plus": "+",
"space": " ",
"bang": "!",
"question": "?",
"caret": "^",
# some other symbols I haven't imported yet, lazy sorry
# 'ampersand': Key('ampersand'),
# 'apostrophe': Key('apostrophe'),
# 'asterisk': Key('asterisk'),
# 'at': Key('at'),
# 'backslash': Key('backslash'),
# 'backtick': Key('backtick'),
# 'bar': Key('bar'),
# 'caret': Key('caret'),
# 'colon': Key('colon'),
# 'comma': Key('comma'),
# 'dollar': Key('dollar'),
# #'(dot|period)': Key('dot'),
# 'double quote': Key('dquote'),
# 'equal': Key('equal'),
# 'bang': Key('exclamation'),
# 'hash': Key('hash'),
# 'hyphen': Key('hyphen'),
# 'minus': Key('minus'),
# 'percent': Key('percent'),
# 'plus': Key('plus'),
# 'question': Key('question'),
# # Getting Invalid key name: 'semicolon'
# #'semicolon': Key('semicolon'),
# 'slash': Key('slash'),
# '[single] quote': Key('squote'),
# 'tilde': Key('tilde'),
# 'underscore | score': Key('underscore'),
}
# Modifiers for the press-command.
modifierMap = {
"alt": "a",
"angry": "a",
"control": "c",
"shift": "s",
"super": "w",
}
# Modifiers for the press-command, if only the modifier is pressed.
singleModifierMap = {
"alt": "alt",
"angry": "alt",
"control": "ctrl",
"shift": "shift",
"super": "win",
}
letterMap = {
"(alpha|arch)": "a",
"(bravo|brav) ": "b",
"(charlie|turley|char) ": "c",
"(delta|del) ": "d",
"(echo|every) ": "e",
"(foxtrot|fox) ": "f",
"(golf|gang) ": "g",
"(hotel) ": "h",
"(india|indigo) ": "i",
"(juliet|julia) ": "j",
"(kilo) ": "k",
"(lima|line) ": "l",
"(mike) ": "m",
"(november|noy) ": "n",
"(Oscar|osh) ": "o",
"(papa|poppa) ": "p",
"(quebec|queen) ": "q",
"(romeo) ": "r",
"(sierra) ": "s",
"(tango|tarnish) ": "t",
"(uniform) ": "u",
"(victor) ": "v",
"(whiskey) ": "w",
"(x-ray) ": "x",
"(yankee) ": "y",
"(zulu|zipper) ": "z",
}
#letterMap = {
#"(alpha|arch)": "a",
#"(bravo|brav) ": "b",
#"(charlie|turley|char) ": "c",
#"(delta|del) ": "d",
#"(echo|eck) ": "e",
#"(foxtrot|fox) ": "f",
#"(golf|gang) ": "g",
#"(hotel) ": "h",
#"(india|indigo|ish) ": "i",
#"(juliet|julia) ": "j",
#"(kilo) ": "k",
#"(lima|lion|line|lie) ": "l",
#"(mike) ": "m",
#"(november|noy) ": "n",
#"(Oscar|osh) ": "o",
#"(papa|poppa|pom) ": "p",
#"(quebec|quiche|queen) ": "q",
#"(romeo|ree) ": "r",
#"(sierra|soy) ": "s",
#"(tango|tay) ": "t",
#"(uniform|umm) ": "u",
#"(victor|van) ": "v",
#"(whiskey|wes) ": "w",
#"(x-ray) ": "x",
#"(yankee|yaa) ": "y",
#"(zulu) ": "z",
#}
# generate uppercase versions of every letter
upperLetterMap = {}
for letter in letterMap:
upperLetterMap["(upper|sky) " + letter] = letterMap[letter].upper()
letterMap.update(upperLetterMap)
numberMap = {
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
}
controlKeyMap = {
"left": "left",
"right": "right",
"up": "up",
"down": "down",
"page up": "pgup",
"page down": "pgdown",
"home": "home",
"end": "end",
"space": "space",
"(enter|return)": "enter",
"escape": "escape",
"tab": "tab",
"backspace": "backspace"
}
# F1 to F12. (do these actually work?)
functionKeyMap = {
'F one': 'f1',
'F two': 'f2',
'F three': 'f3',
'F four': 'f4',
'F five': 'f5',
'F six': 'f6',
'F seven': 'f7',
'F eight': 'f8',
'F nine': 'f9',
'F ten': 'f10',
'F eleven': 'f11',
'F twelve': 'f12',
}
pressKeyMap = {}
pressKeyMap.update(letterMap)
pressKeyMap.update(numberMap)
pressKeyMap.update(controlKeyMap)
pressKeyMap.update(functionKeyMap)
grammarCfg = Config("multi edit")
grammarCfg.cmd = Section("Language section")
grammarCfg.cmd.map = Item(
{
# Navigation keys.
"up [<n>]": Key("up:%(n)d"),
"down [<n>]": Key("down:%(n)d"),
"left [<n>]": Key("left:%(n)d"),
"right [<n>]": Key("right:%(n)d"),
"page up [<n>]": Key("pgup:%(n)d"),
"page down [<n>]": Key("pgdown:%(n)d"),
#"up <n> (page|pages)": Key("pgup:%(n)d"),
#"down <n> (page|pages)": Key("pgdown:%(n)d"),
#"left <n> (word|words)": Key("c-left/3:%(n)d/10"),
#"right <n> (word|words)": Key("c-right/3:%(n)d/10"),
"home": Key("home"),
"end": Key("end"),
"doc home": Key("c-home/3"),
"doc end": Key("c-end/3"),
# Functional keys.
"space": release + Key("space"),
"space [<n>]": release + Key("space:%(n)d"),
"(enter|slap|slop) [<n>]": release + Key("enter:%(n)d"),
"tab [<n>]": Key("tab:%(n)d"),
###"delete [<n>]": Key("del/3:%(n)d"),
"delete [this] line": Key("home, s-end, del"), # @IgnorePep8
"backspace [<n>]": release + Key("backspace:%(n)d"),
"application key": release + Key("apps/3"),
"win key": release + Key("win/3"),
#"paste [that]": Function(paste_command),
#"copy [that]": Function(copy_command),
"cut [that]": release + Key("c-x/3"),
"select all": release + Key("c-a/3"),
"[(hold|press)] alt": Key("alt:down/3"),
"[(hold|press)] angry": Key("alt:down/3"),
"release alt": Key("alt:up"),
"[(hold|press)] shift": Key("shift:down/3"),
"release shift": Key("shift:up"),
"[(hold|press)] control": Key("ctrl:down/3"),
"release control": Key("ctrl:up"),
"release [all]": release,
"press key <pressKey>": Key("%(pressKey)s"),
# Closures.
#"angle brackets": Key("langle, rangle, left/3"),
#"[square] brackets": Key("lbracket, rbracket, left/3"),
#"[curly] braces": Key("lbrace, rbrace, left/3"),
#"(parens|parentheses)": Key("lparen, rparen, left/3"),
#"quotes": Key("dquote/3, dquote/3, left/3"),
#"backticks": Key("backtick:2, left"),
#"single quotes": Key("squote, squote, left/3"),
"squiggle": Text("~"),
"backtick": Key("backtick"),
# Shorthand multiple characters.
"double <char>": Text("%(char)s%(char)s"),
"triple <char>": Text("%(char)s%(char)s%(char)s"),
"double escape": Key("escape, escape"), # Exiting menus.
# Punctuation and separation characters, for quick editing.
"colon [<n>]": Key("colon/2:%(n)d"),
"(semicolon|semi colon) [<n>]": Key("semicolon/2:%(n)d"),
"comma [<n>]": Key("comma/2:%(n)d"),
#"(dot|period|dit|point)": Key("dot"), # cannot be followed by a repeat count
"(period|point)": Key("dot"), # cannot be followed by a repeat count
"(dash|hyphen|minus) [<n>]": Key("hyphen/2:%(n)d"),
"underscore [<n>]": Key("underscore/2:%(n)d"),
"<letters>": Text("%(letters)s"),
"<char>": Text("%(char)s"),
"<modifierSingle> <letters>": Key("%(modifierSingle)s:down") + Text("%(letters)s") + Key("%(modifierSingle)s:up"),
"<modifierSingle> <char>": Key("%(modifierSingle)s:down") + Text("%(char)s") + Key("%(modifierSingle)s:up"),
'langle [<n>]': Key('langle:%(n)d'),
'lace [<n>]': Key('lbrace:%(n)d'),
'(lack|lair) [<n>]': Key('lbracket:%(n)d'),
#'(laip|len) [<n>]': Key('lparen:%(n)d'),
'len [<n>]': Key('lparen:%(n)d'),
'rangle [<n>]': Key('rangle:%(n)d'),
'race [<n>]': Key('rbrace:%(n)d'),
'(rack|rare) [<n>]': Key('rbracket:%(n)d'),
#'(raip|ren|wren) [<n>]': Key('rparen:%(n)d'),
'(ren|wren) [<n>]': Key('rparen:%(n)d'),
"act [<n>]": Key("escape:%(n)d"),
"calm [<n>]": Key("comma:%(n)d"),
'tunnel': Key('space,bar,space'),
'care': Key('home'),
'(doll|dole)': Key('end'),
'chuck [<n>]': Key('del:%(n)d'),
'scratch [<n>]': Key('backspace:%(n)d'),
#"visual": Key("v"),
"visual line": Key("s-v"),
"visual block": Key("c-v"),
"doc save": Key("c-s"),
"arrow": Text("->"),
'gope [<n>]': Key('pgup:%(n)d'),
'drop [<n>]': Key('pgdown:%(n)d'),
'lope [<n>]': Key('c-left:%(n)d'),
'(yope|rope) [<n>]': Key('c-right:%(n)d'),
#'(hill scratch|hatch) [<n>]': Key('c-backspace:%(n)d'),
'hexadecimal': Text("0x"),
'suspend': Key('c-z'),
'word <text>': Function(handle_word),
'number <num>': Text("%(num)d"),
'change <text> to <text2>': Key("home, slash") + Text("%(text)s") + Key("enter, c, e") + Text("%(text2)s") + Key("escape"),
# Microphone sleep/cancel started dictation.
"[<text>] (go to sleep|cancel and sleep) [<text2>]": Function(cancel_and_sleep), # @IgnorePep8
},
namespace={
"Key": Key,
"Text": Text,
}
)
class KeystrokeRule(MappingRule):
exported = False
mapping = grammarCfg.cmd.map
extras = [
IntegerRef("n", 1, 100),
IntegerRef("num", 0, 1000000),
Dictation("text"),
Dictation("text2"),
Choice("char", specialCharMap),
Choice("letters", letterMap),
Choice("modifier1", modifierMap),
Choice("modifier2", modifierMap),
Choice("modifierSingle", singleModifierMap),
Choice("pressKey", pressKeyMap),
]
defaults = {
"n": 1,
}