Skip to content

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

fix: normalize Return to Enter on keydown and keyup dispatch

Luigi Agosti9cb14d50428e
diff --git a/internal/bridge/action_text.go b/internal/bridge/action_text.go
index 0510932f..2bfda38c 100644
--- a/internal/bridge/action_text.go
+++ b/internal/bridge/action_text.go
@@ -308,16 +308,7 @@ func (b *Bridge) dispatchSingleKeyEvent(ctx context.Context, req ActionRequest,
if req.Key == "" {
return nil, fmt.Errorf("key required for %s", resultKey)
}
- err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- params := map[string]any{"type": eventType, "key": req.Key}
- if def, ok := namedKeyDefs[req.Key]; ok {
- params["code"] = def.code
- params["windowsVirtualKeyCode"] = def.virtualKey
- params["nativeVirtualKeyCode"] = def.virtualKey
- }
- return chromedp.FromContext(ctx).Target.Execute(ctx, "Input.dispatchKeyEvent", params, nil)
- }))
- if err != nil {
+ if err := dispatchNamedKeyEvent(ctx, req.Key, eventType); err != nil {
return nil, err
}
return map[string]any{resultKey: req.Key}, nil
diff --git a/internal/bridge/cdp_keyboard.go b/internal/bridge/cdp_keyboard.go
index e14e913b..4a2402ba 100644
--- a/internal/bridge/cdp_keyboard.go
+++ b/internal/bridge/cdp_keyboard.go
@@ -102,6 +102,25 @@ func editingCommand(key string, modifiers int) string {
return ""
}
+func w3cKeyName(key string) string {
+ if key == "Return" {
+ return "Enter"
+ }
+ return key
+}
+
+func dispatchNamedKeyEvent(ctx context.Context, key, eventType string) error {
+ params := map[string]any{"type": eventType, "key": w3cKeyName(key)}
+ if def, ok := namedKeyDefs[key]; ok {
+ params["code"] = def.code
+ params["windowsVirtualKeyCode"] = def.virtualKey
+ params["nativeVirtualKeyCode"] = def.virtualKey
+ }
+ return chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return chromedp.FromContext(ctx).Target.Execute(ctx, "Input.dispatchKeyEvent", params, nil)
+ }))
+}
+
// DispatchNamedKey dispatches key with the given CDP modifier bitmask
// (Alt=1, Ctrl=2, Meta=4, Shift=8). Unrecognised keys with no modifiers fall
// back to chromedp.KeyEvent (types the key); with modifiers they are dispatched
@@ -117,10 +136,7 @@ func DispatchNamedKey(ctx context.Context, key string, modifiers int) error {
return chromedp.Run(ctx, chromedp.KeyEvent(key))
}
- w3cKey := key
- if key == "Return" {
- w3cKey = "Enter"
- }
+ w3cKey := w3cKeyName(key)
// A named key with modifiers is a shortcut (Shift+ArrowRight selects,
// Ctrl+Backspace deletes a word): suppress its default text so it isn't
diff --git a/internal/bridge/cdp_keyboard_test.go b/internal/bridge/cdp_keyboard_test.go
index edaf4207..9a15011c 100644
--- a/internal/bridge/cdp_keyboard_test.go
+++ b/internal/bridge/cdp_keyboard_test.go
@@ -139,3 +139,17 @@ func TestDispatchNamedKey_KnownKeyOnCancelledCtx(t *testing.T) {
t.Error("expected error dispatching Enter on cancelled context")
}
}
+
+func TestW3CKeyName(t *testing.T) {
+ cases := map[string]string{
+ "Return": "Enter",
+ "Enter": "Enter",
+ "Shift": "Shift",
+ "a": "a",
+ }
+ for in, want := range cases {
+ if got := w3cKeyName(in); got != want {
+ t.Errorf("w3cKeyName(%q) = %q, want %q", in, got, want)
+ }
+ }
+}