Skip to content

feat: Allow handling of Modifier(s)+Backspace/Delete key combinations on Windows #1383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions prompt_toolkit/input/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class ConsoleInputReader:
b"\x1d": Keys.ControlSquareClose, # Control-]
b"\x1e": Keys.ControlCircumflex, # Control-^
b"\x1f": Keys.ControlUnderscore, # Control-underscore (Also for Ctrl-hyphen.)
b"\x7f": Keys.Backspace, # (127) Backspace (ASCII Delete.)
b"\x7f": Keys.ControlBackspace, # (127) Backspace (ASCII Delete.)
}

# Keys that don't carry character data.
Expand Down Expand Up @@ -347,7 +347,7 @@ def _event_to_key_presses(self, ev: KEY_EVENT_RECORD) -> List[KeyPress]:
else:
result = KeyPress(u_char, u_char)

# First we handle Shift-Control-Arrow/Home/End (need to do this first)
# First we handle Shift-Control-Arrow/Home/End/Ins/Del/PgUp/PgDown/BS (need to do this first)
if (
(
ev.ControlKeyState & self.LEFT_CTRL_PRESSED
Expand All @@ -364,8 +364,10 @@ def _event_to_key_presses(self, ev: KEY_EVENT_RECORD) -> List[KeyPress]:
Keys.Home: Keys.ControlShiftHome,
Keys.End: Keys.ControlShiftEnd,
Keys.Insert: Keys.ControlShiftInsert,
Keys.Delete: Keys.ControlShiftDelete,
Keys.PageUp: Keys.ControlShiftPageUp,
Keys.PageDown: Keys.ControlShiftPageDown,
Keys.Backspace: Keys.ControlShiftBackspace,
}
result.key = mapping.get(result.key, result.key)

Expand Down Expand Up @@ -403,6 +405,7 @@ def _event_to_key_presses(self, ev: KEY_EVENT_RECORD) -> List[KeyPress]:
Keys.Delete: Keys.ShiftDelete,
Keys.PageUp: Keys.ShiftPageUp,
Keys.PageDown: Keys.ShiftPageDown,
Keys.Backspace: Keys.ShiftBackspace,
}
result.key = mapping.get(result.key, result.key)

Expand Down
7 changes: 7 additions & 0 deletions prompt_toolkit/key_binding/bindings/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def load_basic_bindings() -> KeyBindings:
@handle("c-^")
@handle("c-_")
@handle("backspace")
@handle("s-backspace")
@handle("c-backspace")
@handle("c-s-backspace")
@handle("up")
@handle("down")
@handle("right")
Expand All @@ -103,6 +106,7 @@ def load_basic_bindings() -> KeyBindings:
@handle("delete")
@handle("s-delete")
@handle("c-delete")
@handle("c-s-delete")
@handle("pageup")
@handle("pagedown")
@handle("s-tab")
Expand Down Expand Up @@ -146,6 +150,9 @@ def _ignore(event: E) -> None:
handle("backspace", filter=insert_mode, save_before=if_no_repeat)(
get_by_name("backward-delete-char")
)
handle("c-backspace", filter=insert_mode, save_before=if_no_repeat)(
get_by_name("backward-delete-char")
)
handle("delete", filter=insert_mode, save_before=if_no_repeat)(
get_by_name("delete-char")
)
Expand Down
1 change: 1 addition & 0 deletions prompt_toolkit/key_binding/bindings/emacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def _esc(event: E) -> None:
handle("c-a")(get_by_name("beginning-of-line"))
handle("c-b")(get_by_name("backward-char"))
handle("c-delete", filter=insert_mode)(get_by_name("kill-word"))
handle("c-backspace", filter=insert_mode)(get_by_name("backward-kill-word"))
handle("c-e")(get_by_name("end-of-line"))
handle("c-f")(get_by_name("forward-char"))
handle("c-left")(get_by_name("backward-word"))
Expand Down
3 changes: 3 additions & 0 deletions prompt_toolkit/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class Keys(str, Enum):
ControlSquareClose = "c-]"
ControlCircumflex = "c-^"
ControlUnderscore = "c-_"
ControlBackspace = "c-backspace"
ShiftBackspace = "s-backspace"
ControlShiftBackspace = "c-s-backspace"

Left = "left"
Right = "right"
Expand Down