Introduce toggling system for all keyboards

This commit is contained in:
SerpentSpirale 2021-08-06 23:47:09 +02:00 committed by ArtDev
parent 1f2d599ce5
commit e74fb95f98
3 changed files with 52 additions and 32 deletions

View File

@ -884,21 +884,22 @@ public class BaseMainActivity extends LoggableActivity {
sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ESCAPE); sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ESCAPE);
} }
public void hideKeyboard() {
try {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow((this).getCurrentFocus().getWindowToken(), 0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void showKeyboard() { /**
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); * Toggle on and off the soft keyboard, depending of the state
minecraftGLView.requestFocusFromTouch(); * The condition is prone to errors if the keyboard is being hidden without the consent
minecraftGLView.requestFocus(); * of the current TouchCharInput
*/
public void switchKeyboardState(){
if(touchCharInput.hasFocus()){
touchCharInput.clear();
touchCharInput.disable();
}else{
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
touchCharInput.enable();
touchCharInput.postDelayed(() -> imm.showSoftInput(touchCharInput, InputMethodManager.SHOW_IMPLICIT), 200);
}
} }
protected void setRightOverride(boolean val) { protected void setRightOverride(boolean val) {

View File

@ -63,7 +63,7 @@ public class MainActivity extends BaseMainActivity {
for(int keycode : button.getProperties().keycodes){ for(int keycode : button.getProperties().keycodes){
switch (keycode) { switch (keycode) {
case ControlData.SPECIALBTN_KEYBOARD: case ControlData.SPECIALBTN_KEYBOARD:
showKeyboard(); switchKeyboardState();
break; break;
case ControlData.SPECIALBTN_TOGGLECTRL: case ControlData.SPECIALBTN_TOGGLECTRL:

View File

@ -1,20 +1,14 @@
package net.kdt.pojavlaunch.customcontrols; package net.kdt.pojavlaunch.customcontrols;
import android.content.Context; import android.content.Context;
import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo; import android.widget.Toast;
import android.view.inputmethod.InputConnection;
import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.view.inputmethod.EditorInfoCompat;
import androidx.core.view.inputmethod.InputConnectionCompat;
import androidx.core.view.inputmethod.InputContentInfoCompat;
import net.kdt.pojavlaunch.BaseMainActivity; import net.kdt.pojavlaunch.BaseMainActivity;
import net.kdt.pojavlaunch.LWJGLGLFWKeycode; import net.kdt.pojavlaunch.LWJGLGLFWKeycode;
@ -38,13 +32,13 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
setup(); setup();
} }
private boolean isClearingText = false; private boolean isDoingInternalChanges = false;
TextWatcher mTextWatcher = new TextWatcher() { TextWatcher mTextWatcher = new TextWatcher() {
//TODO Engineer a more performant system //TODO Engineer a more performant system
@Override @Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(isClearingText) return; if(isDoingInternalChanges) return;
for(int j=0; j<charSequence.length(); ++j){ for(int j=0; j<charSequence.length(); ++j){
CallbackBridge.sendKeycode(LWJGLGLFWKeycode.GLFW_KEY_BACKSPACE, '\u0008', 0, 0, true); CallbackBridge.sendKeycode(LWJGLGLFWKeycode.GLFW_KEY_BACKSPACE, '\u0008', 0, 0, true);
@ -53,7 +47,7 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
@Override @Override
public void onTextChanged(CharSequence charSequence, int start, int lengthBefore, int lengthAfter) { public void onTextChanged(CharSequence charSequence, int start, int lengthBefore, int lengthAfter) {
if(isClearingText) return; if(isDoingInternalChanges) return;
for (int i=0; i<charSequence.length(); ++i){ for (int i=0; i<charSequence.length(); ++i){
CallbackBridge.sendChar(charSequence.charAt(i)); CallbackBridge.sendChar(charSequence.charAt(i));
@ -63,10 +57,7 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
@Override @Override
public void afterTextChanged(Editable editable) { public void afterTextChanged(Editable editable) {
if(isClearingText){ isDoingInternalChanges = false;
isClearingText = false;
editable.clear();
}
} }
}; };
@ -76,7 +67,7 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
* It does not affect the in-game input * It does not affect the in-game input
*/ */
public void clear(){ public void clear(){
isClearingText = true; isDoingInternalChanges = true;
setText(""); setText("");
} }
@ -89,6 +80,30 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
clear(); clear();
} }
/**
* Regain ability to exist, take focus and have some text being input
*/
public void enable(){
setEnabled(true);
setFocusable(true);
setVisibility(VISIBLE);
requestFocus();
}
/**
* Lose ability to exist, take focus and have some text being input
*/
public void disable(){
setVisibility(GONE);
clearFocus();
//setFocusable(false);
setEnabled(false);
}
/** /**
* This function deals with anything that has to be executed when the constructor is called * This function deals with anything that has to be executed when the constructor is called
*/ */
@ -101,6 +116,10 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
send(); send();
return false; return false;
}); });
isDoingInternalChanges = true;
setText("");
disable();
} }
} }