def __init__(self, key_binds): self.key_binds = key_binds self.buttons = {} for name in self.key_binds: self.buttons[name] = State() self.key_action_lists = {} for bind_name, bind_value in self.key_binds.items(): if isinstance(bind_value, tuple): for i in range(len(bind_value)): if not (bind_value[i] in self.key_action_lists): self.key_action_lists[bind_value[i]] = [] self.key_action_lists[bind_value[i]].append(bind_name) else: if not (bind_value in self.key_action_lists): self.key_action_lists[bind_value] = [] self.key_action_lists[bind_value].append(bind_name) self.key_is_pressed = {} for key_name in self.key_action_lists: self.key_is_pressed[key_name] = False self.parsed_binds = [] for bind_name, bind_value in self.key_binds.items(): if isinstance(bind_value, tuple): for i in range(len(bind_value)): self.parsed_binds.append( keyboard.parse_hotkey(bind_value[i])) else: self.parsed_binds.append(keyboard.parse_hotkey(bind_value)) self.bind_keys()
def _validate_hotkeys(self): errors = [] for name, hotkey in self.hotkeys.items(): try: keyboard.parse_hotkey(hotkey) except ValueError: errors.append( f'Invalid hotkey for "{name}". Cannot parse "{hotkey}"') return errors
def test_parse_hotkey_keys(self): self.assertEqual(keyboard.parse_hotkey('left shift + a'), (( (5, ), (1, ), ), )) self.assertEqual(keyboard.parse_hotkey('left shift+a'), (( (5, ), (1, ), ), ))
def change_press_text(self): """ check if hotkey in PressText is valid and change StyleSheet accordingly """ try: text = self.PressText.text() # get text if self.output_selection != 'String': keyboard.parse_hotkey(text) # fails if text is invalid self.text = text # if the text doesn't fail save it self.PressText.setStyleSheet( success_style_sheet) # set to non fail colorscheme except: self.PressText.setStyleSheet( error_style_sheet) # set to fail colorscheme
def test_parse_hotkey_example(self): alt_codes = keyboard.key_to_scan_codes('alt') shift_codes = keyboard.key_to_scan_codes('shift') a_codes = keyboard.key_to_scan_codes('a') b_codes = keyboard.key_to_scan_codes('b') c_codes = keyboard.key_to_scan_codes('c') self.assertEqual(keyboard.parse_hotkey("alt+shift+a, alt+b, c"), ((alt_codes, shift_codes, a_codes), (alt_codes, b_codes), (c_codes,)))
def keyboard_press(self, value: str): """ Generates a keypress press """ try: if "+" in value and len(value) > 1: keyboard.parse_hotkey(value) else: keyboard.key_to_scan_codes(value) except ValueError as e: if str(e).startswith("Unexpected key type"): return "notReadable" else: return "notMapped" if self.debug: self._list.append(f"print('Pressing {str(value)}')") self._list.append(f"keyboard.press_and_release('{value}')") return True
def test_parse_hotkey_example(self): alt_codes = keyboard.key_to_scan_codes('alt') shift_codes = keyboard.key_to_scan_codes('shift') a_codes = keyboard.key_to_scan_codes('a') b_codes = keyboard.key_to_scan_codes('b') c_codes = keyboard.key_to_scan_codes('c') self.assertEqual(keyboard.parse_hotkey("alt+shift+a, alt+b, c"), ((alt_codes, shift_codes, a_codes), (alt_codes, b_codes), (c_codes, )))
def test_parse_hotkey_steps(self): self.assertEqual(keyboard.parse_hotkey('a+b, b+c'), (((1, ), (2, )), ((2, ), (3, ))))
def test_parse_hotkey_simple_steps(self): self.assertEqual(keyboard.parse_hotkey('a,b'), (((1, ), ), ((2, ), ))) self.assertEqual(keyboard.parse_hotkey('a, b'), (((1, ), ), ((2, ), )))
def ReleaseKey(hexKeyCode): keyboard.release(keyboard.parse_hotkey(hexKeyCode))
def test_parse_hotkey_separators(self): self.assertEqual(keyboard.parse_hotkey('+'), keyboard.parse_hotkey('plus')) self.assertEqual(keyboard.parse_hotkey(','), keyboard.parse_hotkey('comma'))
def test_parse_hotkey_simple(self): self.assertEqual(keyboard.parse_hotkey('a'), (((1, ), ), )) self.assertEqual(keyboard.parse_hotkey('A'), (((1, -1), ), ))
def test_parse_hotkey_steps(self): self.assertEqual(keyboard.parse_hotkey('a+b, b+c'), (((1,),(2,)),((2,),(3,))))
def test_parse_hotkey_deep_list_scan_codes(self): result = keyboard.parse_hotkey('a') self.assertEqual(keyboard.parse_hotkey(result), (((1, ), ), ))
def unbind_keys(self): keyboard.unhook_all() script_toggle_key = keyboard.parse_hotkey( self.key_binds["toggle_script"]) keyboard.on_press_key(script_toggle_key, self._on_press_key, True) keyboard.on_release_key(script_toggle_key, self._on_release_key, True)
def PressKey(hexKeyCode): keyboard.press(keyboard.parse_hotkey(hexKeyCode))
def parse_hotkey(hotkey): keyboard.parse_hotkey(hotkey) return hotkey
def test_parse_hotkey_list_names(self): self.assertEqual(keyboard.parse_hotkey(['a', 'b', 'c']), (((1,), (2,), (3,)),))
def test_parse_hotkey_deep_list_scan_codes(self): result = keyboard.parse_hotkey('a') self.assertEqual(keyboard.parse_hotkey(result), (((1,),),))
def test_parse_hotkey_list_scan_codes(self): self.assertEqual(keyboard.parse_hotkey([1, 2, 3]), (((1,), (2,), (3,)),))
def test_parse_hotkey_keys(self): self.assertEqual(keyboard.parse_hotkey('left shift + a'), (((5,), (1,),),)) self.assertEqual(keyboard.parse_hotkey('left shift+a'), (((5,), (1,),),))
def test_parse_hotkey_list_scan_codes(self): self.assertEqual(keyboard.parse_hotkey([1, 2, 3]), (((1, ), (2, ), (3, )), ))
def test_parse_hotkey_simple(self): self.assertEqual(keyboard.parse_hotkey('a'), (((1,),),)) self.assertEqual(keyboard.parse_hotkey('A'), (((1,-1),),))
def test_parse_hotkey_list_names(self): self.assertEqual(keyboard.parse_hotkey(['a', 'b', 'c']), (((1, ), (2, ), (3, )), ))
def test_parse_hotkey_simple_steps(self): self.assertEqual(keyboard.parse_hotkey('a,b'), (((1,),),((2,),))) self.assertEqual(keyboard.parse_hotkey('a, b'), (((1,),),((2,),)))