34
34
import android .util .Log ;
35
35
import android .view .SurfaceHolder ;
36
36
import android .view .SurfaceView ;
37
- import android .opengl .GLSurfaceView ;
38
37
import android .view .MotionEvent ;
39
38
import android .view .KeyEvent ;
39
+ import android .view .inputmethod .EditorInfo ;
40
+ import android .view .inputmethod .InputMethodManager ;
41
+ import android .view .inputmethod .InputConnection ;
42
+ import android .view .inputmethod .BaseInputConnection ;
43
+ import android .opengl .GLSurfaceView ;
40
44
import android .net .Uri ;
41
45
import android .os .PowerManager ;
42
46
53
57
54
58
55
59
public class SDLSurfaceView extends SurfaceView implements SurfaceHolder .Callback , Runnable {
56
- private static String TAG = "SDLSurface" ;
60
+ private static String TAG = "SDLSurface" ;
57
61
private final String mVertexShader =
58
62
"uniform mat4 uMVPMatrix;\n " +
59
63
"attribute vec4 aPosition;\n " +
@@ -463,6 +467,10 @@ public void onDestroy() {
463
467
Log .d (TAG , "onDestroy() app already leaved." );
464
468
return ;
465
469
}
470
+
471
+ // close the IME overlay(keyboard)
472
+ InputMethodManager inputMethodManager = (InputMethodManager )getContext ().getSystemService (Context .INPUT_METHOD_SERVICE );
473
+ inputMethodManager .hideSoftInputFromInputMethod (this .getWindowToken (), 0 );
466
474
467
475
// application didn't leave, give 10s before closing.
468
476
// hopefully, this could be enough for launching the on_stop() trigger within the app.
@@ -959,7 +967,7 @@ public boolean onKeyUp(int keyCode, final KeyEvent event) {
959
967
return super .onKeyUp (keyCode , event );
960
968
}
961
969
}
962
-
970
+
963
971
@ Override
964
972
public boolean onKeyMultiple (int keyCode , int count , KeyEvent event ){
965
973
String keys = event .getCharacters ();
@@ -972,18 +980,85 @@ public boolean onKeyMultiple(int keyCode, int count, KeyEvent event){
972
980
// but it my cause some odd behaviour
973
981
keyCode = 45 ;
974
982
}
975
-
976
- if (mInputActivated ){
983
+ if (mInputActivated && event .getAction () == KeyEvent .ACTION_MULTIPLE ){
977
984
keys .getChars (0 , keys .length (), keysBuffer , 0 );
985
+
978
986
for (char c : keysBuffer ){
979
987
//Log.i("python", "Char from multiply " + (int) c);
980
988
// Calls both up/down events to emulate key pressing
981
989
nativeKey (keyCode , 1 , (int ) c );
982
990
nativeKey (keyCode , 0 , (int ) c );
983
991
}
992
+ return true ;
993
+ }else {
994
+ return super .onKeyMultiple (keyCode , count , event );
984
995
}
985
-
986
- return true ;
996
+ }
997
+
998
+ @ Override
999
+ public boolean onKeyPreIme (int keyCode , final KeyEvent event ){
1000
+ //Log.i("python", String.format("key pre ime %d", keyCode));
1001
+ if (mInputActivated ){
1002
+ switch (event .getAction ()) {
1003
+ case KeyEvent .ACTION_DOWN :
1004
+ return onKeyDown (keyCode , event );
1005
+ case KeyEvent .ACTION_UP :
1006
+ return onKeyUp (keyCode , event );
1007
+ case KeyEvent .ACTION_MULTIPLE :
1008
+ return onKeyMultiple (
1009
+ keyCode ,
1010
+ event .getRepeatCount (),
1011
+ event );
1012
+ }
1013
+ return false ;
1014
+ }
1015
+ return super .onKeyPreIme (keyCode , event );
1016
+ }
1017
+
1018
+ @ Override
1019
+ public InputConnection onCreateInputConnection (EditorInfo outAttrs ) {
1020
+ // setting inputtype to TYPE_CLASS_TEXT is necessary for swiftkey to enable
1021
+ outAttrs .inputType = EditorInfo .TYPE_CLASS_TEXT ;
1022
+ // ask IME to avoid taking full screen on landscape mode
1023
+ outAttrs .imeOptions = EditorInfo .IME_FLAG_NO_EXTRACT_UI ;
1024
+ return new BaseInputConnection (this , false ){
1025
+
1026
+ private int mDelLen = 0 ;
1027
+
1028
+ private void deleteLastText (){
1029
+ // send back space keys
1030
+ for (int i = 0 ; i < this .mDelLen ; i ++){
1031
+ nativeKey (KeyEvent .KEYCODE_DEL , 1 , 23 );
1032
+ nativeKey (KeyEvent .KEYCODE_DEL , 0 , 23 );
1033
+ }
1034
+ }
1035
+
1036
+ @ Override
1037
+ public boolean setComposingText (CharSequence text ,
1038
+ int newCursorPosition ){
1039
+ //Log.i("Python:", String.format("set Composing Text %s", text));
1040
+ this .deleteLastText ();
1041
+ // send text
1042
+ for (int i = 0 ; i < text .length (); i ++){
1043
+ // Calls both up/down events to emulate key pressing
1044
+ char c = text .charAt (i );
1045
+ nativeKey (45 , 1 , (int ) c );
1046
+ nativeKey (45 , 0 , (int ) c );
1047
+ }
1048
+ // store len to be deleted for next time
1049
+ this .mDelLen = text .length ();
1050
+ return super .setComposingText (text , newCursorPosition );
1051
+ }
1052
+
1053
+ @ Override
1054
+ public boolean commitText (CharSequence text , int newCursorPosition ) {
1055
+ // some code which takes the input and manipulates it and calls editText.getText().replace() afterwards
1056
+ //Log.i("Python:", String.format("Commit Text %s", text));
1057
+ this .deleteLastText ();
1058
+ this .mDelLen = 0 ;
1059
+ return super .commitText (text , newCursorPosition );
1060
+ }
1061
+ };
987
1062
}
988
1063
989
1064
static void activateInput () {
0 commit comments