8000 windows: Support word-based move/delete key sequences for REPL. · andrewleech/micropython@599371b · GitHub
[go: up one dir, main page]

Skip to content

Commit 599371b

Browse files
stinosdpgeorge
authored andcommitted
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.
1 parent a55c17d commit 599371b

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

ports/windows/windows_mphal.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ typedef struct item_t {
141141
const char *seq;
142142
} item_t;
143143

144-
// map virtual key codes to VT100 escape sequences
144+
// map virtual key codes to key sequences known by MicroPython's readline implementation
145145
STATIC item_t keyCodeMap[] = {
146146
{VK_UP, "[A"},
147147
{VK_DOWN, "[B"},
@@ -153,10 +153,19 @@ STATIC item_t keyCodeMap[] = {
153153
{0, ""} //sentinel
154154
};
155155

156+
// likewise, but with Ctrl key down
157+
STATIC item_t ctrlKeyCodeMap[] = {
158+
{VK_LEFT, "b"},
159+
{VK_RIGHT, "f"},
160+
{VK_DELETE, "d"},
161+
{VK_BACK, "\x7F"},
162+
{0, ""} //sentinel
163+
};
164+
156165
STATIC const char *cur_esc_seq = NULL;
157166

158-
STATIC int esc_seq_process_vk(int vk) {
159-
for (item_t *p = keyCodeMap; p->vkey != 0; ++p) {
167+
STATIC int esc_seq_process_vk(WORD vk, bool ctrl_key_down) {
168+
for (item_t *p = (ctrl_key_down ? ctrlKeyCodeMap : keyCodeMap); p->vkey != 0; ++p) {
160169
if (p->vkey == vk) {
161170
cur_esc_seq = p->seq;
162171
return 27; // ESC, start of escape sequence
@@ -194,14 +203,16 @@ int mp_hal_stdin_rx_chr(void) {
194203
if (rec.EventType != KEY_EVENT || !rec.Event.KeyEvent.bKeyDown) { // only want key down events
195204
continue;
196205
}
206+
const bool ctrl_key_down = (rec.Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) ||
207+
(rec.Event.KeyEvent.dwControlKeyState & RIGHT_CTRL_PRESSED);
208+
const int ret = esc_seq_process_vk(rec.Event.KeyEvent.wVirtualKeyCode, ctrl_key_down);
209+
if (ret) {
210+
return ret;
211+
}
197212
const char c = rec.Event.KeyEvent.uChar.AsciiChar;
198213
if (c) { // plain ascii char, return it
199214
return c;
200215
}
201-
const int ret = esc_seq_process_vk(rec.Event.KeyEvent.wVirtualKeyCode);
202-
if (ret) {
203-
return ret;
204-
}
205216
}
206217
}
207218

0 commit comments

Comments
 (0)
0