Search code examples
androidandroid-softkeyboardenterandroid-eventsoft-keyboard

Use “ENTER” key on softkeyboard to initiation an Event


I'm designing an android keyboard and am having difficulty with initiating an action command from the keyboard.

When I am using the Internet on my device and press enter after typing in a website, rather than going to a new page, a space is shown and no action is done. I'm not sure how to make my enter key become an action key when pressed.

Here's the code where I believe it should be altered:

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();
    switch (primaryCode) {

    case Keyboard.KEYCODE_SHIFT:
        handleShift();
        break;

    case 10:
        //Initiate enter event or new line depending on program being used
        break;
    }
}

Any help would be appreciated.


Solution

  • Alright, thanks to George Rappel - I was sent in the right direction. I found my solution in the online android source code - took some hunting for that. Below is the code for ENTER - where ENTER is the value 10.

    case ENTER:
    
            final EditorInfo editorInfo = getCurrentInputEditorInfo();
            final int imeOptionsActionId = InputTypeUtils.getImeOptionsActionIdFromEditorInfo(editorInfo);
            if (InputTypeUtils.IME_ACTION_CUSTOM_LABEL == imeOptionsActionId) {
                // Enter used as submission
                ic.performEditorAction(editorInfo.actionId);
            } else if (EditorInfo.IME_ACTION_NONE != imeOptionsActionId) {
                // Not quite sure what this is for
                ic.performEditorAction(imeOptionsActionId);
            } else {
                // Enter being used as text
                ic.commitText(String.valueOf((char) primaryCode), 1);
            }
            break;
    

    I also copied the necessary methods from the class InputTypeUtils.java found in the online android repository.