1

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.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
sometimes24
  • 355
  • 4
  • 15

2 Answers2

2

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.

sometimes24
  • 355
  • 4
  • 15
  • Sorry for not beeing able to answer, but i'm already happy to help some way. Good luck with your development :) – George Sep 01 '15 at 11:20
0

You're using KEYCODE_SHIFT, which is actually the one to change the keyboard from upper to lower case. The Enter keycode should be KEYCODE_ENTER, and it's value is 66, not 10.

case 10 would be KEYCODE_3, the number 3, not enter. See here.


You have the right code, all you have to do is change the KEYCODE and add the lines of code you want.

@Override
public void onKey(int primaryCode, int[] keyCodes) {

    switch (primaryCode) {    
        case KeyEvent.KEYCODE_ENTER:
            // Enter has been pressed
        break;
    }
}
George
  • 6,886
  • 3
  • 44
  • 56
  • 1
    The real line is `KeyEvent.KEYCODE_ENTER`. Also, I don't know what lines of code goes in the case statement. That's my real issue :) – sometimes24 Aug 24 '15 at 17:40
  • @sometimes24 my mistake, sorry. But what exactly do you want? To press the ENTER key and start the event for another key? – George Aug 24 '15 at 17:57
  • Since it's a keyboard, when I press the ENTER key - I want to initiate an action button. I'm probably not explaining it clear so hopefully my example makes sense. If I am in the Internet application and am typing a hyperlink, I want, when I press ENTER, to initiate the search command vs. the space character I see instead (because it isn't programmed right). – sometimes24 Aug 26 '15 at 02:51
  • 1
    Thank you - that sent me in the right direction. I pasted my answer here. Just wanted to note - KeyEvent.KEYCODE_ENTER is 66 and it printed B when I used it on my app. They Keys are used based on their ASCII values. The number 10 (line feed) yields the right answer. – sometimes24 Sep 01 '15 at 04:32