1

I'm working with a tablet (v3.1). I set an onKeyListener for an EditText:

myEditText.setOnKeyListener( new View.OnKeyListener() { 
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if( keyCode == KeyEvent.KEYCODE_ENTER &&
            event.getAction() == KeyEvent.ACTION_DOWN ) {
           runStuff();
        }
        return false;
        }
    });

When the tab key is pressed, I receive a keyCode 66 (not 61 as the API says), the same as KEYCODE_ENTER.

ariefbayu
  • 21,849
  • 12
  • 71
  • 92
David Gras
  • 958
  • 2
  • 12
  • 21

2 Answers2

0

It depends on the context in which you are using the keyboard. For text/plain input this shouldn't be an issue. However, tab is used to change focus from one element to another. Since tab is non-existent on most SoftIME's Android intercepts TAB and feeds Enter, this helps in most cases where a form is being filled out or where the focus needs to change to another element after input.

ex) username & password

user enters their username and presses enter, the focus is now changed to the password column.

Additionally android uses something called EditorInfo to change the states of its keyboards. Look at this thread for a simple use of the EditorInfo -> Android: how to make keyboard enter button say "Search" and handle its click?

I've run into this issue a couple times. The only way to change it is overriding an onKeyListener or making a custom EditText where you override the onKeyDown function. That way calling keyCode == KeyEvent.KEYCODE_TAB will give you the actual value.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_TAB) 
    {
        tab();   // implemented seperate
        return false; // ignore focus changes when tab is pressed
    }
    super.onKeyUp(keyCode, event);
}

// additional helper methods

public void insert(String s)
{
    int start = start();
    int end = end();
    if(start != end)
    {
        replace(s, start, end);
    }
    else
    {
        getEditableText().insert(start, s);
    }
    setSelection(start);  // changes focus back to edittext
}

public void replace(String s, int st, int e)
{
    getEditableText().replace(st, e, s);
}

public void tab()
{
    insert("\t");
}

Hope this helps you out.

-Chris

Community
  • 1
  • 1
Chris Sullivan
  • 576
  • 5
  • 10
0

It actually depends not on the device but on the keyboard you select. I've run into this before. If you swap out the keyboard for a different one, you'll find that suddenly it starts reacting as you expect. I'm not sure what the cause of this is, but you should give that a try and see if that solves your issue.

Sapan Diwakar
  • 10,480
  • 6
  • 33
  • 43