@@ -2,6 +2,7 @@ package com.osfans.trime.ime.core
2
2
3
3
import android.inputmethodservice.InputMethodService
4
4
import android.os.SystemClock
5
+ import android.text.TextUtils
5
6
import android.view.InputDevice
6
7
import android.view.KeyCharacterMap
7
8
import android.view.KeyEvent
@@ -126,6 +127,28 @@ class EditorInstance(private val ims: InputMethodService) {
126
127
return ic.getTextBeforeCursor(n.coerceAtMost(1024 ), 0 )?.toString() ? : " "
127
128
}
128
129
130
+ /* * 獲得當前漢字:候選字、選中字、剛上屏字/光標前字/光標前所有字、光標後所有字
131
+ * %s或者%1$s爲當前字符
132
+ * %2$s爲當前輸入的編碼
133
+ * %3$s爲光標前字符
134
+ * %4$s爲光標前所有字符
135
+ * */
136
+ fun getActiveText (type : Int ): String {
137
+ if (type == 2 ) return Rime .RimeGetInput () // 當前編碼
138
+ var s = Rime .getComposingText() // 當前候選
139
+ if (TextUtils .isEmpty(s)) {
140
+ val ic = inputConnection
141
+ var cs = if (ic != null ) ic.getSelectedText(0 ) else null // 選中字
142
+ if (type == 1 && TextUtils .isEmpty(cs)) cs = lastCommittedText // 剛上屏字
143
+ if (TextUtils .isEmpty(cs) && ic != null ) {
144
+ cs = ic.getTextBeforeCursor(if (type == 4 ) 1024 else 1 , 0 ) // 光標前字
145
+ }
146
+ if (TextUtils .isEmpty(cs) && ic != null ) cs = ic.getTextAfterCursor(1024 , 0 ) // 光標後面所有字
147
+ if (cs != null ) s = cs.toString()
148
+ }
149
+ return s
150
+ }
151
+
129
152
/* *
130
153
* Constructs a meta state integer flag which can be used for setting the `metaState` field when sending a KeyEvent
131
154
* to the input connection. If this method is called without a meta modifier set to true, the default value `0` is
0 commit comments