Skip to content

Commit

Permalink
refactor: utilize scancode to improve key event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Nov 7, 2024
1 parent 1613c45 commit c940750
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
9 changes: 6 additions & 3 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,29 @@ class Rime :
override suspend fun processKey(
value: Int,
modifiers: UInt,
code: Int,
): Boolean =
withRimeContext {
processRimeKey(value, modifiers.toInt()).also {
if (it) {
ipcResponseCallback()
} else {
keyEventCallback(KeyValue(value), KeyModifiers(modifiers))
keyEventCallback(KeyValue(value), KeyModifiers(modifiers), code)
}
}
}

override suspend fun processKey(
value: KeyValue,
modifiers: KeyModifiers,
code: Int,
): Boolean =
withRimeContext {
processRimeKey(value.value, modifiers.toInt()).also {
if (it) {
ipcResponseCallback()
} else {
keyEventCallback(value, modifiers)
keyEventCallback(value, modifiers, code)
}
}
}
Expand Down Expand Up @@ -450,8 +452,9 @@ class Rime :
private fun keyEventCallback(
value: KeyValue,
modifiers: KeyModifiers,
code: Int,
) {
handleRimeEvent(RimeEvent.EventType.Key, RimeEvent.KeyEvent.Data(value, modifiers))
handleRimeEvent(RimeEvent.EventType.Key, RimeEvent.KeyEvent.Data(value, modifiers, code))
}

private fun <T> handleRimeEvent(
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ interface RimeApi {
suspend fun processKey(
value: Int,
modifiers: UInt = 0u,
code: Int = 0,
): Boolean

suspend fun processKey(
value: KeyValue,
modifiers: KeyModifiers,
code: Int = 0,
): Boolean

suspend fun selectCandidate(idx: Int): Boolean
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/osfans/trime/core/RimeEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sealed class RimeEvent<T>(
data class Data(
val value: KeyValue,
val modifiers: KeyModifiers,
val unicode: Int,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import com.osfans.trime.core.RimeEvent
import com.osfans.trime.core.RimeKeyMapping
import com.osfans.trime.core.RimeNotification
import com.osfans.trime.core.RimeProto
import com.osfans.trime.core.ScancodeMapping
import com.osfans.trime.daemon.RimeDaemon
import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.data.db.DraftHelper
Expand Down Expand Up @@ -302,8 +303,8 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
sendDownKeyEvent(eventTime, keyCode, it.modifiers.metaState)
}
} else {
if (it.modifiers.modifiers != KeyModifier.Release.modifier && it.value.value > 0) {
commitText(Char(it.value.value).toString())
if (it.modifiers.modifiers != KeyModifier.Release.modifier && it.unicode > 0) {
commitText(Char(it.unicode).toString())
} else {
Timber.w("Unhandled Rime KeyEvent: $it")
}
Expand Down Expand Up @@ -647,7 +648,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
0,
metaState,
KeyCharacterMap.VIRTUAL_KEYBOARD,
0,
ScancodeMapping.keyCodeToScancode(keyEventCode),
KeyEvent.FLAG_SOFT_KEYBOARD or KeyEvent.FLAG_KEEP_TOUCH_MODE,
InputDevice.SOURCE_KEYBOARD,
),
Expand All @@ -669,7 +670,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
0,
metaState,
KeyCharacterMap.VIRTUAL_KEYBOARD,
0,
ScancodeMapping.keyCodeToScancode(keyEventCode),
KeyEvent.FLAG_SOFT_KEYBOARD or KeyEvent.FLAG_KEEP_TOUCH_MODE,
InputDevice.SOURCE_KEYBOARD,
),
Expand Down Expand Up @@ -767,14 +768,14 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
val charCode = event.unicodeChar
if (charCode > 0 && charCode != '\t'.code) {
postRimeJob {
processKey(charCode, modifiers.modifiers)
processKey(charCode, modifiers.modifiers, event.scanCode)
}
return true
}
val keyVal = KeyValue.fromKeyEvent(event)
if (keyVal.value != RimeKeyMapping.RimeKey_VoidSymbol) {
postRimeJob {
processKey(keyVal, modifiers)
processKey(keyVal, modifiers, event.scanCode)
}
return true
}
Expand Down Expand Up @@ -832,9 +833,10 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
.takeIf { it != RimeKeyMapping.RimeKey_VoidSymbol }
?: Rime.getRimeKeycodeByName(Keycode.keyNameOf(keyEventCode))
val modifiers = KeyModifiers.fromMetaState(metaState).modifiers
val code = ScancodeMapping.keyCodeToScancode(keyEventCode)
var result = false
postRimeJob {
if (!processKey(value, modifiers)) {
if (!processKey(value, modifiers, code)) {
if (!hookKeyboard(keyEventCode, metaState)) {
if (!openCategory(keyEventCode)) {
result = false
Expand Down

0 comments on commit c940750

Please sign in to comment.