-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathmodified-enter.py
More file actions
47 lines (34 loc) · 1.32 KB
/
modified-enter.py
File metadata and controls
47 lines (34 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
"""
Demo for modified-Enter key bindings (Ctrl-Enter and Ctrl-Shift-Enter).
prompt_toolkit enables xterm's `modifyOtherKeys` protocol at startup, so
terminals that implement it (xterm, iTerm2 with the option enabled, kitty,
WezTerm, Alacritty, foot, ghostty, Windows Terminal, ...) can distinguish
these from plain Enter.
Run this and try pressing each combination. Plain Enter still submits.
Terminals that don't support the protocol will just submit on any Enter
variant — that's the expected fallback.
Shift-Enter alone is not included: many terminals that support
modifyOtherKeys still send plain '\\r' for it, so a binding would fire
inconsistently.
"""
from prompt_toolkit import prompt
from prompt_toolkit.application import run_in_terminal
from prompt_toolkit.key_binding import KeyBindings
def main():
bindings = KeyBindings()
def _announce(label):
def _print():
print(f"[{label}] pressed")
run_in_terminal(_print)
@bindings.add("c-enter")
def _(event):
_announce("Ctrl-Enter")
@bindings.add("c-s-enter")
def _(event):
_announce("Ctrl-Shift-Enter")
print("Try Ctrl-Enter and Ctrl-Shift-Enter. Plain Enter submits.")
text = prompt("> ", key_bindings=bindings)
print(f"You said: {text!r}")
if __name__ == "__main__":
main()