Skip to content
Open
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
11 changes: 6 additions & 5 deletions src/app_model/types/_keys/_key_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,7 @@ class _KM(NamedTuple):
SCANCODE_FROM_LOWERCASE_STRING: Dict[str, ScanCode] = {}

KEYCODE_TO_STRING: Dict[KeyCode, str] = {}
KEYCODE_FROM_LOWERCASE_STRING: Dict[str, KeyCode] = {
# two special cases for assigning os-specific strings to the meta key
'win': KeyCode.Meta,
'cmd': KeyCode.Meta,
}
KEYCODE_FROM_LOWERCASE_STRING: Dict[str, KeyCode] = {}

# key symbols on all platforms
KEY_SYMBOLS: dict[KeyCode, str] = {
Expand Down Expand Up @@ -660,6 +656,11 @@ class _KM(NamedTuple):
if km.virtual_key:
_NATIVE_WINDOWS_VK_TO_KEYCODE[km.virtual_key] = km.keycode

# also parse the os-specific symbols and names returned by `to_text`
for mapping in (*OS_KEY_SYMBOLS.values(), *OS_KEY_NAMES.values()):
for keycode, string in mapping.items():
KEYCODE_FROM_LOWERCASE_STRING.setdefault(string.lower(), keycode)

def _keycode_to_string(keycode: KeyCode) -> str:
"""Return the string representation of a KeyCode."""
# sourcery skip
Expand Down
11 changes: 6 additions & 5 deletions src/app_model/types/_keys/_keybindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,18 @@ def validate(cls, v: Any) -> "KeyBinding":
raise TypeError("invalid keybinding") # pragma: no cover


_re_ctrl = re.compile(r"(ctrl|control|ctl|⌃|\^)[\+|\-]")
_re_shift = re.compile(r"(shift|⇧)[\+|\-]")
_re_alt = re.compile(r"(alt|opt|option|⌥)[\+|\-]")
_re_meta = re.compile(r"(meta|super|win|windows|⊞|cmd|command|⌘)[\+|\-]")
_re_ctrl = re.compile(r"(ctrl|control|ctl|\^)[\+|\-]|⌃[\+|\-]?(?=.)")
_re_shift = re.compile(r"shift[\+|\-]|⇧[\+|\-]?(?=.)")
_re_alt = re.compile(r"(alt|opt|option)[\+|\-]|⌥[\+|\-]?(?=.)")
_re_meta = re.compile(r"(meta|super|win|windows|cmd|command)[\+|\-]|[⊞⌘][\+|\-]?(?=.)")


def _parse_modifiers(input: str) -> tuple[dict[str, bool], str]:
"""Parse modifiers from a string (case insensitive).

modifiers must start at the beginning of the string, and be separated by
"+" or "-". e.g. "ctrl+shift+alt+K" or "Ctrl-Cmd-K"
"+" or "-". e.g. "ctrl+shift+alt+K" or "Ctrl-Cmd-K". Modifier symbols
may omit the separator, as long as a key follows. e.g. "⌘⇧K"
"""
remainder = input.lower()

Expand Down
13 changes: 13 additions & 0 deletions tests/test_keybindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,16 @@ def test_int_conversion_respects_os(enc: int, os: OperatingSystem, expect: str)
kb = KeyBinding.from_int(enc, os=os)
assert kb.to_text(os=os) == expect
assert kb.to_int(os=os) == enc


@pytest.mark.parametrize(
("text", "expected"),
[
("Ctrl+↵", "Ctrl+Enter"),
("⌘⇧↵", "Shift+Meta+Enter"),
("⌘", "Meta"),
("Option", "Alt"),
],
)
def test_from_str_symbols(text: str, expected: str) -> None:
assert str(KeyBinding.from_str(text)) == expected
Loading