手柄的WebView谷歌电视的应用程序从D-键盘方向键 [英] Handle arrow keys from D-pad on WebView Google Tv app

查看:279
本文介绍了手柄的WebView谷歌电视的应用程序从D-键盘方向键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个Android应用程序,它加载的WebView功能HTML页面,并且工作正常,但事实上,行动应该发生在D垫箭头键将无法正常工作。如果我改变了与其他键箭头行动,它的工作原理。加载HTML网页的Web浏览器工作正常,PC键盘上的箭头键返回正确的行动,但在Android中的WebView,D-键盘方向键无法正常工作。

I have built an Android app which loads a html page in WebView, and it is working ok, except the fact that actions should happen on D-pad arrow keys won't work. If I change action for arrows with other keys, it works. Loading the html page in web browser works fine, PC keyboard arrow keys return correct actions, but in Android WebView, D-pad arrow keys aren't working.

这就是我的老虎键pressed在JS:

This is how I tiger key pressed in js:

window.addEventListener('keydown', keyDownHandler, true);
function keyDownHandler(evt){
var keyCode=evt.keyCode;
alert(keyCode);
}

除了方向键,pressing任何其他键返回键code,而箭头则不会。

Except arrow keys, pressing any other key returns the key code, but arrows won't.

这可能是因为在这里复制:<一href="http://stackoverflow.com/questions/10172852/android-webview-handle-arrow-keys-in-javacript">android WebView功能:处理在javacript 箭头键,但没有找到解决工作

This might be duplicate as here: android WebView: Handle arrow keys in javacript but didn't find a solution to work.

有没有什么办法可以让D-键盘方向键codeS在Android中的WebView?

Is there any way I can get the D-pad arrow keys codes in Android WebView?

推荐答案

上有所谓的WebAppNativePlayback谷歌电视采样的示例应用程序:<一href="https://$c$c.google.com/p/googletv-android-samples/source/browse/#git%2FWebAppNativePlayback">https://$c$c.google.com/p/googletv-android-samples/source/browse/#git%2FWebAppNativePlayback

There is an example app on the Google TV samples called WebAppNativePlayback: https://code.google.com/p/googletv-android-samples/source/browse/#git%2FWebAppNativePlayback

本质上,D - 垫是由本地应用所消耗,所以你需要处理的是,如果你使用的是全屏的WebView您可以通过它注入JS通过相关钥匙的WebView。

Essentially the d-pad is consumed by the native application, so you need to handle that, if you using a fullscreen WebView you can pass the relevant keys to the WebView by injecting it into JS.

主件code要注意的是:

The main pieces of code to pay attention to are:

在活动,消耗的关键事件,并通过了下来:

In the Activity, consume key events and pass down:

/**
 * This method will check if the key press should be handled by the system
 * or if we have chosen to override it to pass to the WebView. In
 * development builds of the application, the R key is used refresh the page
 * (required to ensure cached versions of the page are not used)
 */
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (mIsDevelopmentBuild && event.getKeyCode() == KeyEvent.KEYCODE_R) {
        mWebViewFragment.refresh();
    }

    int eventKeyCode = event.getKeyCode();
    for (int i = 0; i < mOverrideKeyCodes.length; i++) {
        if (eventKeyCode == mOverrideKeyCodes[i]) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                mWebViewFragment.handleKeyInjection(eventKeyCode);
            }
            return true;
        }
    }

    return super.dispatchKeyEvent(event);
}

当替代键是:

Where the override keys are:

mOverrideKeyCodes = new int[] {
                KeyEvent.KEYCODE_DPAD_CENTER,
                KeyEvent.KEYCODE_DPAD_UP,
                KeyEvent.KEYCODE_DPAD_LEFT,
                KeyEvent.KEYCODE_DPAD_DOWN,
                KeyEvent.KEYCODE_DPAD_RIGHT
        };

在其中的WebView住(虽然这可能是您的活动)的片段:

In the Fragment where the webview lives (although this may be in your activity):

/**
 * Given a key code, this method will pass it into the web view to handle
 * accordingly
 * 
 * @param keycode Native Android KeyCode
 */
public void handleKeyInjection(int keycode) {
    String jsSend = "javascript:androidKeyHandler.handleUri('nativewebsample://KEY_EVENT;"
            + keycode + ";');";
    loadJavascriptAction(jsSend);
}

loadJavascriptAction简直就是

loadJavascriptAction is simply

mWebView.loadUrl(jsSend);

然后在您的网页,你需要设置的使用方法或对象 - 在这种情况下,应用程序设置对象window.androidKeyHandler

Then in your web page, you need to set an accessible method or object - in this case the app sets an object window.androidKeyHandler

/**
* This method will set up any additional key handling (i.e. Android key handling)
* @function
*/
IndexPage.prototype.setUpKeyHandling = function () {
    if(this.isEmbedded()) {
        // We want the native app to access this
        window.androidKeyHandler = new AndroidKeyHandler(this.getFocusController());
    }
};

这不是处理的按键像这样:

Which than handles the keys like so:

/**
* Handle a keypress directly from the native app
* @function
* @param {int} keyCode The native Android key code
*/
AndroidKeyHandler.prototype.handleNativeKeyPress = function (keyCode) {
    var focusController = this.getFocusController();
    switch(parseInt(keyCode, 10)) {
        case 23:
            // DPAD Center
            console.log("Native Enter");
            if(focusController.getCurrentlyFocusedItem()) {
                focusController.getCurrentlyFocusedItem().onItemClick();
            }
            break;
        case 20:
            // DPAD Down
            console.log("Native Down Pressed");
            focusController.moveFocus({x: 0, y: -1});
            break;
        case 21:
            // DPAD Left
            console.log("Native Left Pressed");
            focusController.moveFocus({x: -1, y: 0});
            break;
        case 22:
            // DPAD Right
            console.log("Native RIGHT Pressed");
            focusController.moveFocus({x: 1, y: 0});
            break;
        case 19:
            // DPAD Up
            console.log("Native UP Pressed");
            focusController.moveFocus({x: 0, y: 1});
            break;
        default:
            console.log("Keycode not registered");
            break;
    }
};

这个例子可能是复杂得多比它需要的,但如果你通过每一件上面找份工作,尝试一下,你应该不存在太多的麻烦

This example is probably far more complex than it needs to be, but if you get work through each piece above and try it out, you should get there without too much hassle

这篇关于手柄的WebView谷歌电视的应用程序从D-键盘方向键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆