Skip to content

Commit 6d4aded

Browse files
committed
Merge pull request kivy#104 from kivy/1066
Swype Input Support
2 parents 222bdab + 4ec2d54 commit 6d4aded

File tree

1 file changed

+82
-7
lines changed

1 file changed

+82
-7
lines changed

src/src/org/renpy/android/SDLSurfaceView.java

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434
import android.util.Log;
3535
import android.view.SurfaceHolder;
3636
import android.view.SurfaceView;
37-
import android.opengl.GLSurfaceView;
3837
import android.view.MotionEvent;
3938
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;
4044
import android.net.Uri;
4145
import android.os.PowerManager;
4246

@@ -53,7 +57,7 @@
5357

5458

5559
public class SDLSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
56-
private static String TAG = "SDLSurface";
60+
private static String TAG = "SDLSurface";
5761
private final String mVertexShader =
5862
"uniform mat4 uMVPMatrix;\n" +
5963
"attribute vec4 aPosition;\n" +
@@ -463,6 +467,10 @@ public void onDestroy() {
463467
Log.d(TAG, "onDestroy() app already leaved.");
464468
return;
465469
}
470+
471+
// close the IME overlay(keyboard)
472+
InputMethodManager inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
473+
inputMethodManager.hideSoftInputFromInputMethod(this.getWindowToken(), 0);
466474

467475
// application didn't leave, give 10s before closing.
468476
// 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) {
959967
return super.onKeyUp(keyCode, event);
960968
}
961969
}
962-
970+
963971
@Override
964972
public boolean onKeyMultiple(int keyCode, int count, KeyEvent event){
965973
String keys = event.getCharacters();
@@ -972,18 +980,85 @@ public boolean onKeyMultiple(int keyCode, int count, KeyEvent event){
972980
// but it my cause some odd behaviour
973981
keyCode = 45;
974982
}
975-
976-
if (mInputActivated){
983+
if (mInputActivated && event.getAction() == KeyEvent.ACTION_MULTIPLE){
977984
keys.getChars(0, keys.length(), keysBuffer, 0);
985+
978986
for(char c: keysBuffer){
979987
//Log.i("python", "Char from multiply " + (int) c);
980988
// Calls both up/down events to emulate key pressing
981989
nativeKey(keyCode, 1, (int) c);
982990
nativeKey(keyCode, 0, (int) c);
983991
}
992+
return true;
993+
}else {
994+
return super.onKeyMultiple(keyCode, count, event);
984995
}
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+
};
9871062
}
9881063

9891064
static void activateInput() {

0 commit comments

Comments
 (0)