Skip to content

Commit

Permalink
Merge pull request #12125 from keymanapp/feat/android/enter-mode
Browse files Browse the repository at this point in the history
feat(android): Enhance how ENTER key is handled in apps
  • Loading branch information
darcywong00 authored Aug 19, 2024
2 parents cd17ffb + 686cfe0 commit 79ebbfa
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ public void onStartInput(EditorInfo attribute, boolean restarting) {
}
}

// Determine special handling for ENTER key
KMManager.setEnterMode(attribute.imeOptions, inputType);

InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ExtractedText icText = ic.getExtractedText(new ExtractedTextRequest(), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static android.content.Context.VIBRATOR_SERVICE;

import com.keyman.engine.KMManager.EnterModeType;
import com.keyman.engine.KMManager.KeyboardType;
import com.keyman.engine.data.Keyboard;
import com.keyman.engine.util.CharSequenceUtil;
Expand Down Expand Up @@ -153,7 +154,53 @@ public void run() {
}

if (s.length() > 0 && s.charAt(0) == '\n') {
keyDownUp(KeyEvent.KEYCODE_ENTER, 0);
if (k.keyboardType == KeyboardType.KEYBOARD_TYPE_SYSTEM) {
// Special handling of ENTER key
switch (KMManager.enterMode) {
// Go action
case GO :
ic.performEditorAction(EditorInfo.IME_ACTION_GO);
break;

// Search action
case SEARCH :
ic.performEditorAction(EditorInfo.IME_ACTION_SEARCH);
break;

// Send action
case SEND :
ic.performEditorAction(EditorInfo.IME_ACTION_SEND);
break;

// Next action
case NEXT :
ic.performEditorAction(EditorInfo.IME_ACTION_NEXT);
break;

// Done action
case DONE :
ic.performEditorAction(EditorInfo.IME_ACTION_DONE);
break;

// Previous action
case PREVIOUS :
ic.performEditorAction(EditorInfo.IME_ACTION_PREVIOUS);
break;

// Messaging apps
case NEWLINE :
// Send newline and advance cursor
ic.commitText("\n", 1);
break;

// Default ENTER action
default:
keyDownUp(KeyEvent.KEYCODE_ENTER, 0);
}
} else {
// In-app keyboard uses default ENTER action
keyDownUp(KeyEvent.KEYCODE_ENTER, 0);
}
ic.endBatchEdit();
return;
}
Expand Down
72 changes: 71 additions & 1 deletion android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.Typeface;
import android.inputmethodservice.InputMethodService;
Expand Down Expand Up @@ -180,6 +179,19 @@ public String toString() {
}
}

// Enum for how the System Keyboard ENTER key is handled for the EditorInfo action
// Reference: https://developer.android.com/reference/android/view/inputmethod/EditorInfo#summary
public enum EnterModeType {
GO, // Go action
SEARCH, // Search action
SEND, // Send action
NEXT, // Next action
DONE, // Done action
PREVIOUS, // Previous action
NEWLINE, // Send newline character
DEFAULT, // Default ENTER action
}

protected static InputMethodService IMService;

private static boolean debugMode = false;
Expand Down Expand Up @@ -218,6 +230,9 @@ public String toString() {
// regardless what the Settings preference is.
private static boolean mayPredictOverride = false;

// Determine how system keyboard handles ENTER key
public static EnterModeType enterMode = EnterModeType.DEFAULT;

// Boolean for whether a keyboard can send embedded KMW crash reports to Sentry
// When maySendCrashReport is false, KMW will still attempt to send crash reports, but it
// will be blocked.
Expand Down Expand Up @@ -1251,6 +1266,61 @@ public boolean accept(File pathname) {
}
}

/**
* Sets enterMode which specifies how the System keyboard ENTER key is handled
*
* @param imeOptions EditorInfo.imeOptions
* @param inputType InputType
*/
public static void setEnterMode(int imeOptions, int inputType) {
if ((inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE) != 0) {
enterMode = EnterModeType.NEWLINE;
return;
}

int imeActions = imeOptions & EditorInfo.IME_MASK_ACTION;
EnterModeType value = EnterModeType.DEFAULT;

switch (imeActions) {
case EditorInfo.IME_ACTION_GO:
value = EnterModeType.GO;
break;

case EditorInfo.IME_ACTION_SEARCH:
value = EnterModeType.SEARCH;
break;

case EditorInfo.IME_ACTION_SEND:
value = EnterModeType.SEND;
break;

case EditorInfo.IME_ACTION_NEXT:
value = EnterModeType.NEXT;
break;

case EditorInfo.IME_ACTION_DONE:
value = EnterModeType.DONE;
break;

case EditorInfo.IME_ACTION_PREVIOUS:
value = EnterModeType.PREVIOUS;
break;

default:
value = EnterModeType.DEFAULT;
}

enterMode = value;
}

/**
* Get the value of enterMode
* @return EnterModeType
*/
public static EnterModeType getEnterMode() {
return enterMode;
}

/**
* Sets mayPredictOverride true if the InputType field is a hidden password text field
* (either TYPE_TEXT_VARIATION_PASSWORD or TYPE_TEXT_VARIATION_WEB_PASSWORD
Expand Down

0 comments on commit 79ebbfa

Please sign in to comment.