Skip to content

Auto Toggle Mouse/Keyboard Capture on Hover or Focus #1726

@dmiyamasu

Description

@dmiyamasu

Summary:

Add an option to automatically toggle mouse and keyboard capture when the Moonlight window gains or loses focus — ideally triggered by hover or active window detection.

Use Case:

Currently, interacting with the Moonlight UI in windowed mode requires two clicks: one to focus the window, and another to toggle input capture. This disrupts the user experience, especially when multitasking across monitors or switching between apps. A more seamless input model would improve usability and reduce friction.

Proposed Behavior:

  • When the Moonlight window is hovered or gains focus, automatically enable mouse/keyboard capture.
  • When the window loses focus or the cursor exits the window, automatically release input capture.
  • Optionally, expose this behavior as a toggle in settings for users who prefer manual control.
    Benefits:
  • Smoother transitions between Moonlight and other apps.
  • Reduces reliance on manual shortcut (Ctrl + Alt + Shift + Z) or multiple clicks.
  • Improves accessibility and ergonomics for multi-monitor setups.

🧠 Technical Notes

Context
Moonlight uses SDL2 for cross-platform input and window management. SDL’s input capture model is intentionally conservative to avoid hijacking user input without consent. However, other open-source projects have implemented seamless input capture based on window focus or hover using SDL or native APIs.

References to Similar Implementations

  • Parsec: Implements seamless input capture and release based on window focus. While proprietary, its behavior is often cited as a usability benchmark.
  • Barrier (GitHub): Dynamically redirects mouse/keyboard input across systems based on cursor position using native OS APIs.
  • RustDesk (GitHub): Uses platform-specific hooks to detect focus and cursor events for automatic input capture.
  • Godot Engine (GitHub): Uses SDL but supplements it with native macOS APIs to manage mouse capture and release based on window focus and mouse enter/leave events.

macOS API References

  • focus and notifications
  • for detecting mouse enter/exit events
  • for low-level input monitoring (used in Barrier and Synergy)

🛠️ Suggested Implementation Path

  • Add a Settings Toggle
    Introduce a user-facing setting in the Moonlight macOS client: “Auto-capture input on focus or hover”
  • Detect Focus or Hover Events
    • Use NSWindow delegate methods (windowDidBecomeKey, windowDidResignKey) to detect focus changes.
    • Optionally, use NSTrackingArea to detect mouse enter/exit events for hover-based capture.
    • Trigger SDL Input Capture
    • On focus/hover: call SDL_CaptureMouse(SDL_TRUE) and SDL_SetRelativeMouseMode(SDL_TRUE)
    • On unfocus/unhover: call SDL_CaptureMouse(SDL_FALSE) and SDL_SetRelativeMouseMode(SDL_FALSE)
  • Fallback Behavior
    • If hover-based toggling proves unreliable, fallback to focus-based toggling, which is more consistent across platforms.
  • Respect Manual Override
    • Ensure that manual toggling via Ctrl + Alt + Shift + Z still works and overrides auto-capture temporarily.

Environment:

  • macOS (tested on Ventura and Sonoma)
  • Moonlight client version: 6.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions