mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-11 16:29:48 +07:00
Fix Ctrl-Letter key bindings (#6232)
This commit is contained in:
@ -66,6 +66,13 @@ data class KeyCharAndCode(val char: Char, val code: Int) {
|
|||||||
/** mini-factory for control codes - case insensitive */
|
/** mini-factory for control codes - case insensitive */
|
||||||
fun ctrl(letter: Char) = KeyCharAndCode(Char(letter.code and 31), 0)
|
fun ctrl(letter: Char) = KeyCharAndCode(Char(letter.code and 31), 0)
|
||||||
|
|
||||||
|
/** mini-factory for control codes from keyCodes */
|
||||||
|
fun ctrlFromCode(keyCode: Int): KeyCharAndCode {
|
||||||
|
val name = Input.Keys.toString(keyCode)
|
||||||
|
if (name.length != 1 || !name[0].isLetter()) return KeyCharAndCode(Char.MIN_VALUE, keyCode)
|
||||||
|
return ctrl(name[0])
|
||||||
|
}
|
||||||
|
|
||||||
/** mini-factory for KeyCharAndCode values to be compared by character, not by code */
|
/** mini-factory for KeyCharAndCode values to be compared by character, not by code */
|
||||||
fun ascii(char: Char) = KeyCharAndCode(char.lowercaseChar(), 0)
|
fun ascii(char: Char) = KeyCharAndCode(char.lowercaseChar(), 0)
|
||||||
|
|
||||||
@ -164,31 +171,31 @@ class KeyPressDispatcher(val name: String? = null) : HashMap<KeyCharAndCode, (()
|
|||||||
* @param checkIgnoreKeys An optional lambda - when it returns true all keys are ignored
|
* @param checkIgnoreKeys An optional lambda - when it returns true all keys are ignored
|
||||||
*/
|
*/
|
||||||
fun install(stage: Stage, checkIgnoreKeys: (() -> Boolean)? = null) {
|
fun install(stage: Stage, checkIgnoreKeys: (() -> Boolean)? = null) {
|
||||||
|
if (consoleLog)
|
||||||
|
println("$this: install")
|
||||||
if (installStage != null) uninstall()
|
if (installStage != null) uninstall()
|
||||||
listener =
|
listener =
|
||||||
object : InputListener() {
|
object : InputListener() {
|
||||||
override fun keyDown(event: InputEvent?, keycode: Int): Boolean {
|
override fun keyDown(event: InputEvent?, keycode: Int): Boolean {
|
||||||
/*
|
|
||||||
: Boolean {
|
|
||||||
return super.keyDown(event, keycode)
|
|
||||||
}(event: InputEvent?, character: Char)
|
|
||||||
*/
|
|
||||||
|
|
||||||
// look for both key code and ascii entries - ascii first as the
|
|
||||||
// Char constructor of KeyCharAndCode generates keyCode based instances
|
|
||||||
// preferentially but we would miss Ctrl- combos otherwise
|
|
||||||
val key = when {
|
val key = when {
|
||||||
event == null ->
|
event == null ->
|
||||||
KeyCharAndCode.UNKNOWN
|
KeyCharAndCode.UNKNOWN
|
||||||
|
Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT) ||Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT) ->
|
||||||
|
KeyCharAndCode.ctrlFromCode(event.keyCode)
|
||||||
else ->
|
else ->
|
||||||
KeyCharAndCode(event.keyCode)
|
KeyCharAndCode(event.keyCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// see if we want to handle this key, and if not, let it propagate
|
// see if we want to handle this key, and if not, let it propagate
|
||||||
if (!contains(key) || (checkIgnoreKeys?.invoke() == true))
|
if (!contains(key) || (checkIgnoreKeys?.invoke() == true)) {
|
||||||
|
if (consoleLog)
|
||||||
|
println("${this@KeyPressDispatcher}: $key not handled")
|
||||||
return super.keyDown(event, keycode)
|
return super.keyDown(event, keycode)
|
||||||
|
}
|
||||||
|
|
||||||
// try-catch mainly for debugging. Breakpoints in the vicinity can make the event fire twice in rapid succession, second time the context can be invalid
|
// try-catch mainly for debugging. Breakpoints in the vicinity can make the event fire twice in rapid succession, second time the context can be invalid
|
||||||
|
if (consoleLog)
|
||||||
|
println("${this@KeyPressDispatcher}: handling $key")
|
||||||
try {
|
try {
|
||||||
this@KeyPressDispatcher[key]?.invoke()
|
this@KeyPressDispatcher[key]?.invoke()
|
||||||
} catch (ex: Exception) {}
|
} catch (ex: Exception) {}
|
||||||
@ -216,8 +223,12 @@ class KeyPressDispatcher(val name: String? = null) : HashMap<KeyCharAndCode, (()
|
|||||||
if (listener == null || installStage == null) return
|
if (listener == null || installStage == null) return
|
||||||
if (listenerInstalled && (isEmpty() || isPaused || forceRemove)) {
|
if (listenerInstalled && (isEmpty() || isPaused || forceRemove)) {
|
||||||
listenerInstalled = false
|
listenerInstalled = false
|
||||||
|
if (consoleLog)
|
||||||
|
println("$this: removeListener")
|
||||||
installStage!!.removeListener(listener)
|
installStage!!.removeListener(listener)
|
||||||
} else if (!listenerInstalled && !(isEmpty() || isPaused)) {
|
} else if (!listenerInstalled && !(isEmpty() || isPaused)) {
|
||||||
|
if (consoleLog)
|
||||||
|
println("$this: addListener")
|
||||||
installStage!!.addListener(listener)
|
installStage!!.addListener(listener)
|
||||||
listenerInstalled = true
|
listenerInstalled = true
|
||||||
}
|
}
|
||||||
@ -231,6 +242,9 @@ class KeyPressDispatcher(val name: String? = null) : HashMap<KeyCharAndCode, (()
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
// Control debug logging
|
||||||
|
private const val consoleLog = false
|
||||||
|
|
||||||
/** Tests presence of a physical keyboard - static here as convenience shortcut only */
|
/** Tests presence of a physical keyboard - static here as convenience shortcut only */
|
||||||
val keyboardAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.HardwareKeyboard)
|
val keyboardAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.HardwareKeyboard)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user