Alex Styl
12/08/2024, 12:04 PMAlex Styl
12/08/2024, 12:18 PMfun isCharacterKey(event: KeyEvent): Boolean {
val char = event.key.toString().replace("Key: ", "")
return char.length == 1 && event.key !in listOf(
Key.MetaLeft, Key.MetaRight,
Key.Escape, Key.Delete, Key.Backspace,
Key.Tab, Key.AltLeft, Key.AltRight
)
}
Alex Styl
12/09/2024, 4:04 AMkeyChar
(instead of the replace trick i do above) by using the actual platform.
On desktop you get it by awtEventOrNull!!.keyChar
. haven't checked for others
PS2: Desktop also has a KeyEvent.isTypedEvent
which i think does exactly what i want, but need this on web tooAlex Styl
12/09/2024, 4:34 AMexpect val KeyEvent.isTypedEvent: Boolean
expect val KeyEvent.typedChar: String
// Desktop
actual val KeyEvent.isTypedEvent: Boolean
get() {
return this.isTypedEvent
}
actual val KeyEvent.typedChar: String
get() = awtEventOrNull!!.keyChar.toString()
// Web
actual val KeyEvent.isTypedEvent: Boolean
get() {
return this.typedChar.length == 1
}
actual val KeyEvent.typedChar: String
get() {
return domEventOrNull!!.key
}
Alex Styl
12/10/2024, 5:34 AMawsEventOrNull.isTypedEvent
will only return true if the character is combined with the appropriate key type (key down, etc). so the above won't work on Desktop unless you use it on a TextField.
Just a headsup for whoever reads this in the futureTrejkaz
12/19/2024, 2:23 AMTrejkaz
12/19/2024, 2:23 AMTrejkaz
12/19/2024, 2:26 AMTrejkaz
12/19/2024, 2:27 AMTrejkaz
12/19/2024, 2:27 AMtypedChar.length > 0
?