Android: Key Event from Android Box remote controller
Asked Answered
E

2

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.

Emlyn answered 25/3, 2015 at 9:5 Comment(0)
L
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;
}
Lie answered 25/3, 2015 at 9:21 Comment(16)
aha, thanks a lot. What about the OK button, the one in the middle? How can i catch it?Emlyn
run the code above and try to press OK button, the keycode will be printed to Logcat.Lie
Well, this code takes in consideration the keyevents from smartphones. I wanted only from remote controller. Any solution?Emlyn
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.Lie
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?Emlyn
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.Lie
Sorry if i ask you too much, could you please show me with code, since i am stuck with it?Emlyn
Ok, no problem. I just updated my answer. I'm sorry if i understood your question incorrectly.Lie
@Lie 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?Akeylah
@Dima: There are only Home key is not possible to catch key event. developer.android.com/reference/android/view/…Lie
@Lie Yes, also problem was in app compat. If someone runs into same problem, there is answer https://mcmap.net/q/721729/-upgraded-to-appcompat-v22-1-0-and-now-onkeydown-and-onkeyup-are-not-triggered-when-menu-key-is-pressedAkeylah
@Lie : Can you help me with my question.#42976306Praetor
@saa: I'm sorry because I don't have solution for your question. Could you explain me your situation?Lie
@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.Praetor
@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.Lie
@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.Praetor
C
0

Minor correction on Tien's answer. event.getKeyCode missing () and Toast.LENG_LONG missing TH. Corrected code below:

@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.LENGTH_LONG).show();

} 
return true;

}

Compelling answered 17/9, 2023 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.