From 9e05e4c00c56d7593caa13fe4df15222dbb58412 Mon Sep 17 00:00:00 2001 From: Dennis Zabolotny Date: Wed, 18 Nov 2026 16:53:39 +0200 Subject: [PATCH] fix(input): keep held keyboard keys when pointer capture ends (#2779) * fix(input): keep held keyboard keys when pointer capture ends When a game gives up its pointer lock, Android pointer capture is released and the session dropped every held input, including keyboard keys. In a windowed session with Relative mouse on Auto, holding W to run and releasing the mouse button after steering stopped the character, because the key press is never sent again. Losing pointer capture now releases only the mouse buttons, the pen and the mouse back-button input. Keys are still released when the window loses focus. * fix(input): release held keys when pointer capture ends for the session menu Opening the session menu releases pointer capture, and the menu then consumes key events, including the key-up. A key held at that moment would stay pressed in the guest after the menu closed. Capture loss with the menu open now releases keys as before; other capture loss keeps them. * fix(input): release held keys whenever another surface takes the keyboard Capture loss released keys only while the session menu was open. The other places that take input from the guest (on-screen keyboard, achievements, input settings, WoW chat, touch editor, picture in picture) have the same problem, so keys are now kept only when the session still owns input and the game itself dropped its pointer lock. * fix(input): release held keys when the session menu opens While the menu is open it takes every key event, including the key-up, so a key held when it opened stayed pressed in the guest. The keys are now released when the menu opens, whether or not pointer capture was active. --------- Co-authored-by: zdennisz <15849186+zdennisz@users.noreply.github.com> --- .../com/droiddeck/launcher/SessionActivity.kt | 27 +++++++++++--- .../launcher/input/SessionInputPointerTest.kt | 36 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index e7c5c38e77..5a95f1de4d 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -488,7 +488,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } drawerLayerOpen = false drawerPinned = false - if (value) drawerDocked = dockDrawer() && openDockedMenu() + if (value) { + releaseHeldKeys() + drawerDocked = dockDrawer() && openDockedMenu() + } drawerOpenState = value if (!value && drawerDocked) closeDockedMenu() } @@ -825,7 +828,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde pointerCaptureRefused = false if (usbMouse.active) updatePointerCapture() } else { - releaseHeldGuestInput() + releaseHeldInputOnCaptureLoss() uiHandler.postDelayed(::updatePointerCapture, 250) } } @@ -5118,10 +5121,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private fun relativeMouseAttached(): Boolean = !isInMultiWindowMode && !glassesOutputActive && mouseAttached() + private fun sessionOwnsInput() = + resumed && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !inputSettingsOpen && + !pipUi && !bottomOutputActive && !touchEditorOpen && !wowChatOnTop + private fun updatePointerCapture() { if (!::surfaceView.isInitialized) return val view = window.decorView - val want = (pointerLocked || relativeMouseAttached()) && resumed && hasWindowFocus() && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !inputSettingsOpen && !pipUi && !bottomOutputActive && !touchEditorOpen && !wowChatOnTop && mouseAttached() + val want = (pointerLocked || relativeMouseAttached()) && sessionOwnsInput() && hasWindowFocus() && mouseAttached() releaseDesktopMouseGrab() updateUsbMouse(want) if (want == view.hasPointerCapture()) return @@ -5444,14 +5451,26 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private fun keyboardCoversPad() = SessionState.mode == SessionService.MODE_STEAM && AgentGuest.steamKeyboard - private fun releaseHeldGuestInput() { + private fun releaseHeldInputOnCaptureLoss() { + if (sessionOwnsInput()) releaseHeldPointerInput() else releaseHeldGuestInput() + } + + private fun releaseHeldPointerInput() { mouseBackInput.cancel() cancelPen(releaseKey = true) heldButtons.release(1 or 2 or 8 or 16) + } + + private fun releaseHeldKeys() { guestKeysDown.toList().forEach { sendGuestKey(it, false) } heldKeys.release(8 or 16 or 32) } + private fun releaseHeldGuestInput() { + releaseHeldPointerInput() + releaseHeldKeys() + } + private fun sendGuestKey(key: Int, down: Boolean, owner: Int = 1) { if (down && pipUi) return val code = if (key == KEY_RIGHTCTRL && SessionState.mode == SessionService.MODE_DESKTOP) KEY_LEFTCTRL else key diff --git a/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt index 835eca2e9b..879c18233c 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt @@ -186,6 +186,42 @@ class SessionInputPointerTest { assertEquals(29 to 0, CompositorShadow.keys.last()) } + @Test fun losingPointerCaptureReleasesMouseButtonsButKeepsKeyboardKeysHeld() { + ReflectionHelpers.getField>(activity, "resumed\$delegate").value = true + val keys = ReflectionHelpers.getField(activity, "heldKeys") + val buttons = ReflectionHelpers.getField(activity, "heldButtons") + output.key(17, true) + keys.set(8, 30, true) + buttons.set(2, 272, true) + CompositorShadow.keys.clear() + CompositorShadow.events.clear() + ReflectionHelpers.callInstanceMethod(activity, "releaseHeldInputOnCaptureLoss") + assertTrue(CompositorShadow.keys.none { it.second == 0 }) + assertTrue(CompositorShadow.events.contains(Triple(3, 272, 0))) + output.key(17, false) + assertEquals(17 to 0, CompositorShadow.keys.last()) + } + + @Test fun losingPointerCaptureToAnotherInputSurfaceReleasesKeyboardKeys() { + ReflectionHelpers.getField>(activity, "resumed\$delegate").value = true + for (state in listOf("drawerOpenState", "wowChatOnTop")) { + val open: androidx.compose.runtime.MutableState = ReflectionHelpers.getField(activity, state + "\$delegate") + open.value = true + ReflectionHelpers.getField(activity, "heldKeys").set(16, 17, true) + CompositorShadow.keys.clear() + ReflectionHelpers.callInstanceMethod(activity, "releaseHeldInputOnCaptureLoss") + assertEquals(state, 17 to 0, CompositorShadow.keys.last()) + open.value = false + } + } + + @Test fun openingTheSessionMenuReleasesHeldKeyboardKeys() { + ReflectionHelpers.getField(activity, "heldKeys").set(16, 17, true) + CompositorShadow.keys.clear() + ReflectionHelpers.callInstanceMethod(activity, "setDrawerOpen", ReflectionHelpers.ClassParameter.from(Boolean::class.javaPrimitiveType, true)) + assertEquals(17 to 0, CompositorShadow.keys.last()) + } + @Test fun horizontalWheelUsesTheSameDirectionInBothMouseBackends() { output.scrollHorizontal(2) assertEquals(listOf(Triple(11, 0, 240)), CompositorShadow.events)