Implement --grab-keyboard

Currently works on Wayland.
This commit is contained in:
Kovid Goyal
2025-05-18 14:30:33 +05:30
parent cc4d4eeaca
commit 95c6279bdd
8 changed files with 37 additions and 9 deletions

View File

@@ -123,6 +123,7 @@ from .options.utils import MINIMUM_FONT_SIZE, KeyboardMode, KeyDefinition
from .os_window_size import initial_window_size_func
from .session import Session, create_sessions, get_os_window_sizing_data
from .shaders import load_shader_programs
from .simple_cli_definitions import grab_keyboard_docs
from .tabs import SpecialWindow, SpecialWindowInstance, Tab, TabDict, TabManager
from .types import _T, AsyncResponse, LayerShellConfig, SingleInstanceData, WindowSystemMouseEvent, ac
from .typing_compat import PopenType, TypedDict
@@ -3204,11 +3205,7 @@ class Boss:
continue
window.screen.clear_selection()
@ac('misc', '''
Grab the keyboard. This means global shortcuts defined in the OS will be handled in kitty instead. How well this
works depends on the OS/window manager/desktop environment. On Wayland it works only if the compositor implements
the :link:`inhibit-keyboard-shortcuts protocol <https://wayland.app/protocols/keyboard-shortcuts-inhibit-unstable-v1>`.
''')
@ac('misc', grab_keyboard_docs)
def grab_keyboard(self) -> None:
grab_keyboard(True)

View File

@@ -160,6 +160,7 @@ class Mappings:
key_action = get_shortcut(mode.keymap, ev)
if key_action is None and self.global_shortcuts_map and (global_key_action := get_shortcut(self.global_shortcuts_map, ev)) is not None:
if grab_keyboard(None):
# the shortcuts in the global menubar will have been bypassed so trigger them here
key_action = global_key_action
else:
return True

View File

@@ -42,6 +42,7 @@ from .fast_data_types import (
glfw_get_monitor_workarea,
glfw_init,
glfw_terminate,
grab_keyboard,
is_layer_shell_supported,
load_png_data,
mask_kitty_signals_process_wide,
@@ -256,6 +257,8 @@ def _run_app(opts: Options, args: CLIOptions, bad_lines: Sequence[BadLine] = (),
if _is_panel_kitten and not is_layer_shell_supported():
raise SystemExit('Cannot create panels as the window manager/compositor does not support the neccessary protocols')
pos_x, pos_y = None, None
if args.grab_keyboard:
grab_keyboard(True)
with cached_values_for(run_app.cached_values_name) as cached_values:
if not _is_panel_kitten and not is_wayland():
if opts.remember_window_position:

View File

@@ -323,6 +323,13 @@ def generate_c_parsers() -> Iterator[str]:
# kitty CLI spec {{{
grab_keyboard_docs = """\
Grab the keyboard. This means global shortcuts defined in the OS will be passed to kitty instead. Useful if
you want to create an OS modal window. How well this
works depends on the OS/window manager/desktop environment. On Wayland it works only if the compositor implements
the :link:`inhibit-keyboard-shortcuts protocol <https://wayland.app/protocols/keyboard-shortcuts-inhibit-unstable-v1>`.
"""
listen_on_defn = f'''\
--listen-on
completion=type:special group:complete_kitty_listen_on
@@ -484,6 +491,11 @@ See also :opt:`remember_window_position` to have kitty automatically try
to restore the previous window position.
--grab-keyboard
type=bool-set
{grab_keyboard_docs}
# Debugging options
--version -v
@@ -539,8 +551,9 @@ type=bool-set
'''
setattr(kitty_options_spec, 'ans', OPTIONS.format(
appname=appname, conf_name=appname, listen_on_defn=listen_on_defn,
config_help=CONFIG_HELP.format(appname=appname, conf_name=appname),
))
grab_keyboard_docs=grab_keyboard_docs,
config_help=CONFIG_HELP.format(appname=appname, conf_name=appname
)))
ans: str = getattr(kitty_options_spec, 'ans')
return ans
# }}}
@@ -675,6 +688,11 @@ to :code:`on-demand`. Note that on Wayland, depending on the compositor, this ca
becoming visible.
--grab-keyboard
type=bool-set
{grab_keyboard_docs}
--exclusive-zone
type=int
default={exclusive_zone}
@@ -740,7 +758,7 @@ Path to a log file to store STDOUT/STDERR when using :option:`--detach`
--debug-rendering
type=bool-set
For internal debugging use.
'''.format(appname=appname, listen_on_defn=listen_on_defn, **d)
'''.format(appname=appname, listen_on_defn=listen_on_defn, grab_keyboard_docs=grab_keyboard_docs, **d)
# }}}