prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.13k stars 716 forks source link

Keybinding ctrl-shift-<letter> doesn't work #841

Open harleypellowe opened 5 years ago

harleypellowe commented 5 years ago

The documentation indicates it should be possible to keybind ctrl+\<uppercase letter>, but in practice I haven't been able to get it to work.

Traceback (most recent call last):
  File "app.py", line 10, in <module>
    from app.keybindings import keybindings
  File "/Users/user/code/cli-app/app/keybindings.py", line 43, in <module>
    @keybindings.add('c-Y')
  File "/Users/user/code/cli-app/env/lib/python3.7/site-packages/prompt_toolkit/key_binding/key_bindings.py", line 188, in add
    keys = tuple(_check_and_expand_key(k) for k in keys)
  File "/Users/user/code/cli-app/env/lib/python3.7/site-packages/prompt_toolkit/key_binding/key_bindings.py", line 188, in <genexpr>
    keys = tuple(_check_and_expand_key(k) for k in keys)
  File "/Users/user/code/cli-app/env/lib/python3.7/site-packages/prompt_toolkit/key_binding/key_bindings.py", line 337, in _check_and_expand_key
    raise ValueError('Invalid key: %s' % (key, ))
ValueError: Invalid key: c-Y

It looks like those key combos would need to be added to python-prompt-toolkit/prompt_toolkit/keys.py. I'm not sure if this is a bug, or if I'm just misunderstanding how this works (I've just started learning prompt-toolkit).

jonathanslenders commented 5 years ago

Good point! I think the documentation is wrong here. On posix systems (Linux, mac, ...), control-a is the same as control-A. On Windows it can work, but I don't think we support it.

MikkoMMM commented 4 years ago

Is there really no way around this? I would like to have Ctrl+Shift+Z to be a possible redo binding at least.

asmeurer commented 4 years ago

The only way to add Control-Shift keys is to enable your terminal emulator to send specific escape codes for them, and then add those codes to prompt-toolkit (which is currently broken in prompt-toolkit 3.0, see https://github.com/prompt-toolkit/python-prompt-toolkit/issues/993). On Mac both the default Terminal.app and iTerm2 let you do this. On Linux the only terminal I know that lets you do this is Konsole. You can set any CSI-style escape code you want, but I recommend using the official ones (see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Alt-and-Meta-Keys). For instance, for Control-Shift-Z, use "\E[27;6;90~" (Z is ASCII character 90).