8

I was interested to know how can i catch key/button events from Android TV Box remote controller?

For example, i want a popup menu to show when i click the OK button from remote controller. And i want to catch the next/back key events from remote controller.

Should i use the Key Event class from Android, if yes how should i implement it?

I came across this function but i cannot really make sense of it.

 @Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {

    switch (keyCode) {
        case KeyEvent.KEYCODE_A:
        {
            //your Action code
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

Thanks in advance.

strategos
  • 119
  • 1
  • 2
  • 6

1 Answers1

4

You should catch key event on dispatchKeyEvent

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        Log.e(TAG, "Key down, code " + event.getKeyCode());

    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        Log.e(TAG, "Key up, code " + event.getKeyCode());
    }

    return true;
}

Edit: First, you should know key map of your remote (it is not the same for all kind of Android TV box), the code above will help you know code of key that you press on the remote. For example, i got key code 3 when i press button BACK on remote. Then, i want when back key pressed, a Toast message will be show:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    // You should make a constant instead of hard code number 3.
    if (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode == 3) {
        Toast.makeText(this, "Hello, you just press BACK", Toast.LENG_LONG).show();

    } 
    return true;
}
Tien
  • 2,105
  • 17
  • 14
  • aha, thanks a lot. What about the OK button, the one in the middle? How can i catch it? – strategos Mar 25 '15 at 09:26
  • run the code above and try to press OK button, the keycode will be printed to Logcat. – Tien Mar 25 '15 at 09:33
  • Well, this code takes in consideration the keyevents from smartphones. I wanted only from remote controller. Any solution? – strategos Mar 25 '15 at 13:20
  • No, it's the same for all devices running Android, no matter what it's smartphone or Android TV box. P/s: I'm developing app for Android Set Top Box. – Tien Mar 25 '15 at 15:32
  • Good luck Tien. As for the problem, i want the key event to work only for TV box not in smartphones. Plus, the code oddly keeps repeating itself even though i click just once. Any idea how to implement this? – strategos Mar 25 '15 at 15:57
  • When you press a key on the remote, it will trigger 2 event: one for key down and one for key up. But remember, if you press and hold a key for a while, it will trigger key down event continuously until you release that key (when you release, one key up event will be triggered.) You should choose one event (key up/ key down) and do what you want. – Tien Mar 25 '15 at 16:45
  • Sorry if i ask you too much, could you please show me with code, since i am stuck with it? – strategos Mar 25 '15 at 16:59
  • Ok, no problem. I just updated my answer. I'm sorry if i understood your question incorrectly. – Tien Mar 25 '15 at 17:29
  • @Tien Thank you, you saved my day. But also I have problem with one button, it's menu button and I can't receive keyevent from it. Did you run into that problem? – Dima Aug 12 '15 at 14:13
  • @Dima: There are only Home key is not possible to catch key event. http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME – Tien Aug 13 '15 at 01:16
  • 1
    @Tien Yes, also problem was in app compat. If someone runs into same problem, there is answer http://stackoverflow.com/a/29852304/2004305 – Dima Aug 13 '15 at 08:54
  • 1
    @Tien : Can you help me with my question.http://stackoverflow.com/questions/42975806/is-it-possible-to-receive-android-tv-remote-key-events-without-activity – saa Mar 23 '17 at 13:15
  • @saa: I'm sorry because I don't have solution for your question. Could you explain me your situation? – Tien Mar 23 '17 at 17:23
  • @Tien: Thanks for reply. I have nexus player and developing app for it. Task is overriding Android remote control key events. When my Activity in active state, I could able to get them(key events) in onKeyDown() method. but when my activity is in pause state(in background) i could not able to get key events of remote control. OR Is there any way to register myself to something(Android Component) to listen for Android TV remote control key events. – saa Mar 24 '17 at 06:36
  • @saa: I looked around on the internet but seem that it's not possible to do that. People said that only activity and view are able to receive key events. – Tien Mar 27 '17 at 00:16
  • @Tien: Thanks Tien. for your help. Do you have any idea on How a Android TV(Nexus Player) receiving key events from TV Remote. As per my knowledge, it might be a service. If it's service how to bind to that service(any aidl or interface or Messenger) from my app. – saa Mar 27 '17 at 06:33