diff options
author | stijn <stijn@ignitron.net> | 2020-01-13 12:11:27 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2020-01-22 16:54:19 +1100 |
commit | 599371b133ec24c8ccfc3b75dd5a62b94b4b4055 (patch) | |
tree | d78819b486fa81099ed297a463874bb528fc464b | |
parent | a55c17dc69ac43cd41e7efe603e20298a5513b06 (diff) |
windows: Support word-based move/delete key sequences for REPL.
Translate common Ctrl-Left/Right/Delete/Backspace to the EMACS-style
sequences (i.e. Alt key based) for forward-word, backward-word, forwad-kill
and backward-kill. Requires MICROPY_REPL_EMACS_WORDS_MOVE to be defined so
the readline implementation interprets these.
-rw-r--r-- | ports/windows/windows_mphal.c | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/ports/windows/windows_mphal.c b/ports/windows/windows_mphal.c index 1e3a09291..1ec8e27d6 100644 --- a/ports/windows/windows_mphal.c +++ b/ports/windows/windows_mphal.c @@ -141,7 +141,7 @@ typedef struct item_t { const char *seq; } item_t; -// map virtual key codes to VT100 escape sequences +// map virtual key codes to key sequences known by MicroPython's readline implementation STATIC item_t keyCodeMap[] = { {VK_UP, "[A"}, {VK_DOWN, "[B"}, @@ -153,10 +153,19 @@ STATIC item_t keyCodeMap[] = { {0, ""} //sentinel }; +// likewise, but with Ctrl key down +STATIC item_t ctrlKeyCodeMap[] = { + {VK_LEFT, "b"}, + {VK_RIGHT, "f"}, + {VK_DELETE, "d"}, + {VK_BACK, "\x7F"}, + {0, ""} //sentinel +}; + STATIC const char *cur_esc_seq = NULL; -STATIC int esc_seq_process_vk(int vk) { - for (item_t *p = keyCodeMap; p->vkey != 0; ++p) { +STATIC int esc_seq_process_vk(WORD vk, bool ctrl_key_down) { + for (item_t *p = (ctrl_key_down ? ctrlKeyCodeMap : keyCodeMap); p->vkey != 0; ++p) { if (p->vkey == vk) { cur_esc_seq = p->seq; return 27; // ESC, start of escape sequence @@ -194,14 +203,16 @@ int mp_hal_stdin_rx_chr(void) { if (rec.EventType != KEY_EVENT || !rec.Event.KeyEvent.bKeyDown) { // only want key down events continue; } + const bool ctrl_key_down = (rec.Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) || + (rec.Event.KeyEvent.dwControlKeyState & RIGHT_CTRL_PRESSED); + const int ret = esc_seq_process_vk(rec.Event.KeyEvent.wVirtualKeyCode, ctrl_key_down); + if (ret) { + return ret; + } const char c = rec.Event.KeyEvent.uChar.AsciiChar; if (c) { // plain ascii char, return it return c; } - const int ret = esc_seq_process_vk(rec.Event.KeyEvent.wVirtualKeyCode); - if (ret) { - return ret; - } } } |