Skip to content

Commit

Permalink
Fix Android input routing logic when using a hardware keyboard
Browse files Browse the repository at this point in the history
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 for soft keyboards, for which the current logic was designed as it requires extra processing.
  • Loading branch information
m4gr3d committed Aug 23, 2023
1 parent 6758a7f commit 87102e3
Showing 1 changed file with 29 additions and 0 deletions.
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

0 comments on commit 87102e3

Please sign in to comment.