0

Possible Duplicate:
How can I convert a key code into a char or string?

Hey I'm pretty new to Android and unsure how to use getKeyCode. I have this

public boolean onKeyDown(int keyCode, KeyEvent event)
{
    switch(keyCode)
    {
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        Toast.makeText(getApplicationContext(), "Pressed DPAD Right Button",
        Toast.LENGTH_LONG).show();
        return true;
    case KeyEvent.KEYCODE_DPAD_LEFT:
            Toast.makeText(this, "Pressed DPAD Left Button",
            Toast.LENGTH_LONG).show();
            return true;
    }
    return super.onKeyDown(keyCode,event);
}

But I want to change a textview to say what button was pressed. How do I pass the keyCode into the setText function? I already have it all linked up, I just need the keyCode variable to put into

txt2.setText(Integer.toString(getKeyCode()));
Community
  • 1
  • 1
Calender Man
  • 23
  • 1
  • 8
  • 2
    Have you tried, `KeyEvent.getKeyText(KeyEvent.getKeyCode())`? – rossum Oct 13 '12 at 14:12
  • Thanks, got it working :) Quick question though, is there anyway to get it to display the actual name of the key pressed rather than the number assigned to it? @Rossum, that doesnt seem to work, just gives me static/non-static errors. – Calender Man Oct 13 '12 at 14:13
  • @Calender Man: in your code, try `event.getKeyText(event.getKeyCode())` – rossum Oct 13 '12 at 15:43

1 Answers1

2

If you want to get an actual name of key you can do something like:

int pressedKeyCode = event.getKeyCode();
Object pressedKeyCodeObject = new Integer(pressedKeyCode);
String pressedKeyText = KeyEvent.getKeyText((Integer)pressedKeyCodeObject);
Sabre
  • 4,131
  • 5
  • 36
  • 57