Skip to content

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

refactor: fold keydown/keyup dispatch into a shared helper

Luigi Agosti63803f42ce35
diff --git a/internal/bridge/action_text.go b/internal/bridge/action_text.go
index 2a1e7bf5..0510932f 100644
--- a/internal/bridge/action_text.go
+++ b/internal/bridge/action_text.go
@@ -297,30 +297,19 @@ func (b *Bridge) actionKeyboardInsert(ctx context.Context, req ActionRequest) (m
}
func (b *Bridge) actionKeyDown(ctx context.Context, req ActionRequest) (map[string]any, error) {
- if req.Key == "" {
- return nil, fmt.Errorf("key required for keydown")
- }
- err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- params := map[string]any{"type": "keyDown", "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 {
- return nil, err
- }
- return map[string]any{"keydown": req.Key}, nil
+ return b.dispatchSingleKeyEvent(ctx, req, "keyDown", ActionKeyDown)
}
func (b *Bridge) actionKeyUp(ctx context.Context, req ActionRequest) (map[string]any, error) {
+ return b.dispatchSingleKeyEvent(ctx, req, "keyUp", ActionKeyUp)
+}
+
+func (b *Bridge) dispatchSingleKeyEvent(ctx context.Context, req ActionRequest, eventType, resultKey string) (map[string]any, error) {
if req.Key == "" {
- return nil, fmt.Errorf("key required for keyup")
+ 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": "keyUp", "key": req.Key}
+ params := map[string]any{"type": eventType, "key": req.Key}
if def, ok := namedKeyDefs[req.Key]; ok {
params["code"] = def.code
params["windowsVirtualKeyCode"] = def.virtualKey
@@ -331,5 +320,5 @@ func (b *Bridge) actionKeyUp(ctx context.Context, req ActionRequest) (map[string
if err != nil {
return nil, err
}
- return map[string]any{"keyup": req.Key}, nil
+ return map[string]any{resultKey: req.Key}, nil
}