prompt-toolkit / prompt-toolkit/python-prompt-toolkit
Confusing mapping 127→Backspace on Windows
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 10.6k
- Forks
- 815
- PR merge metrics
- No merged PRs in 30d
Description
On Windows 10, using Windows Terminal as well as the integrated conhost, I tried using ptpython as well as ipython REPLs, both using prompt-toolkit==3.0.39. I encountered an issue: Ctrl+Backspace worked like a regular Backspace.
I looked around, checked configs of the REPLs, and nothing worked until I decided to dig into prompt-toolkit. There, in file input\win32.py i.e. https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/src/prompt_toolkit/input/win32.py, is a line mapping an input sequence to a key:
b"\x7f": Keys.Backspace, # (127) Backspace (ASCII Delete.)
however, when using this handy code:
import msvcrt
while True:
key = msvcrt.getch()
if key == b'\x1b': break # 'ESC'
print(f"ASCII: {ord(key)} = {hex(ord(key))}")
I see that inside REPLs, Ctrl+Backspace is 127 = 0x7f, Backspace and Meta+Backspace is 8 = 0x8.
For my purposes, I fixed the problem for the time being by replacing this mapping with:
b"\x7f": Keys.ControlW,
and now Ctrl+Backspace works as expected (backward_kill_word is called) everywhere. Also, I'm a little confused what's going on, and why this mapping was like this. How Meta+Backspace seemingly sends Backspace sequence, yet also calls backward_kill_word, while Backspace just backspaces, is a cherry on top.
It looks like a bug—but maybe my config is somehow a strange outlier?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/prompt_toolkit/input/win32.py and reproduce the Windows key values with the msvcrt snippet from the issue. Compare the mappings for 0x7f and 0x08 with Ctrl+Backspace, Backspace, and Meta+Backspace behavior in the REPLs. Done means the intended Ctrl+Backspace action works while ordinary Backspace behavior remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100