Skip to content

Commit

Permalink
refactor: tell key processing API if the system key event is ACTION_UP
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Nov 10, 2024
1 parent c940750 commit 2c9cbfb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
15 changes: 6 additions & 9 deletions app/src/main/java/com/osfans/trime/core/KeyModifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,12 @@ value class KeyModifiers(
fun fromKeyEvent(event: KeyEvent): KeyModifiers {
var states = KeyModifier.None.modifier
event.apply {
if (action == KeyEvent.ACTION_UP) {
states += KeyModifier.Release
} else {
if (isAltPressed) states += KeyModifier.Alt
if (isCtrlPressed) states += KeyModifier.Control
if (isShiftPressed) states += KeyModifier.Shift
if (isCapsLockOn) states += KeyModifier.Lock
if (isMetaPressed) states += KeyModifier.Meta
}
if (isAltPressed) states += KeyModifier.Alt
if (isCtrlPressed) states += KeyModifier.Control
if (isShiftPressed) states += KeyModifier.Shift
if (isCapsLockOn) states += KeyModifier.Lock
if (isMetaPressed) states += KeyModifier.Meta
if (action == KeyEvent.ACTION_UP) states += KeyModifier.Release
}
return KeyModifiers(states)
}
Expand Down
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 @@ -78,13 +78,14 @@ class Rime :
value: Int,
modifiers: UInt,
code: Int,
up: Boolean,
): Boolean =
withRimeContext {
processRimeKey(value, modifiers.toInt()).also {
if (it) {
ipcResponseCallback()
} else {
keyEventCallback(KeyValue(value), KeyModifiers(modifiers), code)
keyEventCallback(KeyValue(value), KeyModifiers(modifiers), code, up)
}
}
}
Expand All @@ -93,13 +94,14 @@ class Rime :
value: KeyValue,
modifiers: KeyModifiers,
code: Int,
up: Boolean,
): Boolean =
withRimeContext {
processRimeKey(value.value, modifiers.toInt()).also {
if (it) {
ipcResponseCallback()
} else {
keyEventCallback(value, modifiers, code)
keyEventCallback(value, modifiers, code, up)
}
}
}
Expand Down Expand Up @@ -453,8 +455,9 @@ class Rime :
value: KeyValue,
modifiers: KeyModifiers,
code: Int,
up: Boolean,
) {
handleRimeEvent(RimeEvent.EventType.Key, RimeEvent.KeyEvent.Data(value, modifiers, code))
handleRimeEvent(RimeEvent.EventType.Key, RimeEvent.KeyEvent.Data(value, modifiers, code, up))
}

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 @@ -23,12 +23,14 @@ interface RimeApi {
value: Int,
modifiers: UInt = 0u,
code: Int = 0,
up: Boolean = false,
): Boolean

suspend fun processKey(
value: KeyValue,
modifiers: KeyModifiers,
code: Int = 0,
up: Boolean = false,
): 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 @@ -31,6 +31,7 @@ sealed class RimeEvent<T>(
val value: KeyValue,
val modifiers: KeyModifiers,
val unicode: Int,
val up: Boolean,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.text.InputType
import android.view.InputDevice
import android.view.KeyCharacterMap
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.Window
Expand All @@ -32,7 +33,6 @@ import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import com.osfans.trime.BuildConfig
import com.osfans.trime.R
import com.osfans.trime.core.KeyModifier
import com.osfans.trime.core.KeyModifiers
import com.osfans.trime.core.KeyValue
import com.osfans.trime.core.Rime
Expand Down Expand Up @@ -297,13 +297,13 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
val keyCode = it.value.keyCode
if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
val eventTime = SystemClock.uptimeMillis()
if (it.modifiers.modifiers == KeyModifier.Release.modifier) {
if (it.up) {
sendUpKeyEvent(eventTime, keyCode, it.modifiers.metaState)
} else {
sendDownKeyEvent(eventTime, keyCode, it.modifiers.metaState)
}
} else {
if (it.modifiers.modifiers != KeyModifier.Release.modifier && it.unicode > 0) {
if (!it.up && it.unicode > 0) {
commitText(Char(it.unicode).toString())
} else {
Timber.w("Unhandled Rime KeyEvent: $it")
Expand Down Expand Up @@ -764,18 +764,19 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}

private fun forwardKeyEvent(event: KeyEvent): Boolean {
val up = event.action == MotionEvent.ACTION_UP
val modifiers = KeyModifiers.fromKeyEvent(event)
val charCode = event.unicodeChar
if (charCode > 0 && charCode != '\t'.code) {
postRimeJob {
processKey(charCode, modifiers.modifiers, event.scanCode)
processKey(charCode, modifiers.modifiers, event.scanCode, up)
}
return true
}
val keyVal = KeyValue.fromKeyEvent(event)
if (keyVal.value != RimeKeyMapping.RimeKey_VoidSymbol) {
postRimeJob {
processKey(keyVal, modifiers, event.scanCode)
processKey(keyVal, modifiers, event.scanCode, up)
}
return true
}
Expand Down

0 comments on commit 2c9cbfb

Please sign in to comment.