mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-05 16:37:09 +00:00
chore(display/keyboardInput): follow new docs [untested]
This commit is contained in:
+20
-10
@@ -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
|
||||
|
||||
@@ -12,8 +12,12 @@ patch {
|
||||
|
||||
body:json {
|
||||
{
|
||||
"key": "VK_T",
|
||||
"action": "press"
|
||||
"inputs": [
|
||||
{
|
||||
"key": "VK_T",
|
||||
"action": "press"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+34
-21
@@ -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{}{})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user