-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
583 lines (489 loc) · 19 KB
/
commands.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import log
import time
import re
import json
import struct
import sys
import subprocess
from enum import Enum
import win32gui, win32con, win32api, win32com
import keyboard
import pyautogui
import psutil
from keyboardEvent import KeyboardEvent
from word2number import w2n
import window_properties
from window_properties import currentApp
from forwarder import encode_message, send_message
from globs import gui
from mode import *
import settings
try:
import voice
except ImportError as e:
log.error("FAILED TO IMPORT VOICE")
log.error(str(e))
pass # FIXME: ignore circular import
class KeyboardMessage():
"""Format for keyboard messages sent to our custom firefox extension"""
def __init__(self, key, repeat=False, shiftKey=False, ctrlKey=False, altKey=False, metaKey=False):
self.message = {
'key': key,
'repeat': repeat,
'shiftKey': shiftKey,
'ctrlKey': ctrlKey,
'altKey': altKey,
'metaKey': metaKey
}
def __json__(self):
return self.message
def exeAlt(tokens, mode):
if len(tokens) > 1 and tokens[0] == 'TAB':
KeyboardEvent.pressSequence(['ALT', 'TAB'])
else:
msg = "No parameter" if len(tokens) == 0 else tokens[0]
log.parse_error(log.ParseError.ALT, msg)
return (self.parseImpl(tokens[1:], mode))
def exeResize(tokens, mode):
screen_width = win32api.GetSystemMetrics(0)
screen_height = win32api.GetSystemMetrics(1)
half_width = screen_width // 2
half_height = screen_height // 2
fg_hwnd = win32gui.GetForegroundWindow()
def resize(x, y, w, h):
win32gui.MoveWindow(fg_hwnd, x, y, w, h, True)
if len(tokens) == 0:
log.parse_error(log.ParseError.RESIZE, "No parameter")
elif tokens[0] == 'LEFT':
resize(0, 0, half_width, screen_height)
elif tokens[0] == 'RIGHT':
resize(half_width + 1, 0, half_width, screen_height)
elif tokens[0] == 'UP':
resize(0, 0, screen_width, half_height)
elif tokens[0] == 'DOWN':
resize(0, half_height + 1, screen_width, half_height)
elif tokens[0] == 'FULL':
resize(0, 0, screen_width, screen_height)
else:
log.parse_error(log.ParseError.RESIZE, tokens[0])
return (tokens[1:], mode)
def exeHelp(tokens, mode):
exeFocus(['FIREFOX'], mode)
send_message(encode_message([KeyboardMessage('E', shiftKey=True)]))
# if len(tokens) == 0:
# if currentApp() == "Microsoft Word":
# helpType = "word" + mode.name
# elif currentApp() == "Firefox":
# helpType = "firefox" + mode.name
# gui.helpMode(helpType)
# return ([], GlobalMode.HELP)
# else:
# log.parse_error(log.ParseError.HELP, tokens[0])
def exeSettings(tokens, mode):
if len(tokens) == 0:
gui.settingsMode(settings.loadConfig()["MACROS"])
return ([], GlobalMode.SETTINGS)
elif tokens[0] == 'CALIBRATE':
voice.recalibrate()
#Need to fix circular dependency in order to do this
elif tokens[0] == 'TIMEOUT':
pass
voice.adjustTimeout(tokens[1:])
elif len(tokens) > 1 and tokens[0] == 'TIME' and tokens[1] == 'OUT':
pass
voice.adjustTimeout(tokens[2:])
elif tokens[0] == 'CLOSE':
gui.closeSettings()
elif tokens[0] == 'RESIZE':
gui.resizeWindow(tokens[1:])
else:
log.error(log.ParseError.HELP, tokens[0])
def exeLaunch(tokens, mode):
"""Launch application, or focus it if the app is already launched"""
if len(tokens) != 1:
gui.showError("Unrecognized\nApplication")
log.warn("unrecognized application for launch cmd: '{}'".format(' '.join(tokens)))
elif tokens[0] == 'FIREFOX':
# technically, this will always be open if speechv is running. Focus
# the application instead
exeFocus(['FIREFOX'], mode)
elif tokens[0] == 'WORD':
# check if it's already open
handles = window_properties.getMainWindowHandles(processNameOf('WORD'))
if handles:
exeFocus(['WORD'], mode)
else:
# launch it. To complete this project in a reasonable amount of time
# we hardcode it. Windows has spotty support for this type of stuff
subprocess.Popen([r'C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE'])
def exeSwitch(tokens, mode):
keyboard.press_and_release("alt+tab")
def exeSleep(tokens, mode):
if mode == GlobalMode.FOLLOW:
# exit follow mode in firefox or word
pyautogui.hotkey('escape')
mode = GlobalMode.SLEEPING
return ([], mode)
def exeFocus(tokens, mode):
# https://stackoverflow.com/questions/44735798/pywin32-how-to-get-window-handle-from-process-handle-and-vice-versa
log.debug("app to focus: '{}'".format(tokens[0]))
if len(tokens) != 1:
gui.showError("Incorrect\nusage of focus")
log.warn("focus used without exactly one token")
return ([], mode)
processName = processNameOf(tokens[0])
log.debug("processName: '{}'".format(processName))
handles = window_properties.getMainWindowHandles(processName, expect_one=True)
if not handles:
gui.showError("No app to focus")
return ([], mode)
# NOTE: we choose an arbitrary handle if there's more than one
handle = handles[0]
try:
win32gui.SetForegroundWindow(handle)
except Exception as e:
log.error(str(e))
try:
# SetForegroundWindow is unreliable
# workarounds: https://stackoverflow.com/questions/3772233/
win32gui.ShowWindow(handle, win32con.SW_MINIMIZE)
win32gui.ShowWindow(handle, win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(handle)
except Exception as e:
log.error(str(e))
log.error("couldn't focus app '{}'".format(tokens[0]))
gui.showError("Couldn't focus app")
return ([], mode)
# De-minimize window if necessary
if win32gui.IsIconic(handle):
win32gui.ShowWindow(handle, win32con.SW_SHOWNORMAL)
def processNameOf(app_name):
"""Translate the name a user says to the associated process name"""
# this list is intentionally non-comprehensive. Windows doesn't offer
# enough support to make complete coverage feasible
if app_name == 'WORD' or app_name == "Microsoft Word":
return 'WINWORD.EXE'
else:
return app_name.lower() + '.exe'
def exeTerminate(tokens, mode):
if currentApp() == 'Firefox':
gui.showError('Closing Firefox\nwould close SpeechV')
return ([], mode)
handles = window_properties.getMainWindowHandles(
processNameOf(currentApp()))
#try:
# target = handles[0] # choose arbitrary window to terminate if more than one
#except IndexError:
# log.error("No window to terminate for current app '{}'".format(currentApp()))
# return ([], mode)
if not handles:
log.error("No window to terminate for current app '{}'".format(currentApp()))
return ([], mode)
for handle in handles:
win32api.SendMessage(handle, win32con.WM_DESTROY, None, None)
def exeMaximize(tokens, mode):
if tokens:
gui.showError("Incorrect\nusage of maximize")
handle = win32gui.GetForegroundWindow()
win32gui.ShowWindow(handle, win32con.SW_MAXIMIZE)
def exeMinimize(tokens, mode):
if tokens:
gui.showError("Incorrect\nusage of minimize")
handle = win32gui.GetForegroundWindow()
win32gui.ShowWindow(handle, win32con.SW_MINIMIZE)
def exeCancel(tokens, mode):
"""Remove all follow pop-ups and leave follow mode"""
# NOTE: this could be extended to exit insert mode, etc.
send_message(encode_message([KeyboardMessage('Enter')]))
# pyautogui.hotkey('escape')
mode = GlobalMode.NAVIGATE
return ([], mode)
def exeCopy(tokens, mode):
keyboard.press_and_release("ctrl+c")
def exePaste(tokens, mode):
keyboard.press_and_release("ctrl+v")
def exeRecord(tokens, mode):
"""TODO:
Add ability to record macros.
Tokens could be "Start" or "End" with the macro commands
Sandwiched between "Record start" and "record end"
"""
pass
def exeKeystroke(tokens, mode):
if len(tokens) == 1:
try:
keyboard.press_and_release(tokens[0])
except ValueError as e:
log.error(str(e))
log.error("bad keystroke '{}'".format(tokens[0]))
gui.showError('Invalid usage')
else:
log.Logger.log(log.ParseError.TYPE, tokens)
def exeSearch(tokens):
"""Searchs text in the address bar
Tokens should be the search term.
This effectively accomplishes:
ctrl+k (go to search bar),
entering tokens as text,
enter (open in current tab),
"""
keyboard.press_and_release('ctrl+k')
time.sleep(1)
keyboard.write(' '.join(tokens).capitalize())
time.sleep(0.25)
time.sleep(1)
keyboard.press_and_release('enter')
def exeFind(tokens):
"""Finds text on the page."""
if len(tokens) > 0 and tokens[0] == "PHRASE":
send_message(encode_message([KeyboardMessage('/')]))
time.sleep(0.5)
keyboard.write(' '.join(tokens[1:]))
time.sleep(0.25)
keyboard.press_and_release('enter')
else:
gui.showError("Unrecognized\nFind sequence")
time.sleep(1)
def exeMove(tokens, mode):
gui.enter()
browserKeywords = {
'LEFT' : [KeyboardMessage('h')],
'RIGHT' : [KeyboardMessage('l')],
'UP' : [KeyboardMessage('u', ctrlKey=True)],
'DOWN' : [KeyboardMessage('d', ctrlKey=True)],
'PAGE UP' : [KeyboardMessage('b', ctrlKey=True)],
'PAGE DOWN' : [KeyboardMessage('f', ctrlKey=True)],
'LEFTMOST' : [KeyboardMessage('0')],
'RIGHTMOST' : [KeyboardMessage('$')],
'TOP' : [KeyboardMessage('g'), KeyboardMessage('g')],
'BOTTOM' : [KeyboardMessage('G', shiftKey=True)],
'CLOSE TAB' : [KeyboardMessage('d')],
'REOPEN TAB' : [KeyboardMessage('u')],
'TAB LEFT' : [KeyboardMessage('K', shiftKey=True)],
'TAB RIGHT' : [KeyboardMessage('J', shiftKey=True)],
'REFRESH' : [KeyboardMessage('r')],
'FOLLOW' : [KeyboardMessage('f')],
'OPEN' : [KeyboardMessage('F', shiftKey=True)],
'BACK' : [KeyboardMessage('H', shiftKey=True)],
'FORWARD' : [KeyboardMessage('L', shiftKey=True)],
'ZOOM IN' : [KeyboardMessage('z'), KeyboardMessage('i')],
'ZOOM OUT' : [KeyboardMessage('z'), KeyboardMessage('o')],
'ZOOM DEFAULT' : [KeyboardMessage('z'), KeyboardMessage('z')],
'SAVE BOOKMARK' : [KeyboardMessage('D', shiftKey=True)],
'SHOW BOOKMARKS' : [KeyboardMessage('A', shiftKey=True)],
'SEARCH' : [KeyboardMessage('k', ctrlKey=True)],
# 'FIND PHRASE' is slightly complicated so has it's own function next to search
'FIND NEXT' : [KeyboardMessage('n')],
'FIND PREVIOUS' : [KeyboardMessage('N', shiftKey=True)],
'NEW TAB' : [KeyboardMessage('z'), KeyboardMessage('d')],
}
def forwardBrowser(tokens, mode):
tokenStr = ' '.join(tokens)
if tokenStr in browserKeywords:
# Hacky interception of next few chars to send with follow
if tokenStr == 'FOLLOW' or tokenStr == 'OPEN':
mode = GlobalMode.FOLLOW
send_message(encode_message(browserKeywords[tokenStr]))
elif tokens[0] == 'SEARCH':
exeSearch(tokens[1:])
elif tokens[0] == 'FIND':
exeFind(tokens[1:])
elif len(tokens) > 1 and tokens[0] == "NEW" and tokens[1] == "TAB":
keyboard.press_and_release("ctrl+t")
else:
log.parse_error(log.ParseError.BROWSER, tokenStr)
return ([], mode)
wordCmds = {
"NAVIGATE" : {
'UP' : 'up',
'DOWN' : 'down',
'PARAGRAPH UP': 'ctrl+up',
'PARAGRAPH DOWN': 'ctrl+down',
'PAGE UP': 'ctrl+page up',
'PAGE DOWN': 'ctrl+page down',
'LEFT' : 'ctrl+left',
'RIGHT' : 'ctrl+right',
'SPACE': 'space',
'PERIOD': '.',
'COMMA': ',',
'EXCLAMATION': 'shift+!',
'QUESTION': 'shift+/',
'SLASH': '/',
'COLON': 'shift+:',
'SEMICOLON': ';',
'APOSTROPHE': '\'',
'QUOTE': 'shift+\"',
'OPEN PARENTHESIS': 'shift+(',
'CLOSE PARENTHESIS': 'shift+)',
'AMPERSAND': 'shift+&',
'DOLLAR': 'shift+$',
'STAR': 'shift+*',
'LEFT ALIGN': 'ctrl+l',
'CENTER ALIGN': 'ctrl+e',
'RIGHT ALIGN': 'ctrl+r',
'UNDO': 'ctrl+z',
'RE DO': 'ctrl+y',
'INDENT': 'tab',
'REMOVE INDENT': 'shift+tab',
'NEW LINE': 'enter',
'NEWLINE': 'enter',
'CUT': 'ctrl+x',
'COPY': 'ctrl+c',
'PASTE': 'ctrl+v',
'BOLD': 'ctrl+b',
'ITALICS': 'ctrl+i',
'UNDERLINE': 'ctrl+u',
'DELETE': 'backspace',
'INCREASE SIZE': 'ctrl+]',
'DECREASE SIZE': 'ctrl+[',
'CAPS': 'shift+F3', # rotates between 'this', 'This' and 'THIS'
'UNHIGHLIGHT': 'right',
},
"HIGHLIGHT" : {
'DOWN': 'shift+down',
'UP': 'shift+up',
'RIGHT': 'ctrl+shift+right',
'LEFT': 'ctrl+shift+left',
'ALL': 'ctrl+a',
}
}
class WordForwarder:
def __init__(self):
self.mode = WordMode.NAVIGATE
self.followLayers = 0
def forward(self, tokens, globalMode):
log.debug("In word, current mode: " + self.mode.name)
log.debug("Received tokens: " + ' '.join(tokens))
tokenStr = ' '.join(tokens)
if tokenStr == 'FOLLOW':
log.debug("Entered if 1")
pyautogui.press('alt')
self.followLayers = self.followLayers + 1
mode = GlobalMode.FOLLOW
return ([], mode)
if tokenStr in WordMode.__members__:
log.debug("Entered if 2")
self.mode = WordMode[tokenStr]
return ([], globalMode)
# FIXME: I think insert mode can be handled in one central location, which
# is in the Parser at the moment. Feel free to add a word-specific
# INSERT mode back if it's infeasible/dumb to do.
#
# *** In fact, if the parser's mode is NORMAL then it may
# intercept words that are being said even if word's mode is INSERT
#if self.wordMode == "INSERT":
# keyboard.write(tokenStr.lower())
# return ([], globalMode)
if tokenStr in wordCmds[self.mode.name]:
log.debug("Entered if 3")
keyboard.press_and_release(wordCmds[self.mode.name][tokenStr])
return ([], globalMode)
#This is because we support "down X" where x is arbitrary
elif tokens[0] in ["UP", "DOWN", "LEFT", "RIGHT"]:
log.debug("Entered if 4")
try:
num = w2n.word_to_num(' '.join(tokens[1:]))
except Exception as e:
log.error(str(e))
return
for i in range(num):
keyboard.press_and_release(wordCmds[self.mode.name][tokens[0]])
time.sleep(.1)
elif self.mode.name=="HIGHLIGHT" and tokenStr in wordCmds["NAVIGATE"]:
keyboard.press_and_release(wordCmds["NAVIGATE"][tokenStr])
self.mode = WordMode.NAVIGATE
return ([], globalMode)
log.debug("Done!")
def followWord(self, tokens):
if tokens[0] == 'CANCEL' and len(tokens) == 1:
for i in range(self.followLayers + 1):
pyautogui.hotkey('escape')
self.followLayers = 0
return ([], GlobalMode.NAVIGATE)
elif tokens[0] == 'INSERT' and len(tokens) == 1:
for i in range(self.followLayers + 1):
pyautogui.hotkey('escape')
self.followLayers = 0
return ([], GlobalMode.INSERT)
elif tokens[0] == 'BACK' and len(tokens) == 1:
pyautogui.hotkey('escape')
self.followLayers = self.followLayers - 1
if self.followLayers == 0:
return ([], GlobalMode.NAVIGATE)
return ([], GlobalMode.FOLLOW)
elif tokens[0] in ['UP','DOWN','LEFT','RIGHT','ENTER'] and len(tokens) == 1:
key = tokens[0].lower()
pyautogui.hotkey(key)
return ([], GlobalMode.FOLLOW)
# if user gives NAVIGATE command, get out of follow and execute command
tokenStr = ' '.join(tokens)
if tokenStr in wordCmds['NAVIGATE']:
for i in range(self.followLayers + 1):
pyautogui.hotkey('escape')
self.followLayers = 0
return self.forward(tokens, GlobalMode.NAVIGATE)
if tokenStr in WordMode.__members__:
self.mode = WordMode[tokenStr]
return ([], GlobalMode.NAVIGATE)
# handle follow tokens
newlist = []
for token in tokens:
if token in ['ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', 'ZERO']:
newlist.append(str(w2n.word_to_num(token)))
elif len(token) != 1:
log.warn("cannot handle follow token size greater than 1")
return
else:
newlist.append(token)
# send keystrokes to word
self.followLayers = self.followLayers + 1
command = ''.join(newlist)
pyautogui.typewrite(command)
return ([], GlobalMode.FOLLOW)
def deleteMacro(tokens):
log.info("In deleteMacro. Tokens: " + ' '.join(tokens))
config = settings.loadConfig()
if ' '.join(tokens) in config["MACROS"]:
del config["MACROS"][' '.join(tokens)]
settings.saveConfig(config)
#Reload the menu to reload the macros
gui.settingsMode(settings.loadConfig()["MACROS"])
def forwardSettings(tokens):
log.info("In forwardSettings. Tokens: " + ' '.join(tokens))
if len(tokens) == 0:
return ([], GlobalMode.SETTINGS)
if ' '.join(tokens[:2]) == "MACRO DELETE":
deleteMacro(tokens[2:])
return
elif ' '.join(tokens[:3]) == "VOICE TIME OUT":
voice.adjustTimeout(tokens[3:])
return
elif ' '.join(tokens[:2]) == "VOICE TIMEOUT":
voice.adjustTimeout(tokens[2:])
return
elif tokens[0] == "RESIZE":
gui.resizeWindow(tokens[1:])
return
elif tokens[0] == "CLOSE":
gui.closeSettings()
return ([], GlobalMode.NAVIGATE)
log.debug("No match in forwardSettings!")
return ([], GlobalMode.SETTINGS)
# def forwardHelp(tokens):
# if len(tokens) == 0:
# return ([], GlobalMode.HELP)
# if tokens[0] == "CLOSE":
# gui.closeHelpMenu()
# return ([], GlobalMode.NAVIGATE)
# log.debug("No match in forwardHelp!")
# return ([], GlobalMode.HELP)
'''================
SpeechV Settings
================
Select a setting
"MACRO DELETE <macro>": Delete the given macro
Macros:
"VOICE TIME OUT <seconds>": Adjust timeout for speech recognition.
Seconds in tenths of a second e.g. "one point zero"
"RESIZE <size>": Resize the SpeechV GUI. Default is 100.
"CLOSE"'''