Proposal: input capture, so UI and ImGui can keep input from reaching the game
Is your feature request related to a problem? Please describe.
Game code polls input with Input.IsKeyDown, Input.IsMouseButtonPressed, Input.MouseDelta, gamePad.State, virtual buttons and gestures. Nothing tells that code that a UI layer has already used the input:
- A click on a Stride UI button also reaches a script's
IsMouseButtonPressed(MouseButton.Left). The UI can't prevent this, because UI hit-testing runs inUIRenderFeature.Draw, after scripts have run. - Typing in an
EditTextalso reachesIsKeyDown(Keys.W). The engine's own debug hotkeys fire as well: Ctrl+C+F12 inGame, K and L inStreamingManager, and the Bepu debug renderer's toggle. - A menu navigated with a gamepad also moves the character.
- A console or pause menu can't take a device away from the game.
- ImGui integrations have
WantCaptureMouseandWantCaptureKeyboard, but the engine gives them nothing to act on.
Describe the solution you'd like Owner-based capture of input devices, with game-facing reads of a captured device reporting no input.
input.TryCapture(input.Keyboard, owner, InputCapturePriority.Focus); // text box gets focus
input.Release(input.Keyboard, owner); // text box loses focus
input.TryCapturePointer(touchDevice, pointerId, owner); // finger pressed on a UI joystickWhere the capture state lives. Each device has a
DeviceCaptureState, exposed through a newIInputDevice.CaptureStateproperty. The property has a default interface implementation, so existing third-party devices keep compiling.Masking is written once. It goes into the shared query methods (
IsKeyDown,IsButtonDown, …), so it applies to every device.InputManager's aggregated state (MouseDelta,MouseWheelDelta, event lists), gestures and virtual buttons also skip captured input.- The engine device base classes mask their raw state as well, for example
gamePad.State.
The owner still sees everything. The owner reads raw input through the existing event listeners, which run before masking.
Conflicts use priorities. There are named levels: hover, focus and modal. A higher priority takes the device, and an equal or lower one fails.
Pointer capture. On touch devices a single pointer can be captured, so a finger on a UI joystick doesn't mask a second finger dragging the camera.
No stuck or phantom keys.
- When a capture begins, the game sees held keys and buttons as released.
- When it ends, a key still held doesn't count as pressed until it's pressed again.
Stride UI adopts it.
- UI hit-testing moves before scripts, using the last drawn layout.
- The UI captures the pointer while hovering or dragging.
EditTextcaptures the keyboard while focused.- The debug hotkeys, gestures and virtual buttons are covered through the shared queries, with no per-call-site edits.
An ImGui integration needs about two lines per frame:
if (io.WantCaptureMouse) input.TryCapture(input.Mouse, this); else input.Release(input.Mouse, this); if (io.WantCaptureKeyboard) input.TryCapture(input.Keyboard, this, InputCapturePriority.Focus); else input.Release(input.Keyboard, this);Settings. Masking is on by default. It can be turned off in code with
InputManager.MaskCapturedInput, or in the game settings with a newInputsection.
Describe alternatives you've considered
- Query flags only, like ImGui's
WantCapture*:InputManagerexposesIsPointerCaptured/IsKeyboardCaptured, and game code checks them. It's small and changes no behaviour. But every script, virtual button, gesture and engine hotkey would have to remember to check, so the problem stays. - Input layers with masked views. Layers such as debug, UI and game each claim input and read it through their own filtered view. This is the most general option, but it has the largest API, and scripts'
Inputwould have to become a per-layer view. None of the cases above needs claims finer than a device or a pointer. The proposal above can grow into layers later if needed. - Masking only in
InputManager. Gamepads are mostly read on the device (GetGamePadByIndex(...),DefaultGamePad.State), so gamepad capture would leak.
Additional context
- Behaviour change. With masking on by default, games that relied on clicks passing through UI will behave differently. Release notes would cover this and point to the opt-outs.
- Editor impact. The UI editor reads mouse and keyboard input over the UI it's editing, so its game instances would turn masking off. The scene editor and gizmos would be checked with masking on.
- Third-party devices. A device that implements the interfaces directly is masked through the query methods and
InputManager. Its own raw properties are masked only if it checksCaptureState.IsMasked. - Documentation. The input manual needs a section on capture and the opt-outs.
- Implementation. I'd deliver this as one PR, test-first, with headless tests on simulated devices plus UI game tests. #3495 covers allocation fixes on the same input paths, as groundwork.
Feedback welcome, especially on the default-on masking and on the priority model.
Source: stride3d/stride