diff --git a/display/pkg/main.go b/display/pkg/main.go index 5a3f697..834c71e 100644 --- a/display/pkg/main.go +++ b/display/pkg/main.go @@ -56,15 +56,31 @@ func OpenPresentation(path string) error { return nil } -func KeyboardInput(key int) error { +type KeyAction int + +const ( + KeyPress KeyAction = iota + KeyRelease +) + +func KeyboardInput(key int, action KeyAction) 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) - if err := kb.Launching(); err != nil { - return fmt.Errorf("failed to launch key event: %w", err) + switch action { + case KeyPress: + err = kb.Press() + case KeyRelease: + err = kb.Release() + } + + if err != nil { + return fmt.Errorf("failed to run key event: %w", err) } return nil diff --git a/display/web/main.go b/display/web/main.go index 6c9611e..502c649 100644 --- a/display/web/main.go +++ b/display/web/main.go @@ -15,6 +15,7 @@ import ( "path/filepath" shared "plg-mudics/shared" "strings" + "time" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -188,12 +189,16 @@ func shellCommandRoute(ctx echo.Context) error { func keyboardInputRoute(ctx echo.Context) error { var request struct { - Key string `json:"key"` + Key string `json:"key"` + Action string `json:"action"` } if err := ctx.Bind(&request); err != nil { slog.Error("Failed to parse keyboard input", "error", err) return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: badRequestDescription}) } + if request.Action != "press" && request.Action != "release" { + return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: badRequestDescription}) + } code, ok := pkg.KeyboardEvents[request.Key] if !ok { @@ -201,7 +206,15 @@ func keyboardInputRoute(ctx echo.Context) error { return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: fmt.Sprintf("Unsupported key: %s", request.Key)}) } - err := pkg.KeyboardInput(code) + 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 { slog.Error("Failed to send keyboard input", "key", request.Key, "error", err) return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to send keyboard input"}) @@ -365,10 +378,18 @@ func previewRoute(ctx echo.Context) error { // Reset previous file views so they dont collide with the new one func resetView() error { - err := pkg.KeyboardInput(keybd_event.VK_ESC) + var err error + + err = pkg.KeyboardInput(keybd_event.VK_ESC, pkg.KeyPress) if err != nil { return fmt.Errorf("failed to send ESC key: %w", err) } + time.Sleep(400 * time.Millisecond) + err = pkg.KeyboardInput(keybd_event.VK_ESC, pkg.KeyRelease) + if err != nil { + return fmt.Errorf("failed to send ESC key: %w", err) + } + sseConnection <- "" return nil