Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let string and Unicode processors adapt to CapsLock LED state #129

Merged
merged 7 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 39 additions & 30 deletions src/keyszer/config_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,30 +175,35 @@ class UnicodeNumberToolarge(Exception):

def to_US_keystrokes(s):
"""
Turn alphanumeric string (with spaces and some ASCII) up to length of 100 characters into keystroke commands
Turn alphanumeric string (with spaces and some ASCII) up to length
of 100 characters into keystroke commands

Warn: Almost certainly not going to work with non-US keymaps.
"""
if len(s) > 100:
raise TypingTooLong("`to_keystrokes` only supports strings of 100 characters or less")

combo_list = []
for c in s:
if ord(c) > 127:
combos = unicode_keystrokes(ord(c))
combo_list.extend(combos)
elif c.isupper():
combo_list.append(combo("Shift-" + c))
elif (str.isalnum(c)):
combo_list.append(Key[c.upper()])
elif c in ASCII_TO_KEY:
combo_list.append(ASCII_TO_KEY[c])
elif c in ASCII_WITH_SHIFT:
combo_list.append(ASCII_WITH_SHIFT[c])
else:
raise CharacterNotSupported(f"The character {c} is not supported by `to_keystrokes` yet.")

return combo_list
def _to_keystrokes(ctx):
combo_list = []
for c in s:
if ord(c) > 127:
combo_list.append(unicode_keystrokes(ord(c)))
elif c.isupper():
if ctx.capslock_on: combo_list.append(combo(c))
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
else: combo_list.append(combo("Shift-" + c))
elif (str.isdigit(c)):
combo_list.append(Key[c.upper()])
elif (str.isalpha(c)):
if ctx.capslock_on: combo_list.append(combo("Shift-" + c))
else: combo_list.append(Key[c.upper()])
elif c in ASCII_TO_KEY:
combo_list.append(ASCII_TO_KEY[c])
elif c in ASCII_WITH_SHIFT:
combo_list.append(ASCII_WITH_SHIFT[c])
else:
raise CharacterNotSupported(f"The character {c} is not supported by `to_keystrokes` yet.")
return combo_list

return _to_keystrokes


def _digits(n, base):
Expand All @@ -213,17 +218,21 @@ def unicode_keystrokes(n):
"""Turn Unicode number into keystroke commands"""
if (n > 0x10ffff):
raise UnicodeNumberToolarge(f"{hex(n)} too large for Unicode keyboard entry.")

combo_list = [
combo("Shift-Ctrl-u"),
*[Key[hexdigit]
for digit in _digits(n, 16)
for hexdigit in hex(digit)[2:].upper()
],
Key.ENTER,
]

return combo_list
def _unicode_keystrokes(ctx):
combo_list = [
combo("Shift-Ctrl-u"), # requires "ibus" or "fctix" as input manager?
*[Key[hexdigit]
for digit in _digits(n, 16)
for hexdigit in hex(digit)[2:].upper()
],
Key.ENTER,
]
if ctx.capslock_on:
combo_list.insert(0, Key.CAPSLOCK)
combo_list.append(Key.CAPSLOCK)
RedBearAK marked this conversation as resolved.
Show resolved Hide resolved
return combo_list

return _unicode_keystrokes


def combo(exp): # pylint: disable=invalid-name
Expand Down
11 changes: 8 additions & 3 deletions src/keyszer/transform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import time
import inspect

from evdev import ecodes

Expand Down Expand Up @@ -480,7 +481,7 @@ def transform_key(key, action, ctx):
if not _output.is_mod_pressed(ks.key):
ks.spent = True
debug("spent modifiers", [_.key for _ in held if _.spent])
reset_mode = handle_commands(keymap[combo], key, action, combo)
reset_mode = handle_commands(keymap[combo], key, action, ctx, combo)
if reset_mode:
_active_keymaps = None
return
Expand Down Expand Up @@ -543,7 +544,7 @@ def auto_sticky(combo, input_combo):
# ─── COMMAND PROCESSING ───────────────────────────────────────────────────────


def handle_commands(commands, key, action, input_combo=None):
def handle_commands(commands, key, action, ctx, input_combo=None):
"""
returns: reset_mode (True/False) if this is True, _active_keymaps will be reset
"""
Expand All @@ -566,7 +567,11 @@ def handle_commands(commands, key, action, input_combo=None):
for command in commands:
if callable(command):
# very likely we're just passing None forwards here but that OK
reset_mode = handle_commands(command(), key, action)
cmd_param_cnt = len(inspect.signature(command).parameters)
if cmd_param_cnt == 0:
reset_mode = handle_commands(command(), key, action, ctx)
else:
reset_mode = handle_commands(command(ctx), key, action, ctx)
# if the command wants to disable reset, lets propagate that
if reset_mode is False:
return False
Expand Down