Skip to content

Commit da0b7db

Browse files
committed
#1990 fix: Passthrough the device id of the trigger to the key event action if one is not manually specified
1 parent 9c9dcaf commit da0b7db

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- [#1971](https://github.com/keymapperorg/KeyMapper/issues/1971) Media actions work again in some apps, like YouTube.
1818
- [#1961](https://github.com/keymapperorg/KeyMapper/issues/1961) Disabling setup assistant shows a notification asking for pairing code immediately.
1919
- [#1983](https://github.com/keymapperorg/KeyMapper/issues/1983) Inputting a modifier key and another key as actions through Expert mode applies the correct key character map.
20+
- [#1990](https://github.com/keymapperorg/KeyMapper/issues/1990) Passthrough the device id of the trigger to the key event action if one is not manually specified
2021
- Bugs with expert mode auto starting time.
2122

2223
## [4.0.0 Beta 6](https://github.com/sds100/KeyMapper/releases/tag/v4.0.0-beta.06)

base/src/main/java/io/github/sds100/keymapper/base/actions/PerformActionTriggerDevice.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@ sealed class PerformActionTriggerDevice {
99
*/
1010
data class Evdev(val deviceId: Int) : PerformActionTriggerDevice()
1111

12+
/**
13+
* The action was triggered by an Android InputDevice.
14+
*/
15+
data class AndroidDevice(val deviceId: Int) : PerformActionTriggerDevice()
16+
1217
data object Default : PerformActionTriggerDevice()
1318
}

base/src/main/java/io/github/sds100/keymapper/base/actions/PerformKeyEventActionDelegate.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class PerformKeyEventActionDelegate(
4747
return injectEvdevEvent(inputEventAction, triggerDevice.deviceId, action)
4848
}
4949

50-
val deviceId: Int = getDeviceIdForKeyEventAction(action)
50+
val deviceId: Int = getAndroidDeviceIdForKeyEventAction(triggerDevice, action)
5151

5252
// If the device that the user specified in the action can not be found
5353
// then fallback to evdev injection.
@@ -157,7 +157,10 @@ class PerformKeyEventActionDelegate(
157157
}
158158
}
159159

160-
private fun getDeviceIdForKeyEventAction(action: ActionData.InputKeyEvent): Int {
160+
private fun getAndroidDeviceIdForKeyEventAction(
161+
triggerDevice: PerformActionTriggerDevice,
162+
action: ActionData.InputKeyEvent,
163+
): Int {
161164
if (action.device?.descriptor == null) {
162165
// automatically select a game controller as the input device for game controller key events
163166

@@ -171,7 +174,10 @@ class PerformKeyEventActionDelegate(
171174
}
172175
}
173176

174-
return 0
177+
return when (triggerDevice) {
178+
is PerformActionTriggerDevice.AndroidDevice -> triggerDevice.deviceId
179+
else -> 0
180+
}
175181
}
176182

177183
val inputDevices = devicesAdapter.connectedInputDevices.value

base/src/main/java/io/github/sds100/keymapper/base/detection/KeyMapAlgorithm.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ class KeyMapAlgorithm(
287287

288288
val triggerActions = mutableListOf<IntArray>()
289289
val triggerConstraints = mutableListOf<Array<ConstraintState>>()
290-
val triggerPerformActionDevices = mutableListOf<PerformActionTriggerDevice>()
291290

292291
val sequenceTriggerActionPerformers =
293292
mutableMapOf<Int, SequenceTriggerActionPerformer>()
@@ -1995,8 +1994,11 @@ class KeyMapAlgorithm(
19951994

19961995
private fun AlgoEvent.performActionDevice(): PerformActionTriggerDevice {
19971996
return when (this) {
1998-
is EvdevEventAlgo -> PerformActionTriggerDevice.Evdev(deviceId)
1999-
else -> PerformActionTriggerDevice.Default
1997+
is EvdevEventAlgo -> PerformActionTriggerDevice.Evdev(this.deviceId)
1998+
is KeyEventAlgo -> PerformActionTriggerDevice.AndroidDevice(this.deviceId)
1999+
is AssistantEvent -> PerformActionTriggerDevice.Default
2000+
is FingerprintGestureEvent -> PerformActionTriggerDevice.Default
2001+
is FloatingButtonEvent -> PerformActionTriggerDevice.Default
20002002
}
20012003
}
20022004
}

base/src/test/java/io/github/sds100/keymapper/base/actions/PerformKeyEventActionDelegateTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,36 @@ class PerformKeyEventActionDelegateTest {
6868
)
6969
}
7070

71+
@Test
72+
fun `use trigger device id if no device specified for action`() = runTest(testDispatcher) {
73+
val action = ActionData.InputKeyEvent(
74+
keyCode = KeyEvent.KEYCODE_A,
75+
device = null,
76+
)
77+
78+
delegate.perform(
79+
action,
80+
inputEventAction = InputEventAction.DOWN,
81+
keyMetaState = 0,
82+
triggerDevice = PerformActionTriggerDevice.AndroidDevice(deviceId = 3),
83+
)
84+
85+
val expectedDownEvent = InjectKeyEventModel(
86+
keyCode = KeyEvent.KEYCODE_A,
87+
action = KeyEvent.ACTION_DOWN,
88+
metaState = 0,
89+
deviceId = 3,
90+
scanCode = 0,
91+
repeatCount = 0,
92+
source = InputDevice.SOURCE_KEYBOARD,
93+
)
94+
95+
verify(mockInputEventHub).injectKeyEvent(
96+
expectedDownEvent,
97+
useSystemBridgeIfAvailable = false,
98+
)
99+
}
100+
71101
@Test
72102
fun `inject evdev event if action device set as a non-evdev device but it is disconnected`() =
73103
runTest(testDispatcher) {

0 commit comments

Comments
 (0)