1
0
Fork 0

fixed OK key typing new lines instead of performing an action in some apps

This commit is contained in:
sspanak 2024-04-02 19:09:05 +03:00 committed by Dimo Karaivanov
parent caad8a5446
commit a3724bb80c
7 changed files with 57 additions and 29 deletions

View file

@ -43,13 +43,19 @@ public abstract class HotkeyHandler extends TypingHandler {
@Override public boolean onOK() { @Override public boolean onOK() {
suggestionOps.cancelDelayedAccept(); suggestionOps.cancelDelayedAccept();
if (suggestionOps.isEmpty()) { if (!suggestionOps.isEmpty()) {
int action = textField.getAction();
return action == TextField.IME_ACTION_ENTER ? appHacks.onEnter() : textField.performAction(action);
} else {
onAcceptSuggestionManually(suggestionOps.acceptCurrent(), KeyEvent.KEYCODE_ENTER); onAcceptSuggestionManually(suggestionOps.acceptCurrent(), KeyEvent.KEYCODE_ENTER);
return true; return true;
} }
int action = textField.getAction();
boolean actionPerformed = action == TextField.IME_ACTION_ENTER ? appHacks.onEnter() : textField.performAction(action);
if (actionPerformed && action == TextField.IME_ACTION_ENTER) {
forceShowWindowIfHidden();
}
return actionPerformed;
} }

View file

@ -69,9 +69,10 @@ public class AppHacks {
} }
private boolean isMultilineTextField() { private boolean isMultilineTextInNonSystemApp() {
return return
editorInfo != null editorInfo != null
&& !editorInfo.packageName.contains("android")
&& (editorInfo.inputType & TextField.TYPE_MULTILINE_TEXT) == TextField.TYPE_MULTILINE_TEXT; && (editorInfo.inputType & TextField.TYPE_MULTILINE_TEXT) == TextField.TYPE_MULTILINE_TEXT;
} }
@ -133,30 +134,23 @@ public class AppHacks {
* it does nothing. * it does nothing.
*/ */
public boolean onEnter() { public boolean onEnter() {
if (isTermux()) { if (settings.getFbMessengerHack() && isMessenger()) {
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
return true;
} else if (settings.getFbMessengerHack() && isMessenger()) {
return onEnterFbMessenger(); return onEnterFbMessenger();
} else if (settings.getGoogleChatHack() && isGoogleChat()) { } else if (settings.getGoogleChatHack() && isGoogleChat()) {
return onEnterGoogleChat(); return onEnterGoogleChat();
} else if (isMultilineTextField()) { } else if (isTermux() || isMultilineTextInNonSystemApp()) {
return onEnterMultilineText(); // 1. Termux supports only ENTER, so we convert DPAD_CENTER for it.
// 2. Any extra installed apps are likely not designed for hardware keypads, so again,
// we don't want to send DPAD_CENTER to them.
return sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
} }
// The rest of the cases are probably system apps or numeric fields, which should
// now how to handle the incoming OK key code, be it ENTER or DPAD_CENTER.
return false; return false;
} }
/**
* In generic text fields, or when no hacks are in effect, we just type a new line,
* as one would expect when pressing ENTER.
*/
private boolean onEnterMultilineText() {
return inputConnection != null && inputConnection.commitText("\n", 1);
}
/** /**
* onEnterFbMessenger * onEnterFbMessenger
* Messenger responds only to ENTER, but not DPAD_CENTER, so we make sure to send the correct code, * Messenger responds only to ENTER, but not DPAD_CENTER, so we make sure to send the correct code,
@ -194,17 +188,18 @@ public class AppHacks {
} }
private void sendDownUpKeyEvents(int keyCode) { private boolean sendDownUpKeyEvents(int keyCode) {
sendDownUpKeyEvents(keyCode, false); return sendDownUpKeyEvents(keyCode, false);
} }
private void sendDownUpKeyEvents(int keyCode, boolean shift) { private boolean sendDownUpKeyEvents(int keyCode, boolean shift) {
if (inputConnection != null) { if (inputConnection != null) {
KeyEvent downEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, keyCode, 0, shift ? KeyEvent.META_SHIFT_ON : 0); KeyEvent downEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, keyCode, 0, shift ? KeyEvent.META_SHIFT_ON : 0);
KeyEvent upEvent = new KeyEvent(0, 0, KeyEvent.ACTION_UP, keyCode, 0, shift ? KeyEvent.META_SHIFT_ON : 0); KeyEvent upEvent = new KeyEvent(0, 0, KeyEvent.ACTION_UP, keyCode, 0, shift ? KeyEvent.META_SHIFT_ON : 0);
inputConnection.sendKeyEvent(downEvent); return inputConnection.sendKeyEvent(downEvent) && inputConnection.sendKeyEvent(upEvent);
inputConnection.sendKeyEvent(upEvent);
} }
return false;
} }
} }

View file

@ -341,8 +341,8 @@ public class TextField {
int standardAction = field.imeOptions & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION); int standardAction = field.imeOptions & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
switch (standardAction) { switch (standardAction) {
case EditorInfo.IME_ACTION_GO:
case EditorInfo.IME_ACTION_DONE: case EditorInfo.IME_ACTION_DONE:
case EditorInfo.IME_ACTION_GO:
case EditorInfo.IME_ACTION_NEXT: case EditorInfo.IME_ACTION_NEXT:
case EditorInfo.IME_ACTION_PREVIOUS: case EditorInfo.IME_ACTION_PREVIOUS:
case EditorInfo.IME_ACTION_SEARCH: case EditorInfo.IME_ACTION_SEARCH:

View file

@ -148,7 +148,6 @@ public class SoftKey extends androidx.appcompat.widget.AppCompatButton implement
if (keyId == R.id.soft_key_left_arrow) return tt9.onKeyScrollSuggestion(false, true); if (keyId == R.id.soft_key_left_arrow) return tt9.onKeyScrollSuggestion(false, true);
if (keyId == R.id.soft_key_right_arrow) return tt9.onKeyScrollSuggestion(false, false); if (keyId == R.id.soft_key_right_arrow) return tt9.onKeyScrollSuggestion(false, false);
if (keyId == R.id.soft_key_language) return tt9.onKeyNextLanguage(false); if (keyId == R.id.soft_key_language) return tt9.onKeyNextLanguage(false);
if (keyId == R.id.soft_key_ok) return tt9.onOK();
if (keyId == R.id.soft_key_settings) return tt9.onKeyShowSettings(false); if (keyId == R.id.soft_key_settings) return tt9.onKeyShowSettings(false);
return false; return false;

View file

@ -0,0 +1,28 @@
package io.github.sspanak.tt9.ui.main.keys;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
public class SoftOkKey extends SoftKey {
public SoftOkKey(Context context) {
super(context);
}
public SoftOkKey(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SoftOkKey(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected boolean handleRelease() {
return
validateTT9Handler()
&& tt9.onKeyDown(KeyEvent.KEYCODE_ENTER, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER))
&& tt9.onKeyUp(KeyEvent.KEYCODE_ENTER, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER));
}
}

View file

@ -301,7 +301,7 @@
android:id="@+id/separator_4_2" android:id="@+id/separator_4_2"
style="@style/numSeparator" /> style="@style/numSeparator" />
<io.github.sspanak.tt9.ui.main.keys.SoftKey <io.github.sspanak.tt9.ui.main.keys.SoftOkKey
android:id="@+id/soft_key_ok" android:id="@+id/soft_key_ok"
style="@android:style/Widget.Holo.Button.Borderless" style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="0dp" android:layout_width="0dp"

View file

@ -51,7 +51,7 @@
style="@style/hSeparator" style="@style/hSeparator"
android:background="@drawable/button_separator_dark" /> android:background="@drawable/button_separator_dark" />
<io.github.sspanak.tt9.ui.main.keys.SoftKey <io.github.sspanak.tt9.ui.main.keys.SoftOkKey
android:id="@+id/soft_key_ok" android:id="@+id/soft_key_ok"
style="@android:style/Widget.Holo.Button.Borderless" style="@android:style/Widget.Holo.Button.Borderless"
android:layout_width="0dp" android:layout_width="0dp"