Fix parsing of shortcuts to support the new keys

This commit is contained in:
Kovid Goyal 2021-01-12 08:24:50 +05:30
parent 1690718710
commit 1ec9a8b777
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 25 additions and 37 deletions

View File

@ -4,7 +4,6 @@
# Utils {{{ # Utils {{{
import os import os
from contextlib import suppress
from gettext import gettext as _ from gettext import gettext as _
from typing import ( from typing import (
Any, Callable, Dict, FrozenSet, Iterable, List, Optional, Sequence, Set, Any, Callable, Dict, FrozenSet, Iterable, List, Optional, Sequence, Set,
@ -67,17 +66,29 @@ def parse_shortcut(sc: str) -> SingleKey:
mods = parse_mods(parts[:-1], sc) or 0 mods = parse_mods(parts[:-1], sc) or 0
if not mods: if not mods:
raise InvalidMods('Invalid shortcut') raise InvalidMods('Invalid shortcut')
q = parts[-1].upper()
key: Optional[int] = getattr(defines, 'GLFW_KEY_' + key_name_aliases.get(q, q), None)
is_native = False
if key is None:
q = parts[-1] q = parts[-1]
is_native = False
if q.startswith('0x'): if q.startswith('0x'):
with suppress(Exception): try:
key = int(q, 16) key = int(q, 16)
except Exception:
key = 0
else: else:
key = get_key_name_lookup()(q, False) is_native = True
is_native = key is not None else:
try:
key = ord(q)
except Exception:
uq = q.upper()
uq = key_name_aliases.get(uq, uq)
x: Optional[int] = getattr(defines, f'GLFW_FKEY_{uq}', None)
if x is None:
lf = get_key_name_lookup()
key = lf(q, False) or 0
is_native = key > 0
else:
key = x
return SingleKey(mods, is_native, key or 0) return SingleKey(mods, is_native, key or 0)
@ -123,9 +134,8 @@ as color16 to color255.''')
'shortcuts': [ 'shortcuts': [
_('Keyboard shortcuts'), _('Keyboard shortcuts'),
_('''\ _('''\
For a list of key names, see: :link:`the GLFW key macros Keys are identified simply by their lowercase unicode characters. For example:
<https://github.com/kovidgoyal/kitty/blob/master/glfw/glfw3.h#L349>`. ``a`` for the A key, ``[`` for the left square bracket key, etc.
The name to use is the part after the :code:`GLFW_KEY_` prefix.
For a list of modifier names, see: For a list of modifier names, see:
:link:`GLFW mods <https://www.glfw.org/docs/latest/group__mods.html>` :link:`GLFW mods <https://www.glfw.org/docs/latest/group__mods.html>`

View File

@ -10,30 +10,8 @@ from .constants import is_macos
key_name_aliases = { key_name_aliases = {
'!': 'EXCLAM', 'SPACE': ' ',
'"': 'DOUBLE_QUOTE', 'SPC': ' ',
'#': 'NUMBER_SIGN',
'$': 'DOLLAR',
'&': 'AMPERSAND',
"'": 'APOSTROPHE',
'(': 'PARENTHESIS_LEFT',
')': 'PARENTHESIS_RIGHT',
',': 'COMMA',
'-': 'MINUS',
'.': 'PERIOD',
'/': 'SLASH',
':': 'COLON',
';': 'SEMICOLON',
'<': 'LESS',
'=': 'EQUAL',
'>': 'GREATER',
'@': 'AT',
'[': 'LEFT_BRACKET',
'\\': 'BACKSLASH',
']': 'RIGHT_BRACKET',
'^': 'CIRCUMFLEX',
'_': 'UNDERSCORE',
'`': 'GRAVE_ACCENT',
'ESC': 'ESCAPE', 'ESC': 'ESCAPE',
'PGUP': 'PAGE_UP', 'PGUP': 'PAGE_UP',
'PAGEUP': 'PAGE_UP', 'PAGEUP': 'PAGE_UP',