Android Question Help with keys management

JazzPiano

New Member
Hi,

Does anyone know how to deal with the Fire tv keys on a webapp that uses webview to navigate through a webpage with touchable images and buttons?
The problem is that when dealing with the Activity_KeyPress() to handle the back button and go back or exit the application:

Code:
Sub Activity_KeyPress(KeyCode As Int)
    Dim p As Phone
    Dim handled As Boolean = False
    Select KeyCode
        Case KeyCodes.KEYCODE_BACK
            If myWebViewExtras.CanGoback = True Then
                WebView1.Back
            Else
                Msgbox2Async ("Want to exit?","PitortApp","Yes","","No",Null,True)
                Wait For Msgbox_Result (value As Int)
                If value = DialogResponse.POSITIVE Then
                    WebView1.Visible = False
                    Activity.Finish
                End If
            End If

I don't know how to manage the D-Pad keys. I detect them pressed:

code:
Case KeyCodes.KEYCODE_DPAD_CENTER
    handled = True
Case KeyCodes.KEYCODE_DPAD_UP
    handled = True
Case KeyCodes.KEYCODE_DPAD_DOWN
    handled = True
Case KeyCodes.KEYCODE_DPAD_LEFT
    handled = True
Case KeyCodes.KEYCODE_DPAD_RIGHT
    handled = True

I think I have to propagate the event to let the webview handle it. But I don't know how.
I had a similar problem due to the use of the Activity_KeyPress Sub with the devices (phones and tables) volume control but I solved as follows:

code:
Case KeyCodes.KEYCODE_VOLUME_UP
    p.SetVolume(p.VOLUME_MUSIC, p.GetVolume(p.VOLUME_MUSIC)+1, True)
          
Case KeyCodes.KEYCODE_VOLUME_DOWN
    p.SetVolume(p.VOLUME_MUSIC, p.GetVolume(p.VOLUME_MUSIC)-1, True)

Any idea?
 

JazzPiano

New Member
If the WebView is focused then it will receive the keys events. You can test it with:
B4X:
Sub Activity_KeyPress(KeyCode As Int)
Log(KeyCode)
End Sub
See if it logs anything when the WebView is focused.
Thanks Erel,

The WebView is focused (I guess) since I get the Back Key event and I handle it properly (I get logs too). The problem is that since the Sub Activity_KeyPress is in place, I have to handle all the key events (I had to add specific code to deal with volume keys for mobile and tablet) and I don't know how to deal with Fire TV D-Pad keys (that are correctly detected within the Sub but that event should come back to the WebView to allow navigation within the webpage)

Amazon suggests something like this (I interpret that this last return super.dispatchKeyEvent(event); propagates the event outside the Sub):

Exemple:
public class MainActivity extends Activity {
    private final String mBaseUrl = "https://mysampleapp.com";
    private Context mContext = null;
    private WebView mWebView;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }
      ...
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.d(TAG, "dispatchKeyEvent : KeyCode = " + event.getKeyCode() + " (" + KeyEvent.keyCodeToString(event.getKeyCode()) + ")");
        switch (event.getKeyCode()) {
            // Handle back button from D-Pad
            case KeyEvent.KEYCODE_BACK:
                Log.d(TAG, "dispatchKeyEvent : mWebView.canGoBack = " + mWebView.canGoBack());
                if (mWebView.canGoBack()) {
                    // Navigate back on WebView
                    mWebView.goBack();
                } else {
                    // WebView has no more history, exit the app
                    finish();
                }
        }
        return super.dispatchKeyEvent(event);
    }
}

Before adding the Sub Activity_KeyPress, I could navigate through the webpage within the WebView on devices (touch) and on Fire TV (D-Pad) but the Back Key exited the application. With the Sub, navigation is Ok for touch devices but not for Fire TV.
 
Upvote 0

JazzPiano

New Member
The Activity_KeyPress will not handle all keys. Only keys that are not handled by WebView.

Solved!!

You just pointed out the solution. It is about requesting back the focus for the WebView to let it handle other key events not managed in the Sub Activity_KeyPress.

Thank you very much!
 
Upvote 0
Top