From 99995fd9dc91c52abb72c7951fc1da226949a855 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 3 Dec 2023 13:23:01 +0530 Subject: [PATCH] Remote control API for send-key --- docs/mapping.rst | 1 + kitty/rc/send_key.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 kitty/rc/send_key.py diff --git a/docs/mapping.rst b/docs/mapping.rst index addbed08e..abecb16a6 100644 --- a/docs/mapping.rst +++ b/docs/mapping.rst @@ -178,6 +178,7 @@ matches some criteria, such as when it has a particular title or user variable. Let's see some examples:: map --when-focus-on title:keyboard.protocol kitty_mod+t + This will cause :kbd:`kitty_mod+t` (the default shortcut for opening a new tab) to be unmapped only when the focused window has :code:`keyboard protocol` in its title. Run the show-key kitten as:: diff --git a/kitty/rc/send_key.py b/kitty/rc/send_key.py new file mode 100644 index 000000000..257a05133 --- /dev/null +++ b/kitty/rc/send_key.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# License: GPLv3 Copyright: 2020, Kovid Goyal + +from typing import TYPE_CHECKING, Optional + +from .base import ( + MATCH_TAB_OPTION, + MATCH_WINDOW_OPTION, + ArgsType, + Boss, + PayloadGetType, + PayloadType, + RCOptions, + RemoteCommand, + ResponseType, + Window, +) + +if TYPE_CHECKING: + from kitty.cli_stub import SendKeyRCOptions as CLIOptions + + +class SendKey(RemoteCommand): + protocol_spec = __doc__ = ''' + keys+/list.str: The keys to send + match/str: A string indicating the window to send text to + match_tab/str: A string indicating the tab to send text to + all/bool: A boolean indicating all windows should be matched. + exclude_active/bool: A boolean that prevents sending text to the active window + ''' + short_desc = 'Send arbitrary key presses to the specified windows' + desc = ( + 'Send arbitrary key presses to specified windows. All specified keys are sent first as press events' + ' then as release events in reverse order. Keys are sent to the programs running in the windows.' + ' They are sent only if the current keyboard mode for the program supports the particular key.' + ' For example: send-key ctrl+a ctrl+b' + ) + options_spec = MATCH_WINDOW_OPTION + '\n\n' + MATCH_TAB_OPTION.replace('--match -m', '--match-tab -t') + '''\n +--all +type=bool-set +Match all windows. + + +--exclude-active +type=bool-set +Do not send text to the active window, even if it is one of the matched windows. +''' + args = RemoteCommand.Args(spec='[KEYS TO SEND ...]', json_field='keys') + + def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType: + ret = {'match': opts.match, 'keys': args, 'match_tab': opts.match_tab, 'all': opts.all, 'exclude_active': opts.exclude_active} + return ret + + def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType: + windows = self.windows_for_payload(boss, None, payload_get) + keys = payload_get('keys') + sent = False + for w in windows: + if not w.send_key(*keys): + sent = True + sent + return None + + +send_key = SendKey()