Skip to content

Commit

Permalink
Improved OK handling
Browse files Browse the repository at this point in the history
  * OK key now attempts to send DPAD_CENTER or ENTER (if needed), in attempt to make it work consistently accross all devices

  * OK is no longer ignored in numeric mode
  • Loading branch information
sspanak committed Mar 27, 2023
1 parent d6884ef commit 2df5932
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/io/github/sspanak/tt9/ime/KeyPadHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
isBackspaceHandled = false;
}

if (Key.isOK(keyCode)) {
return true;
}

// In numeric fields, we do not want to handle anything, but "backspace"
if (mEditing == EDITING_STRICT_NUMERIC) {
return false;
Expand Down Expand Up @@ -171,8 +175,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
|| keyCode == KeyEvent.KEYCODE_POUND
|| (Key.isNumber(keyCode) && shouldTrackNumPress())
|| ((keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KeyEvent.KEYCODE_DPAD_DOWN) && shouldTrackUpDown())
|| ((keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) && shouldTrackLeftRight())
|| (mEditing != EDITING_NOSHOW && Key.isOK(keyCode));
|| ((keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) && shouldTrackLeftRight());
}


Expand Down Expand Up @@ -235,6 +238,10 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
return true;
}

if (Key.isOK(keyCode)) {
return onOK();
}

// in numeric fields, we just handle backspace and let the rest go as-is.
if (mEditing == EDITING_STRICT_NUMERIC) {
return false;
Expand All @@ -254,10 +261,6 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
return true;
}

if (Key.isOK(keyCode)) {
return onOK();
}

if (Key.isNumber(keyCode)) {
return onNumber(Key.codeToNumber(settings, keyCode), false, numKeyRepeatCounter);
}
Expand Down
19 changes: 18 additions & 1 deletion src/io/github/sspanak/tt9/ime/TraditionalT9.java
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,24 @@ private boolean performOKAction() {
case EditorInfo.IME_ACTION_NONE:
return false;
case TextField.IME_ACTION_ENTER:
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
String oldText = textField.getTextBeforeCursor() + textField.getTextAfterCursor();

sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_CENTER);

try {
// In Android there is no strictly defined confirmation key, hence DPAD_CENTER may have done nothing.
// If so, send an alternative key code as a final resort.
Thread.sleep(80);
String newText = textField.getTextBeforeCursor() + textField.getTextAfterCursor();
if (newText.equals(oldText)) {
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
}
} catch (InterruptedException e) {
// This thread got interrupted. Assume it's because the connected application has taken an action
// after receiving DPAD_CENTER, so we don't need to do anything else.
return true;
}

return true;
default:
return currentInputConnection.performEditorAction(action);
Expand Down

0 comments on commit 2df5932

Please sign in to comment.