Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: dispatch modifier keys as key events instead of literal text

Bosch918cc4b41287
diff --git a/internal/bridge/cdp_keyboard.go b/internal/bridge/cdp_keyboard.go
index efd6a978..7fa35b9d 100644
--- a/internal/bridge/cdp_keyboard.go
+++ b/internal/bridge/cdp_keyboard.go
@@ -18,6 +18,14 @@ var namedKeyDefs = map[string]struct {
"Return": {"Enter", 13, "\r"},
"Tab": {"Tab", 9, "\t"},
"Escape": {"Escape", 27, ""},
+ // Modifier keys must dispatch keyDown/keyUp events, never text. Without these
+ // entries they fall through to chromedp.KeyEvent and the literal name ("Shift")
+ // is typed into the focused field (issue #588). insertText is empty so the
+ // browser receives a real modifier key event with no character.
+ "Shift": {"ShiftLeft", 16, ""},
+ "Control": {"ControlLeft", 17, ""},
+ "Alt": {"AltLeft", 18, ""},
+ "Meta": {"MetaLeft", 91, ""},
"Backspace": {"Backspace", 8, ""},
"Delete": {"Delete", 46, ""},
"ArrowLeft": {"ArrowLeft", 37, ""},
diff --git a/internal/bridge/cdp_keyboard_test.go b/internal/bridge/cdp_keyboard_test.go
index ba5f1b5f..09726587 100644
--- a/internal/bridge/cdp_keyboard_test.go
+++ b/internal/bridge/cdp_keyboard_test.go
@@ -58,6 +58,27 @@ func TestDispatchNamedKey_NonPrintableNoInsertText(t *testing.T) {
}
}
+// TestDispatchNamedKey_ModifiersNotTyped is the regression test for issue #588:
+// modifier keys (Shift/Control/Alt/Meta) must be recognised named keys with an
+// empty insertText so they dispatch keyDown/keyUp events instead of falling
+// through to chromedp.KeyEvent, which would type the literal name (e.g. "Shift")
+// into the focused field.
+func TestDispatchNamedKey_ModifiersNotTyped(t *testing.T) {
+ for _, k := range []string{"Shift", "Control", "Alt", "Meta"} {
+ def, ok := namedKeyDefs[k]
+ if !ok {
+ t.Errorf("modifier %q must be a recognised named key (else it gets typed as text)", k)
+ continue
+ }
+ if def.insertText != "" {
+ t.Errorf("modifier %q must have empty insertText, got %q (would type literal text)", k, def.insertText)
+ }
+ if def.code == "" || def.virtualKey == 0 {
+ t.Errorf("modifier %q must carry a code and virtualKey, got code=%q vk=%d", k, def.code, def.virtualKey)
+ }
+ }
+}
+
// TestDispatchNamedKey_ReturnAlias verifies that "Return" is an alias for
// Enter and produces the same CDP parameters.
func TestDispatchNamedKey_ReturnAlias(t *testing.T) {
diff --git a/tests/e2e/fixtures/key-modifiers.html b/tests/e2e/fixtures/key-modifiers.html
new file mode 100644
index 00000000..aa9770cc
--- /dev/null
+++ b/tests/e2e/fixtures/key-modifiers.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>E2E Test - Key Modifiers</title>
+ <style>
+ body { font-family: sans-serif; margin: 0; padding: 32px; line-height: 1.4; }
+ #typed { font-size: 24px; width: 600px; padding: 8px; }
+ #state-json { margin-top: 20px; padding: 12px; border-radius: 10px;
+ background: #111827; color: #d1fae5; white-space: pre-wrap; max-width: 720px; }
+ </style>
+</head>
+<body>
+ <h1>Key Modifiers Fixture</h1>
+ <p>Verifies modifier keys are forwarded as keydown/keyup events and never typed
+ as literal text (issue #588).</p>
+
+ <input id="typed" type="text" placeholder="click me, then press keys">
+ <p style="font-size:22px">Field value: <b id="mirror" style="color:#b91c1c">(empty)</b></p>
+ <div id="state-json">[]</div>
+
+ <script>
+ // keyState records each key event as "down:<key>" / "up:<key>" so a test can
+ // assert modifiers ARE forwarded as key events. The #typed value proves they
+ // are NOT inserted as text.
+ window.keyState = [];
+ const input = document.getElementById('typed');
+ const out = document.getElementById('state-json');
+ const mirror = document.getElementById('mirror');
+ const record = (prefix) => (e) => {
+ window.keyState.push(prefix + ':' + e.key);
+ out.textContent = JSON.stringify(window.keyState);
+ };
+ document.addEventListener('keydown', record('down'));
+ document.addEventListener('keyup', record('up'));
+ input.addEventListener('input', () => {
+ mirror.textContent = input.value === '' ? '(empty)' : input.value;
+ });
+ </script>
+</body>
+</html>
diff --git a/tests/e2e/scenarios/cli/actions-extended.sh b/tests/e2e/scenarios/cli/actions-extended.sh
index deb3b5ba..268f19f7 100644
--- a/tests/e2e/scenarios/cli/actions-extended.sh
+++ b/tests/e2e/scenarios/cli/actions-extended.sh
@@ -160,6 +160,43 @@ assert_output_contains "OK" "keyup response"
end_test
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab press modifier keys are not typed as text (issue #588)"
+
+pt_ok nav "${FIXTURES_URL}/key-modifiers.html"
+pt_ok click --css "#typed"
+
+# Reset the input and the recorded key log.
+pt_ok eval "document.querySelector('#typed').value=''; window.keyState=[]; 'reset'"
+
+# Press each modifier. Before the fix these fell through to chromedp.KeyEvent and
+# the literal name (e.g. "Shift") was typed into the focused input.
+pt_ok press Shift
+pt_ok press Control
+pt_ok press Alt
+pt_ok press Meta
+
+# The focused input must remain empty — no modifier name was inserted as text.
+pt_ok eval "document.querySelector('#typed').value"
+assert_output_not_contains "Shift" "Shift must not be typed as text"
+assert_output_not_contains "Control" "Control must not be typed as text"
+assert_output_not_contains "Alt" "Alt must not be typed as text"
+assert_output_not_contains "Meta" "Meta must not be typed as text"
+
+# …but the modifiers ARE forwarded as real keydown/keyup events (issue #588 expected).
+pt_ok eval "JSON.stringify(window.keyState)"
+assert_output_contains "down:Shift" "Shift forwarded as keydown"
+assert_output_contains "up:Shift" "Shift forwarded as keyup"
+assert_output_contains "down:Control" "Control forwarded as keydown"
+
+# Control: a printable key still types into the input.
+pt_ok eval "document.querySelector('#typed').value=''; 'reset'"
+pt_ok press a
+pt_ok eval "document.querySelector('#typed').value"
+assert_output_contains "a" "printable key still types after the modifier fix"
+
+end_test
+
# ─────────────────────────────────────────────────────────────────
start_test "pinchtab keyboard type <text>"