0

I'm a french developer , i'm creating an android tv application about cloud Gaming in a webview with java.

My application start a gaming stream direcly in the webview, on fullscreen, the physical buttons of controlers are working with the game, eccept the "view button" the "back button'. This "view button" or if your prefer "select button" is for android tv a back button to the homescreen. So i have to overiding this back button, and i want replace it by a "select button" that it can interact with the games for displaying maps and inventory like in rpg games.

I know that the name will be "button_select" for interact with the pc game. So in android tv for now i will always redirected to the home page.

this is a sample of my code.

public class MainActivity extends AppCompatActivity {
    @Override
    public void onBackPressed() {
        return;
    }

With this overide, the back button is completly disabled for now, i want replace it or call the "button_select". I readed something about "handler" perhaps this is the solution.

Edit 16/06/2021


I tested much coded, but nothing work.

this one

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
        }
        return super.onKeyDown(keyCode, event);
    }

And its variants

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (action == KeyEvent.ACTION_DOWN ){
                    //Do something in the back button
                    keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;

                }
                return true;
            default:
                return super.dispatchKeyEvent(event);
        }
    }

In game, the back button is disabled but, the" keycode_button_select "not interact with the game. There is nothing. I test with the "keycode button_start" the same things.

I tested the app button mapper for android tv, there is an option for custom keycodes with adb, and nothing is working in game.

https://play.google.com/store/apps/details?id=flar2.homebutton&hl=fr&gl=US

So by the code or by an app, nothing is working for now, i don't want to forcing the "root mode" for this things. Perhaps i'll just have to implementing a gamepad Plugin in java. I don't know...

Edit 18/06/21

I can capture the "back button" and display a dialog alert that appear on middle of the screen. But the actions after has no effect for now. I tested "dispatchEvent" with no success for now. I will testing the functions "robots". Perhaps "robot" and "dispatchEvent" will working together.

Thank you for your help.

1 Answers1

0

I am assuming that you want to call button_select key when user presses back button.

In this case, create a method to call upon pressing back button. And call that method from the onBackPressed method.

For example:

public void callButtonSelectKey() {
    //This could be done in either ways.
    //1.
    val i = Instrumentation()
    i.sendKeyDownUpSync(KeyEvent.KEYCODE_BUTTON_SELECT)
    //2.
    val keyEventDown = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_SELECT)
    val keyEventUp = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_SELECT)
    dispatchKeyEvent(keyEventDown)
    dispatchKeyEvent(keyEventUp)

}

@Override
public void onBackPressed() {
    methodToCallUponBackPress();
    return;
}

Now when the user presses the back button, it will call callButtonSelectKey method invoke "button_select" key. (i.e. remapped backbutton to button_select)

Let me know if you have any more questions.

EDIT

In an Object Oriented Language, overriding the class's (in this case KeyEvent) field (in this case, keycode) will do nothing. Like the code below:

//Do something in the back button
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;

To call button_select when you receive the event of KEYCODE_BACK, you should manually invoke the method to call Key Event, in this case the method I gave you earlier dispatchKeyEvent .

So you should make your change into something like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        //This could be done in either ways.
       //1.
       val i = Instrumentation()
       i.sendKeyDownUpSync(KeyEvent.KEYCODE_BUTTON_SELECT)
       //2.
       val keyEventDown = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_SELECT)
       val keyEventUp = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_SELECT)
       dispatchKeyEvent(keyEventDown)
       dispatchKeyEvent(keyEventUp)

    }
    return super.onKeyDown(keyCode, event); //Edit this return statement if you want to ignore a keypress.
}
minchaej
  • 1,294
  • 1
  • 7
  • 14
  • Thanks a lot for your answer. I understand your function. But for a better understand, i'm not building a game from scratch , it's game streaming from big titles AAA like cyberpunk. So they have already functions with basic xbox controller. – MisterDev21 Jun 14 '21 at 17:26
  • So i want replace this back button by the keycode_button_select. It's like a remapping programaticly, so i understand i have to listen the key pressed for excute another key. I think Keylistner is appropriate for this issue. https://www.programcreek.com/java-api-examples/?class=android.view.KeyEvent&method=KEYCODE_HOME https://developer.android.com/reference/android/view/KeyEvent#KEYCODE_BUTTON_SELECT I hope it's more clear. What do you think ? Thank you so mush, i will credit you for your help. – MisterDev21 Jun 14 '21 at 17:26
  • Sure thanks for the detailed explanation. I have updated my answer to fultill what you are trying to achieve. – minchaej Jun 14 '21 at 17:47
  • Wow , impressive. Yes i think it's the good approach. I can't test it now but in the night directly with a new build of my app in the Nvidia shield tv. This is my project : https://github.com/mistertest/devtest https://www.youtube.com/watch?v=fzqqhDurLcs You'll be credited in my special thanks. – MisterDev21 Jun 14 '21 at 18:02
  • Woah just saw your link and I am very excited about your project! Sure, let me know if you have any other questions. Please accept and vote for my answer it helps! Thanks – minchaej Jun 15 '21 at 01:26
  • i'm happy to hear that. Yes of course bro. I'm testing some various code based in your approach all this day, i will come back to you. – MisterDev21 Jun 15 '21 at 10:17
  • Hello Michaej i ere dited my question with all i tested, and nothing is working for now in game. – MisterDev21 Jun 16 '21 at 16:36
  • Hello @MisterDev21, I see that you have implemented it incorrectly, I have updated my answer to guide you further. Let me know if you have any questions. – minchaej Jun 16 '21 at 19:44