jelic98
jelic98

Reputation: 713

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.

Upvotes: 4

Views: 3181

Answers (2)

vimᴀl kumar
vimᴀl kumar

Reputation: 11

@Override
public void onStartInputView(EditorInfo info, boolean restarting) {
    InputConnection ic = getCurrentInputConnection();
    switch (info.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;
    }
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();
    playClick(primaryCode);
    switch(primaryCode){
        case Keyboard.KEYCODE_DELETE :
            ic.deleteSurroundingText(1, 0);
            break;
        case Keyboard.KEYCODE_SHIFT:
            caps = !caps;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            break;
        case Keyboard.KEYCODE_DONE: // how this case needs to be handled ??
            //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);
    }
}

Upvotes: 0

Jos&#233; Silva
Jos&#233; Silva

Reputation: 534

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;

Upvotes: 16

Related Questions