keys.py (6290B)
1 from typing import Set 2 3 from talon import Module, Context, actions, app 4 import sys 5 6 default_alphabet = "air bat cap drum each fine gust harp sit jury crunch look made near odd pit quench red sun trap urge vest whale plex yank zip".split( 7 " " 8 ) 9 letters_string = "abcdefghijklmnopqrstuvwxyz" 10 11 default_digits = "zero one two three four five six seven eight nine".split(" ") 12 numbers = [str(i) for i in range(10)] 13 default_f_digits = "one two three four five six seven eight nine ten eleven twelve".split( 14 " " 15 ) 16 17 mod = Module() 18 mod.list("letter", desc="The spoken phonetic alphabet") 19 mod.list("symbol_key", desc="All symbols from the keyboard") 20 mod.list("arrow_key", desc="All arrow keys") 21 mod.list("number_key", desc="All number keys") 22 mod.list("modifier_key", desc="All modifier keys") 23 mod.list("function_key", desc="All function keys") 24 mod.list("special_key", desc="All special keys") 25 mod.list("punctuation", desc="words for inserting punctuation into text") 26 27 28 @mod.capture(rule="{self.modifier_key}+") 29 def modifiers(m) -> str: 30 "One or more modifier keys" 31 return "-".join(m.modifier_key_list) 32 33 34 @mod.capture(rule="{self.arrow_key}") 35 def arrow_key(m) -> str: 36 "One directional arrow key" 37 return m.arrow_key 38 39 40 @mod.capture(rule="<self.arrow_key>+") 41 def arrow_keys(m) -> str: 42 "One or more arrow keys separated by a space" 43 return str(m) 44 45 46 @mod.capture(rule="{self.number_key}") 47 def number_key(m) -> str: 48 "One number key" 49 return m.number_key 50 51 52 @mod.capture(rule="{self.letter}") 53 def letter(m) -> str: 54 "One letter key" 55 return m.letter 56 57 58 @mod.capture(rule="{self.special_key}") 59 def special_key(m) -> str: 60 "One special key" 61 return m.special_key 62 63 64 @mod.capture(rule="{self.symbol_key}") 65 def symbol_key(m) -> str: 66 "One symbol key" 67 return m.symbol_key 68 69 70 @mod.capture(rule="{self.function_key}") 71 def function_key(m) -> str: 72 "One function key" 73 return m.function_key 74 75 76 @mod.capture(rule="( <self.letter> | <self.number_key> | <self.symbol_key> )") 77 def any_alphanumeric_key(m) -> str: 78 "any alphanumeric key" 79 return str(m) 80 81 82 @mod.capture( 83 rule="( <self.letter> | <self.number_key> | <self.symbol_key> " 84 "| <self.arrow_key> | <self.function_key> | <self.special_key> )" 85 ) 86 def unmodified_key(m) -> str: 87 "A single key with no modifiers" 88 return str(m) 89 90 91 @mod.capture(rule="{self.modifier_key}* <self.unmodified_key>") 92 def key(m) -> str: 93 "A single key with optional modifiers" 94 try: 95 mods = m.modifier_key_list 96 except AttributeError: 97 mods = [] 98 return "-".join(mods + [m.unmodified_key]) 99 100 101 @mod.capture(rule="<self.key>+") 102 def keys(m) -> str: 103 "A sequence of one or more keys with optional modifiers" 104 return " ".join(m.key_list) 105 106 107 @mod.capture(rule="{self.letter}+") 108 def letters(m) -> str: 109 "Multiple letter keys" 110 return "".join(m.letter_list) 111 112 113 ctx = Context() 114 modifier_keys = { 115 # If you find 'alt' is often misrecognized, try using 'alter'. 116 "alt": "alt", #'alter': 'alt', 117 "control": "ctrl", #'troll': 'ctrl', 118 "shift": "shift", #'sky': 'shift', 119 "super": "super", 120 } 121 if app.platform == "mac": 122 modifier_keys["command"] = "cmd" 123 modifier_keys["option"] = "alt" 124 ctx.lists["self.modifier_key"] = modifier_keys 125 alphabet = dict(zip(default_alphabet, letters_string)) 126 ctx.lists["self.letter"] = alphabet 127 128 # `punctuation_words` is for words you want available BOTH in dictation and as 129 # key names in command mode. `symbol_key_words` is for key names that should be 130 # available in command mode, but NOT during dictation. 131 punctuation_words = { 132 # TODO: I'm not sure why we need these, I think it has something to do with 133 # Dragon. Possibly it has been fixed by later improvements to talon? -rntz 134 "`": "`", 135 ",": ",", # <== these things 136 "back tick": "`", 137 "comma": ",", 138 "period": ".", 139 "semicolon": ";", 140 "colon": ":", 141 "forward slash": "/", 142 "question mark": "?", 143 "exclamation mark": "!", 144 "exclamation point": "!", 145 "dollar sign": "$", 146 "asterisk": "*", 147 "hash sign": "#", 148 "number sign": "#", 149 "percent sign": "%", 150 "at sign": "@", 151 "and sign": "&", 152 "ampersand": "&", 153 } 154 symbol_key_words = { 155 "dot": ".", 156 "quote": "'", 157 "L square": "[", 158 "left square": "[", 159 "square": "[", 160 "R square": "]", 161 "right square": "]", 162 "slash": "/", 163 "backslash": "\\", 164 "minus": "-", 165 "dash": "-", 166 "equals": "=", 167 "plus": "+", 168 "tilde": "~", 169 "bang": "!", 170 "dollar": "$", 171 "down score": "_", 172 "under score": "_", 173 "paren": "(", 174 "L paren": "(", 175 "left paren": "(", 176 "R paren": ")", 177 "right paren": ")", 178 "brace": "{", 179 "left brace": "{", 180 "R brace": "}", 181 "right brace": "}", 182 "angle": "<", 183 "left angle": "<", 184 "less than": "<", 185 "rangle": ">", 186 "R angle": ">", 187 "right angle": ">", 188 "greater than": ">", 189 "star": "*", 190 "pound": "#", 191 "hash": "#", 192 "percent": "%", 193 "caret": "^", 194 "amper": "&", 195 "pipe": "|", 196 "dubquote": '"', 197 "double quote": '"', 198 } 199 200 # make punctuation words also included in {user.symbol_keys} 201 symbol_key_words.update(punctuation_words) 202 ctx.lists["self.punctuation"] = punctuation_words 203 ctx.lists["self.symbol_key"] = symbol_key_words 204 ctx.lists["self.number_key"] = dict(zip(default_digits, numbers)) 205 ctx.lists["self.arrow_key"] = { 206 "down": "down", 207 "left": "left", 208 "right": "right", 209 "up": "up", 210 } 211 212 simple_keys = [ 213 "end", 214 "enter", 215 "escape", 216 "home", 217 "insert", 218 "pagedown", 219 "pageup", 220 "space", 221 "tab", 222 ] 223 224 alternate_keys = { 225 "delete": "backspace", 226 "forward delete": "delete", 227 #'junk': 'backspace', 228 "page up": "pageup", 229 "page down": "pagedown", 230 } 231 # mac apparently doesn't have the menu key. 232 if app.platform in ("windows", "linux"): 233 alternate_keys["menu key"] = "menu" 234 alternate_keys["print screen"] = "printscr" 235 236 special_keys = {k: k for k in simple_keys} 237 special_keys.update(alternate_keys) 238 ctx.lists["self.special_key"] = special_keys 239 ctx.lists["self.function_key"] = { 240 f"F {default_f_digits[i]}": f"f{i + 1}" for i in range(12) 241 } 242 243 244 @mod.action_class 245 class Actions: 246 def get_alphabet() -> dict: 247 """Provides the alphabet dictionary""" 248 return alphabet 249