kevinejohn / react-native-keyevent

Capture external keyboard keys or remote control button events
MIT License
209 stars 97 forks source link

Keyevent stops react navigation working with D-pad #25

Open Rob2k9 opened 5 years ago

Rob2k9 commented 5 years ago

I have created an application for android TV it was working perfect but i ran into an issue with google play requesting that the app responds to play / pause requests from an android TV remote or game pad

i looked around and found this package and it seemed to work perfect everything worked good i could click pause and remote and it would work with react-native-video

the issue is once i built the project and created the apk everything stopped working when i loaded my apk on official hardware "Nvidia" shield TV using the controle and game pad the first touchable would get focus but after that i could not navigate to another touchable

Removing react-native-keyevent puts everything back to normal and all touchables can be navigated to again i am using react-native-navigation with react-native-screens to navigate between screens and stop any bleeding through

Rob2k9 commented 5 years ago

Anyone Have A Fix For This Issue ?

freddieerg commented 4 years ago

Anyone Have A Fix For This Issue ?

Hi Rob, I absolutely feel your pain it took me far too long to work out it was this package that was causing the dpad functionality to break. I believe RN simply uses the dpad spatial mapping natively implemented in Android however, when we implement our own onKeyDown in MainActivity and return true, we're basically saying the keypress has been dealt with and nobody else in the app needs to worry about it, this likely means the built in dpad handler is just ignoring keypresses.

To fix this your onKeyDown handler needs to return false. This will still allow this package to work normally but also tells your app that other onKeyDown handlers still need to be listening. Now, I don't know about your app but after implementing this change my back button suddenly broke. To get around this I've added a condition which will check to see if the keypress was a back button: "4". If it is a back button I return true, and this magically fixes it for reasons unknown to me.

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);
    super.onKeyDown(keyCode, event);
    if(keyCode == 4) {
      return true;
    }
    return false;
  }

Hope this helps.