From 89ce7402dffdf8efaec34aa199b3a44c962fa84b Mon Sep 17 00:00:00 2001 From: 2mal3 <56305732+2mal3@users.noreply.github.com> Date: Fri, 9 Jan 2026 11:21:02 +0100 Subject: [PATCH] chore(display/keyboardInput): follow new docs [untested] --- display/pkg/main.go | 30 ++++++++++------ display/requests/keyboardInput.bru | 8 +++-- display/web/main.go | 55 ++++++++++++++++++------------ 3 files changed, 60 insertions(+), 33 deletions(-) diff --git a/display/pkg/main.go b/display/pkg/main.go index 99e4eec..16d8cfd 100644 --- a/display/pkg/main.go +++ b/display/pkg/main.go @@ -54,24 +54,34 @@ const ( KeyRelease ) -func KeyboardInput(key int, action KeyAction) error { +type Input struct { + Key int + Action KeyAction +} + +func KeyboardInput(inputs []Input) error { var err error kb, err := keybd_event.NewKeyBonding() if err != nil { return fmt.Errorf("failed to create key bonding: %w", err) } - kb.SetKeys(key) - switch action { - case KeyPress: - err = kb.Press() - case KeyRelease: - err = kb.Release() - } + for _, input := range inputs { + kb.SetKeys(input.Key) - if err != nil { - return fmt.Errorf("failed to run key event: %w", err) + switch input.Action { + case KeyPress: + err = kb.Press() + case KeyRelease: + err = kb.Release() + } + + if err != nil { + return fmt.Errorf("failed to run key event: %w", err) + } + + time.Sleep(time.Microsecond * 100) } return nil diff --git a/display/requests/keyboardInput.bru b/display/requests/keyboardInput.bru index 488d165..60c5f08 100644 --- a/display/requests/keyboardInput.bru +++ b/display/requests/keyboardInput.bru @@ -12,8 +12,12 @@ patch { body:json { { - "key": "VK_T", - "action": "press" + "inputs": [ + { + "key": "VK_T", + "action": "press" + } + ] } } diff --git a/display/web/main.go b/display/web/main.go index e6eefd6..6f0d74f 100644 --- a/display/web/main.go +++ b/display/web/main.go @@ -195,38 +195,51 @@ func shellCommandRoute(ctx echo.Context) error { func keyboardInputRoute(ctx echo.Context) error { var request struct { - Key string `json:"key"` - Action string `json:"action"` + Inputs []struct { + Key string `json:"key"` + Action string `json:"action"` + } `json:"inputs"` } if err := ctx.Bind(&request); err != nil { slog.Error("Failed to parse keyboard input", "error", err) return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: shared.BadRequestDescription}) } - if request.Action != "press" && request.Action != "release" { - return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: shared.BadRequestDescription}) + + var inputs []pkg.Input + + for _, input := range request.Inputs { + if input.Action != "press" && input.Action != "release" { + slog.Error("Invalid keyboard action", "action", input.Action) + return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: fmt.Sprintf("Invalid action: %s", input.Action)}) + } + + code, ok := pkg.KeyboardEvents[input.Key] + if !ok { + slog.Error("Unsupported key", "key", input.Key) + return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: fmt.Sprintf("Unsupported key: %s", input.Key)}) + } + + var action pkg.KeyAction + if input.Action == "press" { + action = pkg.KeyPress + } + if input.Action == "release" { + action = pkg.KeyRelease + } + + inputs = append(inputs, pkg.Input{ + Key: code, + Action: action, + }) } - code, ok := pkg.KeyboardEvents[request.Key] - if !ok { - slog.Error("Unsupported key", "key", request.Key) - return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: fmt.Sprintf("Unsupported key: %s", request.Key)}) - } - - var action pkg.KeyAction - if request.Action == "press" { - action = pkg.KeyPress - } - if request.Action == "release" { - action = pkg.KeyRelease - } - - err := pkg.KeyboardInput(code, action) + err := pkg.KeyboardInput(inputs) if err != nil { - slog.Error("Failed to send keyboard input", "key", request.Key, "error", err) + slog.Error("Failed to send keyboard input", "inputs", inputs, "error", err) return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to send keyboard input"}) } - slog.Info("Keyboard input sent", "key", request.Key) + slog.Info("Keyboard input sent") return ctx.JSON(http.StatusOK, struct{}{}) }