Issue
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:
- Handle the event keycode for D-Pad Down/Up/etc. in the Javascript onKeyDown function
- 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?
Solution
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
}
});
Answered By - Jacob Ras
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.