diff --git a/command_handler.py b/command_handler.py index dbf11a3..a251da0 100644 --- a/command_handler.py +++ b/command_handler.py @@ -32,13 +32,10 @@ class CommandHandler: # Ищем команду for cmd, action in self.commands[language].items(): if cmd.lower() == command_text: - return action + # Возвращаем строку действия, а не код + return self.actions.get(action) return None - def get_action_code(self, action_name): - """Получение кода действия по имени""" - return self.actions.get(action_name) - def add_command(self, language, command_text, action_name): """Добавление новой команды""" if language not in self.commands: diff --git a/commands.json b/commands.json index bfb6f49..2a5c4ee 100644 --- a/commands.json +++ b/commands.json @@ -2,6 +2,7 @@ "commands": { "ru": { "удали слово": "backspace_word", + "удали слова": "backspace_word", "очисти всё": "clear_all", "новая строка": "new_line", "точка": "period", @@ -20,12 +21,12 @@ } }, "actions": { - "backspace_word": "backspace_word()", - "clear_all": "select_all_and_delete()", - "new_line": "press_key('enter')", - "period": "type_text('.')", - "comma": "type_text(',')", - "question_mark": "type_text('?')", - "exclamation_mark": "type_text('!')" + "backspace_word": "backspace_word", + "clear_all": "clear_all", + "new_line": "new_line", + "period": "type_text:.", + "comma": "type_text:,", + "question_mark": "type_text:?", + "exclamation_mark": "type_text:!" } } \ No newline at end of file diff --git a/main.py b/main.py index 2375cb4..778be8b 100644 --- a/main.py +++ b/main.py @@ -102,12 +102,10 @@ class VoiceTextInput: return # Проверяем, является ли текст командой - action = self.command_handler.get_command_action(text, self.language) - if action: - action_code = self.command_handler.get_action_code(action) - if action_code: - print(f"Выполнение команды: {text}") - self.text_input.execute_action(action_code) + action_string = self.command_handler.get_command_action(text, self.language) + if action_string: + print(f"Выполнение команды: {text}") + self.text_input.execute_action(action_string) return # Если это обычный текст, вводим его diff --git a/text_input.py b/text_input.py index 11c9e70..a19d5d5 100644 --- a/text_input.py +++ b/text_input.py @@ -5,7 +5,8 @@ import pyperclip class TextInput: def __init__(self): - pass + # Устанавливаем задержку между действиями + pyautogui.PAUSE = 0.01 def type_text(self, text): """Ввод текста в активное окно с поддержкой кириллицы""" @@ -46,45 +47,57 @@ class TextInput: # Вставляем текст с помощью горячих клавиш (Ctrl+V) pyautogui.hotkey('ctrl', 'v') - # Восстанавливаем оригинальное содержимое буфера обмена (опционально) - pyperclip.copy(original_clipboard) - except Exception as e: print(f"Ошибка при вводе через clipboard: {e}") - # Восстанавливаем буфер обмена в случае ошибки - try: - pyperclip.copy(original_clipboard) - except: - pass def press_key(self, key): """Нажатие клавиши""" - pyautogui.press(key) + try: + pyautogui.press(key) + except Exception as e: + print(f"Ошибка при нажатии клавиши {key}: {e}") def backspace_word(self): """Удаление последнего слова""" - # Выделяем последнее слово и удаляем его - pyautogui.hotkey('ctrl', 'backspace') + try: + pyautogui.hotkey('ctrl', 'backspace') + except Exception as e: + print(f"Ошибка при удалении слова: {e}") def select_all_and_delete(self): """Выделить всё и удалить""" - pyautogui.hotkey('ctrl', 'a') - pyautogui.press('backspace') + try: + pyautogui.hotkey('ctrl', 'a') + pyautogui.press('backspace') + except Exception as e: + print(f"Ошибка при очистке: {e}") - def execute_action(self, action_code): - """Выполнение действия по коду""" - actions = { - 'backspace_word': self.backspace_word, - 'clear_all': self.select_all_and_delete, - 'new_line': lambda: self.press_key('enter'), - 'period': lambda: self.type_text('.'), - 'comma': lambda: self.type_text(','), - 'question_mark': lambda: self.type_text('?'), - 'exclamation_mark': lambda: self.type_text('!') - } + def execute_action(self, action_string): + """Выполнение действия по строке действия""" + if not action_string: + return - if action_code in actions: - try: - actions[action_code]() - except Exception as e: - print(f"Ошибка при выполнении действия {action_code}: {e}") \ No newline at end of file + try: + # Разбираем строку действия + if ':' in action_string: + action_type, action_param = action_string.split(':', 1) + else: + action_type = action_string + action_param = None + + # Выполняем соответствующее действие + if action_type == "type_text" and action_param: + self.type_text(action_param) + elif action_type == "press_key" and action_param: + self.press_key(action_param) + elif action_type == "backspace_word": + self.backspace_word() + elif action_type == "clear_all": + self.select_all_and_delete() + elif action_type == "new_line": + self.press_key('enter') + else: + print(f"Неизвестное действие: {action_type}") + + except Exception as e: + print(f"Ошибка при выполнении действия {action_string}: {e}") \ No newline at end of file