Skip to content

Commit

Permalink
Merge pull request #110 from f1andrew/master
Browse files Browse the repository at this point in the history
changed importing of cli_keymap
  • Loading branch information
willwade committed Jul 18, 2022
2 parents 3394c34 + fd00821 commit 5a35919
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
33 changes: 33 additions & 0 deletions cli_keymap.py
Original file line number Diff line number Diff line change
@@ -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(),[])
30 changes: 2 additions & 28 deletions relaykeys-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,24 +36,6 @@
parser.add_argument('commands', metavar='COMMAND', nargs='*',
help='One or more commands, format: <cmdname>:<data>')

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:
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions relaykeys-qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5a35919

Please sign in to comment.