fix(input): digital trigger keys work again on pads that also list analog axes (#2291)

Since #2205 a pad that lists an analog trigger axis had its L2/R2 keys
ignored. Handhelds whose triggers can be switched to digital keep listing
the axis but only send the keys in that mode, so the triggers stopped
responding.

A trigger key now presses its trigger when the axis is idle at that
moment and holds it until the key is released; motion events with an
idle axis keep it held. As soon as the axis reports a value it takes
over again, so pads that send both (the #2205 case) still follow their
analog axis and key repeats never change the value.
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-11 20:56:19 -05:00
1 parent f4503c2eda
commit 65554a544c
2 files changed
+69 -14

No files matched your search

@@ -56,6 +56,7 @@ public final class PadBridge {
private final FakeInputWriter[] playerWriters = new FakeInputWriter[SLOTS];
private final PadState[] playerStates = new PadState[SLOTS];
private final float[][] rawSticks = new float[SLOTS][4];
private final boolean[][] keyTriggers = new boolean[SLOTS][2];
private final int[] retainedSticks = new int[SLOTS];
private final PadState effectiveState = new PadState();
private final PadState homeGuideReport = new PadState();
@@ -407,11 +408,11 @@ public final class PadBridge {
break;
case KeyEvent.KEYCODE_BUTTON_L2:
pad.leftTrigger = keyTrigger(pad.leftTrigger, pressed, event.getRepeatCount(),
analogTrigger(event.getDevice(), MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE));
analogTrigger(event.getDevice(), MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE), keyTriggers[slot], 0);
break;
case KeyEvent.KEYCODE_BUTTON_R2:
pad.rightTrigger = keyTrigger(pad.rightTrigger, pressed, event.getRepeatCount(),
analogTrigger(event.getDevice(), MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS));
analogTrigger(event.getDevice(), MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS), keyTriggers[slot], 1);
break;
case KeyEvent.KEYCODE_DPAD_UP: pad.up = pressed; break;
case KeyEvent.KEYCODE_DPAD_RIGHT: pad.right = pressed; break;
@@ -471,6 +472,7 @@ public final class PadBridge {
stateFor(slot).clear();
retainedSticks[slot] = 0;
Arrays.fill(rawSticks[slot], 0f);
Arrays.fill(keyTriggers[slot], false);
}
private void mutateState(java.util.function.Consumer<PadState> mutation) {
@@ -525,11 +527,11 @@ public final class PadBridge {
float lt = 0f;
float rt = 0f;
if (wired || hasAxis(device, source, MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE)) {
lt = leftTrigger(event, wired);
lt = axisTrigger(leftTrigger(event, wired), keyTriggers[slot], 0);
pad.leftTrigger = lt;
}
if (wired || hasAxis(device, source, MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS)) {
rt = rightTrigger(event, wired);
rt = axisTrigger(rightTrigger(event, wired), keyTriggers[slot], 1);
pad.rightTrigger = rt;
}
if (hasAxis(device, source, MotionEvent.AXIS_HAT_X)) {
@@ -553,9 +555,27 @@ public final class PadBridge {
return true;
}
static float keyTrigger(float current, boolean pressed, int repeatCount, boolean analog) {
if (analog || repeatCount > 0) return current;
return pressed ? 1f : 0f;
static float keyTrigger(float current, boolean pressed, int repeatCount, boolean analog, boolean[] held, int side) {
if (repeatCount > 0) return current;
if (!analog) {
held[side] = pressed;
return pressed ? 1f : 0f;
}
if (pressed) {
if (current != 0f) return current;
held[side] = true;
return 1f;
}
if (!held[side]) return current;
held[side] = false;
return 0f;
}
static float axisTrigger(float value, boolean[] held, int side) {
if (!held[side]) return value;
if (value == 0f) return 1f;
held[side] = false;
return value;
}
static boolean analogTrigger(InputDevice device, int... axes) {
@@ -126,11 +126,11 @@ class PadPartialAxesTest {
@Test fun wiredXboxTriggerKeysDoNotOverrideTheirAxes() {
val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ, wired = true)
motion(device, MotionEvent.AXIS_Z to 0f, MotionEvent.AXIS_RZ to -1f)
motion(device, MotionEvent.AXIS_Z to 0f, MotionEvent.AXIS_RZ to -0.6f)
key(device, KeyEvent.KEYCODE_BUTTON_L2)
key(device, KeyEvent.KEYCODE_BUTTON_R2)
assertEquals(0.5f, pad().leftTrigger)
assertEquals(0f, pad().rightTrigger)
assertEquals(0.2f, pad().rightTrigger, 0.0001f)
}
@Test fun digitalTriggerKeysStillPressAndRelease() {
@@ -148,11 +148,46 @@ class PadPartialAxesTest {
}
@Test fun keyTriggerDecision() {
assertEquals(1f, PadBridge.keyTrigger(0f, true, 0, false))
assertEquals(0f, PadBridge.keyTrigger(1f, false, 0, false))
assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 1, false))
assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 0, true))
assertEquals(0.25f, PadBridge.keyTrigger(0.25f, false, 0, true))
val held = BooleanArray(2)
assertEquals(1f, PadBridge.keyTrigger(0f, true, 0, false, held, 0))
assertEquals(0f, PadBridge.keyTrigger(1f, false, 0, false, held, 0))
assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 1, false, held, 0))
assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 0, true, held, 0))
assertEquals(0.25f, PadBridge.keyTrigger(0.25f, false, 0, true, held, 0))
assertEquals(1f, PadBridge.keyTrigger(0f, true, 0, true, held, 1))
assertEquals(1f, PadBridge.axisTrigger(0f, held, 1))
assertEquals(0f, PadBridge.keyTrigger(1f, false, 0, true, held, 1))
assertEquals(0f, PadBridge.axisTrigger(0f, held, 1))
}
@Test fun silentTriggerAxesLetTheirKeysPress() {
val device = device(MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_RTRIGGER)
motion(device)
key(device, KeyEvent.KEYCODE_BUTTON_L2)
key(device, KeyEvent.KEYCODE_BUTTON_R2)
assertEquals(1f, pad().leftTrigger)
assertEquals(1f, pad().rightTrigger)
motion(device, MotionEvent.AXIS_X to 0.8f)
key(device, KeyEvent.KEYCODE_BUTTON_L2, repeat = 1)
assertEquals(1f, pad().leftTrigger)
assertEquals(1f, pad().rightTrigger)
key(device, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.ACTION_UP)
assertEquals(0f, pad().leftTrigger)
assertEquals(1f, pad().rightTrigger)
key(device, KeyEvent.KEYCODE_BUTTON_R2, KeyEvent.ACTION_UP)
assertEquals(0f, pad().rightTrigger)
}
@Test fun aLiveAxisTakesOverFromItsKey() {
val device = device(MotionEvent.AXIS_LTRIGGER)
key(device, KeyEvent.KEYCODE_BUTTON_L2)
assertEquals(1f, pad().leftTrigger)
motion(device, MotionEvent.AXIS_LTRIGGER to 0.4f)
assertEquals(0.4f, pad().leftTrigger)
motion(device, MotionEvent.AXIS_LTRIGGER to 0f)
assertEquals(0f, pad().leftTrigger)
key(device, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.ACTION_UP)
assertEquals(0f, pad().leftTrigger)
}
private fun pad(): PadState = requireNotNull(JoyConPadTest.RecordingWriter.pads[0])