Skip to content

Commit

Permalink
android: Fix softkeyboard issue in SDL on Android.
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 26, 2017
1 parent 9bbf92e commit 8e37bed
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
31 changes: 17 additions & 14 deletions android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
Expand Up @@ -1248,6 +1248,9 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getSource() & InputDevice.SOURCE_KEYBOARD) != 0) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
//Log.v("SDL", "key down: " + keyCode);
if (SDLActivity.isTextInputEvent(event)) {
SDLInputConnection.nativeCommitText(String.valueOf((char) event.getUnicodeChar()), 1);
}
SDLActivity.onNativeKeyDown(keyCode);
return true;
}
Expand Down Expand Up @@ -1435,6 +1438,7 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (SDLActivity.isTextInputEvent(event)) {
ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1);
return true;
}
SDLActivity.onNativeKeyDown(keyCode);
return true;
Expand Down Expand Up @@ -1484,26 +1488,23 @@ public SDLInputConnection(View targetView, boolean fullEditor) {
@Override
public boolean sendKeyEvent(KeyEvent event) {
/*
* This handles the keycodes from soft keyboard (and IME-translated input from hardkeyboard)
* This used to handle the keycodes from soft keyboard (and IME-translated input from hardkeyboard)
* However, as of Ice Cream Sandwich and later, almost all soft keyboard doesn't generate key presses
* and so we need to generate them ourselves in commitText. To avoid duplicates on the handful of keys
* that still do, we empty this out.
*/
int keyCode = event.getKeyCode();
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (SDLActivity.isTextInputEvent(event)) {
commitText(String.valueOf((char) event.getUnicodeChar()), 1);
}
SDLActivity.onNativeKeyDown(keyCode);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {
SDLActivity.onNativeKeyUp(keyCode);
return true;
}
return super.sendKeyEvent(event);
}

@Override
public boolean commitText(CharSequence text, int newCursorPosition) {

nativeCommitText(text.toString(), newCursorPosition);
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
nativeGenerateScancodeForUnichar(c);
}

SDLInputConnection.nativeCommitText(text.toString(), newCursorPosition);

return super.commitText(text, newCursorPosition);
}
Expand All @@ -1516,7 +1517,9 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) {
return super.setComposingText(text, newCursorPosition);
}

public native void nativeCommitText(String text, int newCursorPosition);
public static native void nativeCommitText(String text, int newCursorPosition);

public native void nativeGenerateScancodeForUnichar(char c);

public native void nativeSetComposingText(String text, int newCursorPosition);

Expand Down
32 changes: 32 additions & 0 deletions src/core/android/SDL_android.c
Expand Up @@ -31,6 +31,8 @@
#include "SDL_android.h"
#include <EGL/egl.h>

#include "keyinfotable.h"

#include "../../events/SDL_events_c.h"
#include "../../video/android/SDL_androidkeyboard.h"
#include "../../video/android/SDL_androidmouse.h"
Expand Down Expand Up @@ -749,6 +751,36 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(
(*env)->ReleaseStringUTFChars(env, text, utftext);
}

JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeGenerateScancodeForUnichar)(
JNIEnv* env, jclass cls,
jchar chUnicode)
{
SDL_Scancode code = SDL_SCANCODE_UNKNOWN;
uint16_t mod = 0;

// We do not care about bigger than 127.
if (chUnicode < 127) {
AndroidKeyInfo info = unicharToAndroidKeyInfoTable[chUnicode];
code = info.code;
mod = info.mod;
}

if (mod & KMOD_SHIFT) {
/* If character uses shift, press shift down */
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
}

/* send a keydown and keyup even for the character */
SDL_SendKeyboardKey(SDL_PRESSED, code);
SDL_SendKeyboardKey(SDL_RELEASED, code);

if (mod & KMOD_SHIFT) {
/* If character uses shift, press shift back up */
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
}
}


JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeSetComposingText)(
JNIEnv* env, jclass cls,
jstring text, jint newCursorPosition)
Expand Down

0 comments on commit 8e37bed

Please sign in to comment.