From fd008214ee9e6ac5346d12c3bdfb22c689472859 Mon Sep 17 00:00:00 2001 From: f1andrew Date: Mon, 18 Jul 2022 10:15:15 +0300 Subject: [PATCH] changed importing of cli_keymap --- cli_keymap.py | 33 +++++++++++++++++++++++++++++++++ relaykeys-cli.py | 30 ++---------------------------- relaykeys-qt.py | 9 +++++---- 3 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 cli_keymap.py diff --git a/cli_keymap.py b/cli_keymap.py new file mode 100644 index 0000000..41d7503 --- /dev/null +++ b/cli_keymap.py @@ -0,0 +1,33 @@ +from pathlib import Path +import json + +nonchars_key_map = None + +def load_keymap_file(config): + global nonchars_key_map + + keymap_file = Path(__file__).resolve().parent / "cli_keymaps" / config.get("keymap_file", "us_keymap.json") + if keymap_file.exists() and keymap_file.is_file(): + with open(keymap_file, "r") as f: + try: + nonchars_key_map = json.loads(f.read()) + except Exception as e: + print("Invalid keymap json file:", e) + return + else: + print("Invalid path to keymap file:", keymap_file) + return False + + return True + +def char_to_keyevent_params (char): + global nonchars_key_map + + ret = nonchars_key_map.get(char, None) + if ret is not None: + return ret + if (char.isdigit()): + return (char,[]) + if (char.upper() == char): + return (char.upper(),["LSHIFT"]) + return (char.upper(),[]) \ No newline at end of file diff --git a/relaykeys-cli.py b/relaykeys-cli.py index 60cf56b..122f0ee 100644 --- a/relaykeys-cli.py +++ b/relaykeys-cli.py @@ -17,6 +17,8 @@ from notifypy import Notify +from cli_keymap import * + parser = argparse.ArgumentParser(description='Relay keys daemon, BLEHID controller.') parser.add_argument('--debug', dest='debug', action='store_const', const=True, default=False, @@ -34,24 +36,6 @@ parser.add_argument('commands', metavar='COMMAND', nargs='*', help='One or more commands, format: :') -nonchars_key_map = None - -def load_keymap_file(config): - global nonchars_key_map - keymap_file = Path(__file__).resolve().parent / "cli_keymaps" / config.get("keymap_file", "us_keymap.json") - if keymap_file.exists() and keymap_file.is_file(): - with open(keymap_file, "r") as f: - try: - nonchars_key_map = json.loads(f.read()) - except Exception as e: - print("Invalid keymap json file:", e) - return - else: - print("Invalid path to keymap file:", keymap_file) - return False - - return True - def parse_macro(file_arg): # if argument is file name without any path check it in macros folder if file_arg.find('/') == -1 and file_arg.find('/') == -1: @@ -194,16 +178,6 @@ def do_daemoncommand(client, command, notify=False): if notify: send_notification("daemon command", command, ret["result"]) -def char_to_keyevent_params (char): - ret = nonchars_key_map.get(char, None) - if ret is not None: - return ret - if (char.isdigit()): - return (char,[]) - if (char.upper() == char): - return (char.upper(),["LSHIFT"]) - return (char.upper(),[]) - def do_main (args, config): url = config.get("url", None) if args.url == None else args.url host = config.get("host", None) diff --git a/relaykeys-qt.py b/relaykeys-qt.py index 7d4d733..b6bf33e 100644 --- a/relaykeys-qt.py +++ b/relaykeys-qt.py @@ -27,8 +27,9 @@ from pathlib import Path # this import is only to support 'type' command in macrofiles -import importlib -relaykeys_cli = importlib.import_module("relaykeys-cli") +from cli_keymap import * +#import importlib +#relaykeys_cli = importlib.import_module("relaykeys-cli") parser = argparse.ArgumentParser(description='Relay Keys qt client.') parser.add_argument('--debug', dest='debug', action='store_const', @@ -390,7 +391,7 @@ def __init__(self, args, config): # loading ketmap for supporting type command in macros if "cli" not in config.sections(): config["cli"] = {} - if not relaykeys_cli.load_keymap_file(config["cli"]): + if not load_keymap_file(config["cli"]): return self._client_queue = Queue(64) @@ -1250,7 +1251,7 @@ def executeMacroBuffer(self): elif cmd_type == "type": #print("got type command: ", cmd_args[0]) #temp for char in cmd_args[0]: - type_key, type_mods = relaykeys_cli.char_to_keyevent_params(char) + type_key, type_mods = char_to_keyevent_params(char) self.send_action("keyevent", type_key, type_mods, True) sleep(0.05)