@@ -279,43 +279,40 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
279
279
is RimeMessage .KeyMessage ->
280
280
it.data.let event@{
281
281
val keyCode = it.value.keyCode
282
- if (keyCode != KeyEvent .KEYCODE_UNKNOWN ) {
283
- val eventTime = SystemClock .uptimeMillis()
284
- if (it.modifiers.release) {
285
- sendUpKeyEvent(eventTime, keyCode, it.modifiers.metaState)
286
- } else {
287
- // TODO: look for better workaround for this
288
- if (keyCode == KeyEvent .KEYCODE_ENTER ) {
289
- handleReturnKey()
290
- return
291
- }
292
-
293
- if (keyCode in KeyEvent .KEYCODE_NUMPAD_0 .. KeyEvent .KEYCODE_NUMPAD_EQUALS ) {
294
- // ignore KP_X keys, which is handled in `CommonKeyboardActionListener`.
295
- // Requires this empty body becoz Kotlin request it
296
- return
282
+ if (keyCode == KeyEvent .KEYCODE_UNKNOWN ) {
283
+ when {
284
+ ! it.modifiers.release && it.value.value > 0 -> {
285
+ runCatching {
286
+ commitText(" ${it.value.value.toChar()} " )
287
+ }
297
288
}
289
+ else -> Timber .w(" Unhandled Rime KeyEvent: $it " )
290
+ }
291
+ return
292
+ }
298
293
299
- it.modifiers.apply {
300
- if (this .shift) sendDownKeyEvent(eventTime, KeyEvent .KEYCODE_SHIFT_LEFT )
301
-
302
- sendDownKeyEvent(eventTime, keyCode, it.modifiers.metaState)
294
+ val eventTime = SystemClock .uptimeMillis()
295
+ if (it.modifiers.release) {
296
+ sendUpKeyEvent(eventTime, keyCode, it.modifiers.metaState)
297
+ return
298
+ }
303
299
304
- if (this .shift) sendUpKeyEvent(eventTime, KeyEvent .KEYCODE_SHIFT_LEFT )
300
+ // TODO: look for better workaround for this
301
+ if (keyCode == KeyEvent .KEYCODE_ENTER ) {
302
+ handleReturnKey()
303
+ return
304
+ }
305
305
306
- if (ctrl && keyCode == KeyEvent .KEYCODE_C ) cancelTextSelection()
307
- }
308
- }
309
- } else {
310
- if (! it.modifiers.release && it.value.value > 0 ) {
311
- try {
312
- commitText(Char (it.value.value).toString())
313
- } catch (_: Exception ) {
314
- }
315
- } else {
316
- Timber .w(" Unhandled Rime KeyEvent: $it " )
317
- }
306
+ if (keyCode in KeyEvent .KEYCODE_NUMPAD_0 .. KeyEvent .KEYCODE_NUMPAD_EQUALS ) {
307
+ // ignore KP_X keys, which is handled in `CommonKeyboardActionListener`.
308
+ // Requires this empty body becoz Kotlin request it
309
+ return
318
310
}
311
+
312
+ if (it.modifiers.shift) sendDownKeyEvent(eventTime, KeyEvent .KEYCODE_SHIFT_LEFT )
313
+ sendDownKeyEvent(eventTime, keyCode, it.modifiers.metaState)
314
+ if (it.modifiers.shift) sendUpKeyEvent(eventTime, KeyEvent .KEYCODE_SHIFT_LEFT )
315
+ if (it.modifiers.ctrl && keyCode == KeyEvent .KEYCODE_C ) clearTextSelection()
319
316
}
320
317
else -> {}
321
318
}
@@ -993,7 +990,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
993
990
if (et.selectionStart != et.selectionEnd) {
994
991
ic.performContextMenuAction(android.R .id.copy).also { result ->
995
992
if (result) {
996
- cancelTextSelection ()
993
+ clearTextSelection ()
997
994
}
998
995
return result
999
996
}
@@ -1052,7 +1049,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
1052
1049
return false
1053
1050
}
1054
1051
1055
- fun cancelTextSelection () {
1052
+ fun clearTextSelection () {
1056
1053
val ic = currentInputConnection ? : return
1057
1054
val etr = ExtractedTextRequest ().apply { token = 0 }
1058
1055
val et = currentInputConnection.getExtractedText(etr, 0 )
0 commit comments