allow remapping of unknown keycodes (#189)

This commit is contained in:
Felix Nüsse 2024-07-15 19:16:03 +02:00 committed by GitHub
parent 93025c95f2
commit eafb03ad38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 4 deletions

View File

@ -194,8 +194,7 @@ class EmulationActivity : AppCompatActivity() {
return false return false
} }
val button = val button = preferences.getInt(InputBindingSetting.getInputButtonKey(event), event.scanCode)
preferences.getInt(InputBindingSetting.getInputButtonKey(event.keyCode), event.keyCode)
val action: Int = when (event.action) { val action: Int = when (event.action) {
KeyEvent.ACTION_DOWN -> { KeyEvent.ACTION_DOWN -> {
// On some devices, the back gesture / button press is not intercepted by androidx // On some devices, the back gesture / button press is not intercepted by androidx

View File

@ -224,8 +224,10 @@ class InputBindingSetting(
Toast.makeText(context, R.string.input_message_analog_only, Toast.LENGTH_LONG).show() Toast.makeText(context, R.string.input_message_analog_only, Toast.LENGTH_LONG).show()
return return
} }
writeButtonMapping(getInputButtonKey(keyEvent.keyCode))
val uiString = "${keyEvent.device.name}: Button ${keyEvent.keyCode}" val code = translateEventToKeyId(keyEvent)
writeButtonMapping(getInputButtonKey(code))
val uiString = "${keyEvent.device.name}: Button $code"
value = uiString value = uiString
} }
@ -285,9 +287,17 @@ class InputBindingSetting(
/** /**
* Helper function to get the settings key for an gamepad button. * Helper function to get the settings key for an gamepad button.
*
*/ */
@Deprecated("Use the new getInputButtonKey(keyEvent) method to handle unknown keys")
fun getInputButtonKey(keyCode: Int): String = "${INPUT_MAPPING_PREFIX}_HostAxis_${keyCode}" fun getInputButtonKey(keyCode: Int): String = "${INPUT_MAPPING_PREFIX}_HostAxis_${keyCode}"
/**
* Helper function to get the settings key for an gamepad button.
*
*/
fun getInputButtonKey(event: KeyEvent): String = "${INPUT_MAPPING_PREFIX}_HostAxis_${translateEventToKeyId(event)}"
/** /**
* Helper function to get the settings key for an gamepad axis. * Helper function to get the settings key for an gamepad axis.
*/ */
@ -303,5 +313,23 @@ class InputBindingSetting(
*/ */
fun getInputAxisOrientationKey(axis: Int): String = fun getInputAxisOrientationKey(axis: Int): String =
"${getInputAxisKey(axis)}_GuestOrientation" "${getInputAxisKey(axis)}_GuestOrientation"
/**
* This function translates a keyEvent into an "keyid"
* This key id is either the keyCode from the event, or
* the raw scanCode.
* Only when the keyCode itself is 0, (so it is an unknown key)
* we fall back to the raw scan code.
* This handles keys like the media-keys on google statia-controllers
* that don't have a conventional "mapping" and report as "unknown"
*/
fun translateEventToKeyId(event: KeyEvent): Int {
return if (event.keyCode == 0) {
event.scanCode
} else {
event.keyCode
}
}
} }
} }