Remote control API for send-key

This commit is contained in:
Kovid Goyal 2023-12-03 13:23:01 +05:30
parent 74388b4183
commit 99995fd9dc
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 66 additions and 0 deletions

View file

@ -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::

65
kitty/rc/send_key.py Normal file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
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()