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

This commit is contained in:
2026-01-09 11:21:02 +01:00
parent 45e8e87b35
commit a833be2af5
3 changed files with 60 additions and 33 deletions
+20 -10
View File
@@ -54,24 +54,34 @@ 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 {
case KeyPress: kb.SetKeys(input.Key)
err = kb.Press()
case KeyRelease:
err = kb.Release()
}
if err != nil { switch input.Action {
return fmt.Errorf("failed to run key event: %w", err) 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 return nil
+6 -2
View File
@@ -12,8 +12,12 @@ patch {
body:json { body:json {
{ {
"key": "VK_T", "inputs": [
"action": "press" {
"key": "VK_T",
"action": "press"
}
]
} }
} }
+34 -21
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 {
Key string `json:"key"` Inputs []struct {
Action string `json:"action"` Key string `json:"key"`
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[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] err := pkg.KeyboardInput(inputs)
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)
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{}{})
} }