fix(input): mouse right click no longer acts as Back in REDMAGIC cast mode

REDMAGIC's cast mode replaces a mouse's right button with a Back key that
names no device and lacks the virtual-hard-key flag that the navigation
bar and back gesture set. During a session, a Back from a mouse, or that
injected form while a mouse is connected, now presses the guest's right
button. The navigation bar, back gesture and controller Back still act
as Back, and so does a right click while a DroidDeck menu is open.

A mouse's Back and Forward buttons no longer also send a left click;
Android delivers them as key events.
This commit is contained in:
Omar Al Taki authored and Kurt Himebauch committed 2026-11-05 12:13:51 -05:00
1 parent bae4ecd867
commit 57b3623a9a
3 files changed
+172 -17

No files matched your search

@@ -0,0 +1,39 @@
package com.droiddeck.launcher.input
import android.view.InputDevice
import android.view.KeyCharacterMap
import android.view.KeyEvent
/**
* Some firmware turns a mouse's right button into Back. Most report it from the mouse itself; REDMAGIC's
* cast mode injects it from no device and without the virtual-hard-key flag that the navigation bar and
* back gesture set, so that form only counts while a mouse is connected.
*/
class MouseBackInput(private val sendRight: (Boolean) -> Unit, private val mouseAttached: () -> Boolean) {
private val held = mutableSetOf<Int>()
fun onKeyEvent(event: KeyEvent, toGuest: Boolean): Boolean {
if (event.keyCode != KeyEvent.KEYCODE_BACK) return false
val device = event.deviceId
if (device in held) {
if (event.action == KeyEvent.ACTION_UP) {
held.remove(device)
sendRight(false)
}
return true
}
if (event.action != KeyEvent.ACTION_DOWN || event.repeatCount > 0 || !toGuest) return false
if (!isFromMouse(event) && !(isInjectedBack(event) && mouseAttached())) return false
held.add(device)
sendRight(true)
return true
}
companion object {
fun isFromMouse(event: KeyEvent): Boolean =
event.isFromSource(InputDevice.SOURCE_MOUSE) || event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE)
fun isInjectedBack(event: KeyEvent): Boolean =
event.deviceId == KeyCharacterMap.VIRTUAL_KEYBOARD && (event.flags and KeyEvent.FLAG_VIRTUAL_HARD_KEY) == 0
}
}