From 50be325bb77c93281f22b3a8fabbbe9d314fa078 Mon Sep 17 00:00:00 2001 From: SerpentSpirale Date: Sun, 8 Aug 2021 14:13:19 +0200 Subject: [PATCH] Add support for many ways of losing keyboard focus --- .../customcontrols/TouchCharInput.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/TouchCharInput.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/TouchCharInput.java index ae04cbfb5..a8308f178 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/TouchCharInput.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/TouchCharInput.java @@ -2,6 +2,7 @@ package net.kdt.pojavlaunch.customcontrols; import android.content.Context; import android.util.AttributeSet; +import android.view.KeyEvent; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -30,6 +31,12 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText private boolean isDoingInternalChanges = false; + + /** + * We take the new chars, and send them to the game. + * If less chars are present, remove some. + * The text is always cleaned up. + */ @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { super.onTextChanged(text, start, lengthBefore, lengthAfter); @@ -51,6 +58,33 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText } + /** + * When we change from app to app, the keyboard gets disabled. + * So, we disable the object + */ + @Override + public void onWindowFocusChanged(boolean hasWindowFocus) { + super.onWindowFocusChanged(hasWindowFocus); + disable(); + } + + /** + * Intercepts the back key to disable focus + * Does not affect the rest of the activity. + */ + @Override + public boolean onKeyPreIme(final int keyCode, final KeyEvent event) { + if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { + disable(); + } + return super.onKeyPreIme(keyCode, event); + } + + @Override + public void setSelection(int index) { + super.setSelection(5); + } + /** * Clear the EditText from any leftover inputs * It does not affect the in-game input @@ -84,6 +118,7 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText * Lose ability to exist, take focus and have some text being input */ public void disable(){ + clear(); setVisibility(GONE); clearFocus(); setEnabled(false);