[Concept,08/19] input: Add Ctrl+arrow key support for sandbox SDL console

Message ID 20260130035849.3580212-9-simon.glass@canonical.com
State New
Headers
Series Enhanced command-line editing with undo/redo support |

Commit Message

Simon Glass Jan. 30, 2026, 3:58 a.m. UTC
  Track the Ctrl modifier state in the input driver and generate the
appropriate escape sequences (ESC [ 1 ; 5 D/C) when Ctrl+Left or
Ctrl+Right arrow keys are pressed. This enables word navigation in
the sandbox SDL console.

There are no tests for this feature.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 drivers/input/input.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
  

Patch

diff --git a/drivers/input/input.c b/drivers/input/input.c
index 3f146fb07e6..5206d6f68d6 100644
--- a/drivers/input/input.c
+++ b/drivers/input/input.c
@@ -28,6 +28,9 @@  enum {
 	/* Special flag ORed with key code to indicate release */
 	KEY_RELEASE		= 1 << 15,
 	KEY_MASK		= 0xfff,
+
+	/* Modifier bits for config->modifiers */
+	MOD_CTRL		= 1 << 0,
 };
 
 /*
@@ -428,6 +431,31 @@  static int input_keycode_to_ansi364(struct input_config *config,
 	int ch_count;
 	int i;
 
+	/* Handle Ctrl+arrow keys for word navigation */
+	if (config->modifiers & MOD_CTRL) {
+		const char *seq = NULL;
+
+		switch (keycode) {
+		case KEY_LEFT:
+			seq = "[1;5D";	/* Ctrl+Left: backward-word */
+			break;
+		case KEY_RIGHT:
+			seq = "[1;5C";	/* Ctrl+Right: forward-word */
+			break;
+		}
+		if (seq) {
+			ch_count = 0;
+			output_ch[ch_count++] = 0x1b;
+			while (*seq) {
+				if (ch_count < max_chars)
+					output_ch[ch_count] = *seq;
+				ch_count++;
+				seq++;
+			}
+			return ch_count;
+		}
+	}
+
 	for (i = ch_count = 0; i < ARRAY_SIZE(kbd_to_ansi364); i++) {
 		if (keycode != kbd_to_ansi364[i].kbd_scan_code)
 			continue;
@@ -483,6 +511,9 @@  static int input_keycodes_to_ascii(struct input_config *config,
 			table = process_modifier(config, key,
 					keycode[i] & KEY_RELEASE);
 		}
+		/* Track Ctrl state for special key handling */
+		if (key == KEY_LEFTCTRL || key == KEY_RIGHTCTRL)
+			config->modifiers |= MOD_CTRL;
 	}
 
 	/* Start conversion by looking for the first new keycode (by same). */