45
45
import android .opengl .GLSurfaceView ;
46
46
import android .net .Uri ;
47
47
import android .os .PowerManager ;
48
+ import android .os .Handler ;
48
49
import android .content .pm .PackageManager ;
49
50
import android .content .pm .ApplicationInfo ;
50
51
import android .graphics .PixelFormat ;
@@ -1058,32 +1059,26 @@ public boolean onKeyUp(int keyCode, final KeyEvent event) {
1058
1059
1059
1060
@ Override
1060
1061
public boolean onKeyMultiple (int keyCode , int count , KeyEvent event ){
1062
+ String keys = event .getCharacters ();
1061
1063
if (DEBUG )
1062
1064
Log .d (TAG , String .format (
1063
- "onKeyMultiple() keyCode=%d count=%d" , keyCode , count ));
1064
- String keys = event .getCharacters ();
1065
+ "onKeyMultiple() keyCode=%d count=%d, keys=%s" , keyCode , count , keys ));
1065
1066
char [] keysBuffer = new char [keys .length ()];
1066
1067
if (mDelLen > 0 ){
1067
1068
mDelLen = 0 ;
1068
1069
return true ;
1069
1070
}
1070
1071
if (keyCode == 0 ){
1071
- // FIXME: here is hardcoed value of "q" key
1072
+ // FIXME: here is hardcoded value of "q" key
1072
1073
// on hacker's keyboard. It is passed to
1073
1074
// nativeKey function to get it worked if
1074
1075
// we get 9 and some non-ascii characters
1075
1076
// but it my cause some odd behaviour
1076
1077
keyCode = 45 ;
1077
1078
}
1078
1079
if (mInputActivated && event .getAction () == KeyEvent .ACTION_MULTIPLE ){
1079
- keys .getChars (0 , keys .length (), keysBuffer , 0 );
1080
-
1081
- for (char c : keysBuffer ){
1082
- //Log.i("python", "Char from multiply " + (int) c);
1083
- // Calls both up/down events to emulate key pressing
1084
- nativeKey (keyCode , 1 , (int ) c );
1085
- nativeKey (keyCode , 0 , (int ) c );
1086
- }
1080
+ String message = String .format ("INSERTN:%s" , keys );
1081
+ dispatchCommand (message );
1087
1082
return true ;
1088
1083
}else {
1089
1084
return super .onKeyMultiple (keyCode , count , event );
@@ -1110,6 +1105,50 @@ public boolean onKeyPreIme(int keyCode, final KeyEvent event){
1110
1105
return super .onKeyPreIme (keyCode , event );
1111
1106
}
1112
1107
1108
+ public void delayed_message (String message , int delay ){
1109
+ Handler handler = new Handler ();
1110
+ final String msg = message ;
1111
+ final int d = delay ;
1112
+ handler .postDelayed (new Runnable (){
1113
+ @ Override
1114
+ public void run (){
1115
+ dispatchCommand (msg );
1116
+ }
1117
+ }, d );
1118
+ }
1119
+
1120
+ public void dispatchCommand (String message ){
1121
+
1122
+ Boolean ret = false ;
1123
+ int delay = 0 ;
1124
+ while (message .length () > 50 ){
1125
+ delayed_message (message .substring (0 , 50 ), delay );
1126
+ delay += 100 ;
1127
+ message = String .format ("INSERTN:%s" , message .substring (50 , message .length ()));
1128
+ if (message .length () <= 50 ){
1129
+ delayed_message (message , delay +50 );
1130
+ return ;
1131
+ }
1132
+ }
1133
+
1134
+ if (DEBUG ) Log .d (TAG , String .format ("dispatch :%s" , message ));
1135
+ int keyCode = 45 ;
1136
+ //send control sequence start \x01
1137
+ nativeKey (keyCode , 1 , 1 );
1138
+ nativeKey (keyCode , 0 , 1 );
1139
+
1140
+ for (int i =0 ; i < message .length (); i ++){
1141
+ //Calls both up/down events to emulate key pressing
1142
+ nativeKey (keyCode , 1 , (int ) message .charAt (i ));
1143
+ nativeKey (keyCode , 0 , (int ) message .charAt (i ));
1144
+ }
1145
+
1146
+ //send control sequence start \x01
1147
+ nativeKey (keyCode , 1 , 2 );
1148
+ nativeKey (keyCode , 0 , 2 );
1149
+
1150
+ }
1151
+
1113
1152
@ Override
1114
1153
public InputConnection onCreateInputConnection (EditorInfo outAttrs ) {
1115
1154
// setting inputtype to TYPE_CLASS_TEXT is necessary for swiftkey to enable
@@ -1118,27 +1157,28 @@ public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
1118
1157
outAttrs .imeOptions = EditorInfo .IME_FLAG_NO_EXTRACT_UI ;
1119
1158
return new BaseInputConnection (this , false ){
1120
1159
1121
-
1122
1160
private void deleteLastText (){
1123
1161
// send back space keys
1124
- for (int i = 0 ; i < mDelLen ; i ++){
1125
- nativeKey (KeyEvent .KEYCODE_DEL , 1 , 23 );
1126
- nativeKey (KeyEvent .KEYCODE_DEL , 0 , 23 );
1162
+ if (DEBUG ){
1163
+ Log .i ("Python:" , String .format ("delete text%s" , mDelLen ));
1164
+ }
1165
+
1166
+ if (mDelLen == 0 ){
1167
+ return ;
1127
1168
}
1169
+
1170
+ String message = String .format ("DEL:%s" , mDelLen );
1171
+ dispatchCommand (message );
1128
1172
}
1129
1173
1130
1174
@ Override
1131
1175
public boolean setComposingText (CharSequence text ,
1132
1176
int newCursorPosition ){
1133
- if (DEBUG ) Log .i ("Python:" , String .format ("set Composing Text %s" , text ));
1134
1177
this .deleteLastText ();
1178
+ if (DEBUG ) Log .i ("Python:" , String .format ("set Composing Text %s" , text ));
1135
1179
// send text
1136
- for (int i = 0 ; i < text .length (); i ++){
1137
- // Calls both up/down events to emulate key pressing
1138
- char c = text .charAt (i );
1139
- nativeKey (45 , 1 , (int ) c );
1140
- nativeKey (45 , 0 , (int ) c );
1141
- }
1180
+ String message = String .format ("INSERT:%s" , text );
1181
+ dispatchCommand (message );
1142
1182
// store len to be deleted for next time
1143
1183
mDelLen = text .length ();
1144
1184
return super .setComposingText (text , newCursorPosition );
@@ -1147,8 +1187,8 @@ public boolean setComposingText(CharSequence text,
1147
1187
@ Override
1148
1188
public boolean commitText (CharSequence text , int newCursorPosition ) {
1149
1189
// some code which takes the input and manipulates it and calls editText.getText().replace() afterwards
1150
- if (DEBUG ) Log .i ("Python:" , String .format ("Commit Text %s" , text ));
1151
1190
this .deleteLastText ();
1191
+ if (DEBUG ) Log .i ("Python:" , String .format ("Commit Text %s" , text ));
1152
1192
mDelLen = 0 ;
1153
1193
return super .commitText (text , newCursorPosition );
1154
1194
}
@@ -1168,6 +1208,17 @@ public boolean setComposingRegion(int start, int end){
1168
1208
@ Override
1169
1209
public boolean deleteSurroundingText (int beforeLength , int afterLength ){
1170
1210
if (DEBUG ) Log .d ("Python:" , String .format ("deleteLastText %s %s" , beforeLength , afterLength ));
1211
+ // move cursor to place from where to start deleting
1212
+ // send right arrow keys
1213
+ for (int i = 0 ; i < afterLength ; i ++){
1214
+ nativeKey (45 , 1 , 39 );
1215
+ nativeKey (45 , 0 , 39 );
1216
+ }
1217
+ // remove text before cursor
1218
+ mDelLen = beforeLength + afterLength ;
1219
+ this .deleteLastText ();
1220
+ mDelLen = 0 ;
1221
+
1171
1222
return super .deleteSurroundingText (beforeLength , afterLength );
1172
1223
}
1173
1224
0 commit comments