1
votes

I created a custom input method, just the keyboard not a complete IME with an editText, but for some reason my "Enter" button isnt performing like that of the built in keyboard. On certain apps, such as the Facebook app for eg., there is a "Log In" button or some similar button following the "password" field. When I use the built in keyboard and I put in my password and press the "Enter" button it begins the log in process, but my keyboard doesnt do that. I tried sending the key event in different ways using:

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

and

sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);

but neither seems to have the same effect. Just to be clear; both pieces of code satisfy other "Enter" functions such as committing text to a textfied or starting a new line. Could someone please tell me what I am missing here?

2
I am also writing an input method and got stuck on the same issue. Did you ever solve it?Dov Grobgeld
@DovGrobgeld Hey first let me say thanks for taking part in the conversation. Sorry though, I never did solve it.I_am_that_guy

2 Answers

0
votes

Try sendDefaultEditorAction(true) method. For my case on AndroidTV this code works fine:

class MyInputMethodService : InputMethodService() {
    
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            sendDefaultEditorAction(true)
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

}
0
votes

refer below example to trigger KeyEvent.KEYCODE_ENTER when 'line feed' https://cs.android.com/android/platform/superproject/+/master:development/samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java;l=521;drc=master

private void keyDownUp(int keyEventCode) {
    getCurrentInputConnection().sendKeyEvent(
            new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
    getCurrentInputConnection().sendKeyEvent(
            new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
}

private void sendKey(int keyCode) {
    switch (keyCode) {
        case '\n':
            keyDownUp(KeyEvent.KEYCODE_ENTER);
            break;
        default:
            if (keyCode >= '0' && keyCode <= '9') {
                keyDownUp(keyCode - '0' + KeyEvent.KEYCODE_0);
            } else {
                getCurrentInputConnection().commitText(String.valueOf((char) keyCode), 1);
            }
            break;
    }
}