dtech
dtech

Reputation: 14060

Catch D-Pad/Trackball Events in Android Web App

I've got an Android app which is mostly a wrapper around a WebView which show local files. The WebView gets the events because setContentView(thewebview) is called. This works fine most of the times (since we also handle Android Webkit Browser in the online version).

Problem is that I want to add support for D-Pad/Trackball events. I've written the appropriate onKeyDown's, but the WebView is consuming the events, not the activity.

This can be fixed in two ways:

  1. Handle the event keycode for D-Pad Down/Up/etc. in the Javascript onKeyDown function
  2. Get and consume the appropriate key events before the webview can get them. (ofcourse also all events can be gotten, most will simply not be consumed)

Problem is I don't know how to do either. How can I do this or solve this in a different way?

Upvotes: 1

Views: 1777

Answers (1)

Jacob Ras
Jacob Ras

Reputation: 5969

Simply 'catch' the keypresses on the WebView by using View.setOnKeyListener() to handle the KeyEvents.

public void setOnKeyListener (View.OnKeyListener l)

Register a callback to be invoked when a key is pressed in this view.

In your example that would be used like this:

thewebview.setOnKeyListener(new OnKeyListener()
{
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        // do your stuff here
    }
});

Upvotes: 2

Related Questions