From 2dfba40e8e5f9c23198848e3d215474dfa557e41 Mon Sep 17 00:00:00 2001 From: Eric Werner Date: Thu, 25 Apr 2024 01:51:07 +0200 Subject: [PATCH] hotkey assembly repaired for ahk2 #266 --- ui/a2runtime.py | 22 ++++++++++------------ ui/a2widget/a2hotkey/edit_func_widget.py | 4 ++-- ui/a2widget/a2hotkey/hotkey_common.py | 11 ++++++----- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/ui/a2runtime.py b/ui/a2runtime.py index 02af193a..1452c494 100644 --- a/ui/a2runtime.py +++ b/ui/a2runtime.py @@ -1,6 +1,7 @@ """ Functionality all around managing the Autohotkey runtime of a2. """ + import os import enum import subprocess @@ -273,32 +274,29 @@ def gather(self, mod): for key in iter_str_or_list(hotkeys): if not key: continue - self._scope_types[scope_type][scope_string].setdefault(key, []).append( - (command, mod) - ) + self._scope_types[scope_type][scope_string].setdefault(key, []).append((command, mod)) def get_content(self): - scope_modes = {Scope.incl: '#IfWinActive', Scope.excl: '#IfWinNotActive'} + scope_prefix = '#HotIf' + scope_modes = { + Scope.incl: f'\n{scope_prefix} WinActive', + Scope.excl: f'\n{scope_prefix} !WinActive' + } # write global hotkey text - content = scope_modes[Scope.incl] + ',\n' + content = f'{scope_prefix}\n' content += self._create_hotkey_code(self.hotkeys_global) # write the scoped stuff for mode, scope_dict in self._scope_types.items(): for scope, hotkey_data in scope_dict.items(): - content += '\n%s, %s\n' % (scope_modes[mode], scope) + content += f'{scope_modes[mode]}("{scope}")\n' content += self._create_hotkey_code(hotkey_data) return content @property def has_content(self): - return any( - [ - d != {} - for d in [self.hotkeys_global, self.hotkeys_scope_incl, self.hotkeys_scope_excl] - ] - ) + return any([d != {} for d in [self.hotkeys_global, self.hotkeys_scope_incl, self.hotkeys_scope_excl]]) @staticmethod def _create_hotkey_code(hotkey_data): diff --git a/ui/a2widget/a2hotkey/edit_func_widget.py b/ui/a2widget/a2hotkey/edit_func_widget.py index 1c6219b4..44f5db6f 100644 --- a/ui/a2widget/a2hotkey/edit_func_widget.py +++ b/ui/a2widget/a2hotkey/edit_func_widget.py @@ -123,13 +123,13 @@ def on_send_mode_change(self): def on_input(self, code): current = self.ui.cfg_functionMode.currentText() if current == FuncTypes.open: - code = 'Run, ' + code + code = f'Run "{code}"' elif current == FuncTypes.send: # If `code` is empty we SHOULD strip the whole thing but we also # detect the Send mode from it :/ This has to happen somewhere else! send_mode = self.ui.function_send_mode.currentText() - code = '%s, %s' % (send_mode, code) + code = f'{send_mode} "{code}"' self._config_dict[FUNCTIONS[current]] = code self.changed.emit() diff --git a/ui/a2widget/a2hotkey/hotkey_common.py b/ui/a2widget/a2hotkey/hotkey_common.py index 78448cf4..f6308e56 100644 --- a/ui/a2widget/a2hotkey/hotkey_common.py +++ b/ui/a2widget/a2hotkey/hotkey_common.py @@ -2,9 +2,6 @@ Common hotkey things. """ -import unittest - - SEND_MODES = ('Send', 'SendRaw', 'SendInput', 'SendPlay', 'SendEvent') MOD_KEYS = ('! - Alt', '^ - Control', '+ - Shift', '# - Win') DISPLAY_MODIFIERS = { @@ -153,12 +150,16 @@ def strip_mode(code, modes): Return stripped code and found mode in tuple.""" _code = code.lower() for mode in modes: - if _code.startswith(f'{mode.lower()},'): - return code[len(mode) :].lstrip(' ,'), mode + _mode = mode.lower() + if _code.startswith(f'{_mode} '): + return code[len(mode) :].strip('" '), mode + if _code.startswith(f'{_mode},'): + return code[len(mode) + 1:].strip('" '), mode return code, modes[0] if __name__ == '__main__': + import unittest from a2widget.a2hotkey.test import test_common unittest.main(test_common, verbosity=2)