chore(display/keyboardInput): follow new docs [untested]

This commit is contained in:
2026-01-09 11:21:02 +01:00
parent 45e8e87b35
commit 89ce7402df
3 changed files with 60 additions and 33 deletions
+13 -3
View File
@@ -54,16 +54,23 @@ const (
KeyRelease KeyRelease
) )
func KeyboardInput(key int, action KeyAction) error { type Input struct {
Key int
Action KeyAction
}
func KeyboardInput(inputs []Input) error {
var err error var err error
kb, err := keybd_event.NewKeyBonding() kb, err := keybd_event.NewKeyBonding()
if err != nil { if err != nil {
return fmt.Errorf("failed to create key bonding: %w", err) return fmt.Errorf("failed to create key bonding: %w", err)
} }
kb.SetKeys(key)
switch action { for _, input := range inputs {
kb.SetKeys(input.Key)
switch input.Action {
case KeyPress: case KeyPress:
err = kb.Press() err = kb.Press()
case KeyRelease: case KeyRelease:
@@ -74,6 +81,9 @@ func KeyboardInput(key int, action KeyAction) error {
return fmt.Errorf("failed to run key event: %w", err) return fmt.Errorf("failed to run key event: %w", err)
} }
time.Sleep(time.Microsecond * 100)
}
return nil return nil
} }
+4
View File
@@ -11,10 +11,14 @@ patch {
} }
body:json { body:json {
{
"inputs": [
{ {
"key": "VK_T", "key": "VK_T",
"action": "press" "action": "press"
} }
]
}
} }
settings { settings {
+23 -10
View File
@@ -195,38 +195,51 @@ func shellCommandRoute(ctx echo.Context) error {
func keyboardInputRoute(ctx echo.Context) error { func keyboardInputRoute(ctx echo.Context) error {
var request struct { var request struct {
Inputs []struct {
Key string `json:"key"` Key string `json:"key"`
Action string `json:"action"` Action string `json:"action"`
} `json:"inputs"`
} }
if err := ctx.Bind(&request); err != nil { if err := ctx.Bind(&request); err != nil {
slog.Error("Failed to parse keyboard input", "error", err) slog.Error("Failed to parse keyboard input", "error", err)
return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: shared.BadRequestDescription}) 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[request.Key] code, ok := pkg.KeyboardEvents[input.Key]
if !ok { if !ok {
slog.Error("Unsupported key", "key", request.Key) slog.Error("Unsupported key", "key", input.Key)
return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: fmt.Sprintf("Unsupported key: %s", request.Key)}) return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: fmt.Sprintf("Unsupported key: %s", input.Key)})
} }
var action pkg.KeyAction var action pkg.KeyAction
if request.Action == "press" { if input.Action == "press" {
action = pkg.KeyPress action = pkg.KeyPress
} }
if request.Action == "release" { if input.Action == "release" {
action = pkg.KeyRelease action = pkg.KeyRelease
} }
err := pkg.KeyboardInput(code, action) inputs = append(inputs, pkg.Input{
Key: code,
Action: action,
})
}
err := pkg.KeyboardInput(inputs)
if err != nil { 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"}) 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{}{}) return ctx.JSON(http.StatusOK, struct{}{})
} }