diff --git a/kitty/config.py b/kitty/config.py index 1961578d1..cc28b4b07 100644 --- a/kitty/config.py +++ b/kitty/config.py @@ -20,6 +20,7 @@ from .conf.utils import ( from .config_data import all_options, parse_mods, type_map from .constants import cache_dir, defconf, is_macos from .utils import log_error +from .key_names import get_key_name_lookup named_keys = { "'": 'APOSTROPHE', @@ -47,11 +48,12 @@ def parse_shortcut(sc): key = getattr(defines, 'GLFW_KEY_' + named_keys.get(key, key), None) is_native = False if key is None: - if parts[-1].startswith('0x'): + q = parts[-1] + if q.startswith('0x'): with suppress(Exception): - key = int(parts[-1], 16) + key = int(q, 16) else: - key = defines.key_for_native_key_name(parts[-1]) + key = get_key_name_lookup()(q) is_native = key is not None return mods, is_native, key diff --git a/kitty/key_names.py b/kitty/key_names.py new file mode 100644 index 000000000..4fc5775a3 --- /dev/null +++ b/kitty/key_names.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +# License: GPLv3 Copyright: 2019, Kovid Goyal + +import sys + +from .constants import is_macos + + +def null_lookup(name, case_sensitive=False): + pass + + +if is_macos: + def get_key_name_lookup(): + return null_lookup +else: + + def load_libxkb_lookup(): + import ctypes + lib = ctypes.CDLL('libxkbcommon.so') + f = lib.xkb_keysym_from_name + f.argtypes = [ctypes.c_char_p, ctypes.c_int] + f.restype = ctypes.c_int + + def xkb_lookup(name, case_sensitive=False): + name = name.encode('utf-8') + return f(name, int(case_sensitive)) or None + + return xkb_lookup + + def get_key_name_lookup(): + ans = getattr(get_key_name_lookup, 'ans', None) + if ans is None: + try: + ans = load_libxkb_lookup() + except Exception as e: + print('Failed to load libxkbcommon.xkb_keysym_from_name with error:', e, file=sys.stderr) + ans = null_lookup + get_key_name_lookup.ans = ans + return ans diff --git a/kitty/main.py b/kitty/main.py index e3e51ee38..f1959e319 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -244,7 +244,6 @@ def _main(): args, rest = parse_args(args=args) args.args = rest if args.debug_config: - init_glfw(args.debug_keyboard) # needed for parsing native keysyms create_opts(args, debug_config=True) return if getattr(args, 'detach', False): @@ -258,9 +257,9 @@ def _main(): if not is_first: talk_to_instance(args) return - init_glfw(args.debug_keyboard) # needed for parsing native keysyms bad_lines = [] opts = create_opts(args, accumulate_bad_lines=bad_lines) + init_glfw(args.debug_keyboard) setup_environment(opts, args) try: with setup_profiling(args):