From a0f3e801ce11a40ba5139b929552f8e5dbecf083 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Wed, 24 Feb 2021 19:01:13 +0300 Subject: [PATCH 1/3] feat: add Control+/Shift+Backspace on Windows Allows e.g. mapping Control+BS to a delete-last-word function Partially covers #1380 --- prompt_toolkit/input/win32.py | 4 +++- prompt_toolkit/key_binding/bindings/basic.py | 3 +++ prompt_toolkit/keys.py | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/prompt_toolkit/input/win32.py b/prompt_toolkit/input/win32.py index d79bded043..467a6ddfa5 100644 --- a/prompt_toolkit/input/win32.py +++ b/prompt_toolkit/input/win32.py @@ -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. @@ -366,6 +366,7 @@ def _event_to_key_presses(self, ev: KEY_EVENT_RECORD) -> List[KeyPress]: Keys.Insert: Keys.ControlShiftInsert, Keys.PageUp: Keys.ControlShiftPageUp, Keys.PageDown: Keys.ControlShiftPageDown, + Keys.Backspace: Keys.ControlShiftBackspace, } result.key = mapping.get(result.key, result.key) @@ -403,6 +404,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) diff --git a/prompt_toolkit/key_binding/bindings/basic.py b/prompt_toolkit/key_binding/bindings/basic.py index 56efe5d693..ede126fbfa 100644 --- a/prompt_toolkit/key_binding/bindings/basic.py +++ b/prompt_toolkit/key_binding/bindings/basic.py @@ -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") diff --git a/prompt_toolkit/keys.py b/prompt_toolkit/keys.py index 58b2e4b7c0..86fbade6ce 100644 --- a/prompt_toolkit/keys.py +++ b/prompt_toolkit/keys.py @@ -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" From a2cc7c325fe1e8867d6e87f0eb959c9c4fd6d634 Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Thu, 25 Feb 2021 18:35:31 +0300 Subject: [PATCH 2/3] feat: add ControlBackspace basic(backward-delete-char)/emacs(backward-kill-word) default handling --- prompt_toolkit/key_binding/bindings/basic.py | 3 +++ prompt_toolkit/key_binding/bindings/emacs.py | 1 + 2 files changed, 4 insertions(+) diff --git a/prompt_toolkit/key_binding/bindings/basic.py b/prompt_toolkit/key_binding/bindings/basic.py index ede126fbfa..3d53bbdba5 100644 --- a/prompt_toolkit/key_binding/bindings/basic.py +++ b/prompt_toolkit/key_binding/bindings/basic.py @@ -149,6 +149,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") ) diff --git a/prompt_toolkit/key_binding/bindings/emacs.py b/prompt_toolkit/key_binding/bindings/emacs.py index 35c8aa4e8c..107e442422 100644 --- a/prompt_toolkit/key_binding/bindings/emacs.py +++ b/prompt_toolkit/key_binding/bindings/emacs.py @@ -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")) From 9280dc6c58ec1451d2cebbbfca643337ffe09b3c Mon Sep 17 00:00:00 2001 From: eugenesvk Date: Fri, 26 Feb 2021 16:11:13 +0300 Subject: [PATCH 3/3] feat: add handling of Control-Shift-Delete on Windows --- prompt_toolkit/input/win32.py | 3 ++- prompt_toolkit/key_binding/bindings/basic.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/prompt_toolkit/input/win32.py b/prompt_toolkit/input/win32.py index 467a6ddfa5..3e819a5c95 100644 --- a/prompt_toolkit/input/win32.py +++ b/prompt_toolkit/input/win32.py @@ -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 @@ -364,6 +364,7 @@ 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, diff --git a/prompt_toolkit/key_binding/bindings/basic.py b/prompt_toolkit/key_binding/bindings/basic.py index 3d53bbdba5..6f272390a7 100644 --- a/prompt_toolkit/key_binding/bindings/basic.py +++ b/prompt_toolkit/key_binding/bindings/basic.py @@ -106,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")