Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Android input routing logic when using a hardware keyboard #80932

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.godotengine.godot.*;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
Expand Down Expand Up @@ -209,6 +210,13 @@ public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) {
mRenderView.getView().requestFocus();
}

// When a hardware keyboard is connected, all key events come through so we can route them
// directly to the engine.
// This is not the case when using a soft keyboard, requiring extra processing from this class.
if (hasHardwareKeyboard()) {
return mRenderView.getInputHandler().onKeyDown(keyCode, keyEvent);
}

// pass event to godot in special cases
if (needHandlingInGodot(keyCode, keyEvent) && mRenderView.getInputHandler().onKeyDown(keyCode, keyEvent)) {
return true;
Expand All @@ -219,6 +227,13 @@ public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) {

@Override
public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {
// When a hardware keyboard is connected, all key events come through so we can route them
// directly to the engine.
// This is not the case when using a soft keyboard, requiring extra processing from this class.
if (hasHardwareKeyboard()) {
return mRenderView.getInputHandler().onKeyUp(keyCode, keyEvent);
}

if (needHandlingInGodot(keyCode, keyEvent) && mRenderView.getInputHandler().onKeyUp(keyCode, keyEvent)) {
return true;
} else {
Expand All @@ -235,10 +250,20 @@ private boolean needHandlingInGodot(int keyCode, KeyEvent keyEvent) {
isModifiedKey;
}

boolean hasHardwareKeyboard() {
Configuration config = getResources().getConfiguration();
return config.keyboard != Configuration.KEYBOARD_NOKEYS &&
config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
}

// ===========================================================
// Methods
// ===========================================================
public void showKeyboard(String p_existing_text, VirtualKeyboardType p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
if (hasHardwareKeyboard()) {
return;
}

int maxInputLength = (p_max_input_length <= 0) ? Integer.MAX_VALUE : p_max_input_length;
if (p_cursor_start == -1) { // cursor position not given
this.mOriginText = p_existing_text;
Expand All @@ -262,6 +287,10 @@ public void showKeyboard(String p_existing_text, VirtualKeyboardType p_type, int
}

public void hideKeyboard() {
if (hasHardwareKeyboard()) {
return;
}

final Message msg = new Message();
msg.what = HANDLER_CLOSE_IME_KEYBOARD;
msg.obj = this;
Expand Down
Loading