Search code examples
androidandroid-softkeyboardenterime

DONE key on android keyboard


I'm having trouble with done/enter/next key. I'm making android soft keyboard and whenever I try to search for something I can't do that because when I press DONE key the cursor just moves to next row. Here is my code:

XML

<Key android:keyLabel="DONE" android:keyWidth="11.11%" android:codes="-1"/>

JAVA

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();
    switch(primaryCode){
        case -1:
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
            break;
        default:
            char code = (char)primaryCode;
            if(Character.isLetter(code) && caps){
                code = Character.toUpperCase(code);
            }
            ic.commitText(String.valueOf(code), 1);
    }
}

Example: If I search for something on Google and I type what I want and when I click DONE key I want it to start the search. Can you please help me with that. Thanks in advance.


Solution

  • Instead of

    ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
    

    try

    ic.performEditorAction(EditorInfo.IME_ACTION_GO);
    

    The action performed should be the one defined on the EditorInfo, which is passed to you on method onStartInputView

    To switch between actions, use this:

    (sEditorInfo.imeOptions & (EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION))
    

    Hope this helps.

    Code:

    case -1:
    
        switch (sEditorInfo.imeOptions & (EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
            case EditorInfo.IME_ACTION_GO:
                ic.performEditorAction(EditorInfo.IME_ACTION_GO);
                break;
            case EditorInfo.IME_ACTION_NEXT:
                ic.performEditorAction(EditorInfo.IME_ACTION_NEXT);
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                ic.performEditorAction(EditorInfo.IME_ACTION_SEARCH);
                break;
            case EditorInfo.IME_ACTION_SEND:
                ic.performEditorAction(EditorInfo.IME_ACTION_SEND);
                break;
            default:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
        }
    
        break;