Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
 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, ),
     ), ))
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 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,)))
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
 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, )))
Ejemplo n.º 8
0
 def test_parse_hotkey_steps(self):
     self.assertEqual(keyboard.parse_hotkey('a+b, b+c'),
                      (((1, ), (2, )), ((2, ), (3, ))))
Ejemplo n.º 9
0
 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, ), )))
Ejemplo n.º 10
0
def ReleaseKey(hexKeyCode):
    keyboard.release(keyboard.parse_hotkey(hexKeyCode))
Ejemplo n.º 11
0
 def test_parse_hotkey_separators(self):
     self.assertEqual(keyboard.parse_hotkey('+'),
                      keyboard.parse_hotkey('plus'))
     self.assertEqual(keyboard.parse_hotkey(','),
                      keyboard.parse_hotkey('comma'))
Ejemplo n.º 12
0
 def test_parse_hotkey_simple(self):
     self.assertEqual(keyboard.parse_hotkey('a'), (((1, ), ), ))
     self.assertEqual(keyboard.parse_hotkey('A'), (((1, -1), ), ))
Ejemplo n.º 13
0
 def test_parse_hotkey_steps(self):
     self.assertEqual(keyboard.parse_hotkey('a+b, b+c'), (((1,),(2,)),((2,),(3,))))
Ejemplo n.º 14
0
 def test_parse_hotkey_deep_list_scan_codes(self):
     result = keyboard.parse_hotkey('a')
     self.assertEqual(keyboard.parse_hotkey(result), (((1, ), ), ))
Ejemplo n.º 15
0
 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)
Ejemplo n.º 16
0
def PressKey(hexKeyCode):
    keyboard.press(keyboard.parse_hotkey(hexKeyCode))
Ejemplo n.º 17
0
def parse_hotkey(hotkey):
    keyboard.parse_hotkey(hotkey)
    return hotkey
Ejemplo n.º 18
0
 def test_parse_hotkey_list_names(self):
     self.assertEqual(keyboard.parse_hotkey(['a', 'b', 'c']), (((1,), (2,), (3,)),))
Ejemplo n.º 19
0
 def test_parse_hotkey_deep_list_scan_codes(self):
     result = keyboard.parse_hotkey('a')
     self.assertEqual(keyboard.parse_hotkey(result), (((1,),),))
Ejemplo n.º 20
0
 def test_parse_hotkey_list_scan_codes(self):
     self.assertEqual(keyboard.parse_hotkey([1, 2, 3]), (((1,), (2,), (3,)),))
Ejemplo n.º 21
0
 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,),),))
Ejemplo n.º 22
0
 def test_parse_hotkey_separators(self):
     self.assertEqual(keyboard.parse_hotkey('+'), keyboard.parse_hotkey('plus'))
     self.assertEqual(keyboard.parse_hotkey(','), keyboard.parse_hotkey('comma'))
Ejemplo n.º 23
0
 def test_parse_hotkey_list_scan_codes(self):
     self.assertEqual(keyboard.parse_hotkey([1, 2, 3]),
                      (((1, ), (2, ), (3, )), ))
Ejemplo n.º 24
0
 def test_parse_hotkey_simple(self):
     self.assertEqual(keyboard.parse_hotkey('a'), (((1,),),))
     self.assertEqual(keyboard.parse_hotkey('A'), (((1,-1),),))
Ejemplo n.º 25
0
 def test_parse_hotkey_list_names(self):
     self.assertEqual(keyboard.parse_hotkey(['a', 'b', 'c']),
                      (((1, ), (2, ), (3, )), ))
Ejemplo n.º 26
0
 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,),)))