Cordova插件 - 键盘事件Android

时间:2016-08-26 19:56:37

标签: javascript java android cordova

我试图在Android上为Cordova创建插件,可以拦截硬件键盘事件,并将它们提供给Javascript侦听器。我以前创建了其他插件,但我正在努力解决这个问题,因为我在Java方面并不是特别强大。下面的代码片段中的所有内容编译都很好,但Javascript方面没有任何内容。对此的任何和所有帮助表示赞赏!

更新 经过一些研究后,默认情况下,在Android的更高版本中,关键事件似乎被禁用。这是由于一些BS借助于使用如此多的不同输入设备和方法来获得更好的用户体验。如果你是从头开始构建应用程序,那么帖子here似乎可以提供解决方案,但不适用于Cordova。 This post有一些可以用于Cordova的东西,但它似乎侵入了插件。我想覆盖CordovaWebView中的一些方法,但我不想重新初始化它。它是Javascript中的常见做法;有没有人知道如何在Java for Android中做到这一点?

KeyboardPlugin.java

package com.otb.cordova.keyboard;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.KeyEvent;
import android.view.*;

public class KeyboardPlugin extends CordovaPlugin implements OnKeyListener{
    private CallbackContext keyup_callback = null;
    private CallbackContext keydown_callback = null;

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {

        if (action.equalsIgnoreCase("keyUp")) {
            keyup_callback = callbackContext;
        }
        else if(action.equalsIgnoreCase("stopKeyUp")){
            keyup_callback = null;
        }
        else if(action.equalsIgnoreCase("keyDown")){
            keydown_callback = callbackContext;
        }
        else if(action.equalsIgnoreCase("stopKeyDown")){
            keydown_callback = null;
        }
        else {
            // invalid action
            return false;
        }

        PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
        result.setKeepCallback(true);
        callbackContext.sendPluginResult(result);

        return true;
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            KeyUp(keyCode, event);
        }
        else if (event.getAction() == KeyEvent.ACTION_DOWN) {
            KeyDown(keyCode, event);
        }
        return false;
    }

    private boolean KeyDown(int keyCode, KeyEvent event){
        if(keydown_callback == null){
            return true;
        }
        PluginResult result = new PluginResult(PluginResult.Status.OK, Integer.toString(keyCode));
        result.setKeepCallback(true);
        keydown_callback.sendPluginResult(result);
        return false;
    }

    private boolean KeyUp(int keyCode, KeyEvent event){
        if(keyup_callback == null){
            return true;
        }
        PluginResult result = new PluginResult(PluginResult.Status.OK, Integer.toString(keyCode));
        result.setKeepCallback(true);
        keyup_callback.sendPluginResult(result);
        return false;
    }
}

plugin.js

    Plugin.keyboardPlugin = {
  onKeyUp: function(callback, onFail){
    cordova.exec(callback, onFail, 'KeyboardPlugin', 'keyUp', []);
  },

  onKeyDown: function(callback, onFail){
    cordova.exec(callback, onFail, 'KeyboardPlugin', 'keyDown', []);
  },

  stopKeyUp: function(callback, onFail){
    cordova.exec(callback, onFail, 'KeyboardPlugin', 'stopKeyUp', []);
  },

  stopKeyDown: function(callback, onFail){
    cordova.exec(callback, onFail, 'KeyboardPlugin', 'stopKeyDown', []);
  }
};

的plugin.xml

    <?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
        xmlns:android="http://schemas.android.com/apk/res/android"
        id="cordova-plugin-keyboard"
        version="0.0.1">
    <name>Keyboard Plugin</name>
    <description>This is a project to read key strokes from Cordova on Android.</description>
    <license>Apache 2.0</license>
    <keywords>cordova,plugin,keyboard</keywords>
    <repo>https://github.com/mircerlancerous/cordova-plugin-keyboard</repo>
    <issue>https://github.com/mircerlancerous/cordova-plugin-keyboard/issues</issue>

    <engines>
        <engine name="cordova-android" version=">=3.6.0" /><!-- Requires CordovaPlugin.preferences -->
    </engines>


    <js-module src="www/plugin.js" name="keyboardPlugin">
        <runs/>
    </js-module>

    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="KeyboardPlugin" >
                <param name="android-package" value="com.otb.cordova.keyboard.KeyboardPlugin"/>
                <param name="onload" value="true" />
            </feature>
        </config-file>

        <source-file src="src/android/KeyboardPlugin.java" target-dir="src/com/otb/cordova/keyboard"/>
    </platform>

</plugin>

这里是github存储库的链接。 https://github.com/mircerlancerous/cordova-plugin-keyboard/

0 个答案:

没有答案