科尔多瓦捕捉向上,向下,向左,向右按钮

时间:2016-03-11 10:47:20

标签: javascript java android jquery cordova

我正在为android 4.4.2设备编写一个cordova应用程序。这个设备有一个遥控器,我需要抓住 Up Down Left Right -keys。

不幸的是,cordova仅支持捕获上传,下推,菜单,搜索和后退键的事件(See documentation

所以我试着编写自己的插件,但不幸的是我被困住了,不知道该怎么办。

Java-Part(简化):

public OwnPlugin() {}

  public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
  }

  public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    if (action.equals("overrideUpButton")) {
      this.overrideUpButton(args.getBoolean(0));
    }
    return true;
  }

  public void overrideUpButton(boolean override) {
    webView.setButtonPlumbedToJs(KeyEvent.KEYCODE_DPAD_UP, override);
  }
}

JS-Part(简化):

cordova.define("com. ... ", function(require, exports, module) {
var exec = require('cordova/exec');

function OwnPlugin() {
}

OwnPlugin.prototype.overrideUpButton = function(override, onsucess, onfail){
 exec(onsucess, onfail, "OwnPlugin", "overrideUpButton" ,[ override ]);
};


 var ownPlugin = new OwnPlugin();
 module.exports = ownPlugin;
});

当尝试使用OwnPlugin.overrideUpButton(true);覆盖按钮时,这给了我一个例外:

E/PluginManager: Uncaught exception from plugin
E/PluginManager: java.lang.IllegalArgumentException: Unsupported keycode: 19
E/PluginManager:     at org.apache.cordova.CordovaWebViewImpl.setButtonPlumbedToJs(CordovaWebViewImpl.java:366)

好的...这种方法不起作用,因为CordovaWebViewImpl.java

不支持我需要的按钮代码

那我现在该怎么办?如何捕获这些密钥代码,以便我可以阻止默认操作,并在JavaScript中使用它们?

  • KEYPAD_DPAD_UP
  • KEYPAD_DPAD_DOWN
  • KEYPAD_DPAD_LEFT
  • KEYPAD_DPAD_RIGHT

1 个答案:

答案 0 :(得分:1)

您可以使用javascript来抓住这些密钥

window.addEventListener("keydown", moveSomething, false);
 
function moveSomething(e) {
    switch(e.keyCode) {
        case 37:
            // left key pressed
            break;
        case 38:
            // up key pressed
            break;
        case 39:
            // right key pressed
            break;
        case 40:
            // down key pressed
            break;  
    }   
}