I spin my wheels trying to create a plugin for Cordoba on Android that can capture hardware keyboard events and pass them to a Javascript listener. I have created other plugins before, but I am struggling with this since I am not particularly strong in Java for starters. Everything in the code snippet below compiles fine, but nothing works on the Javascript side. Any help on this is appreciated!
Update: After some more research, it seems that key events are disabled by default in later versions of Android. This is due to some BS dictum about best practices with many different input devices and methods. The post here seems to offer a solution if you are building an application from scratch, but not for Cordoba. There is something in this post that might work for Cordoba, but it seems invasive for the plugin. I would like to override some methods in CordovaWebView, but I would prefer not to reinitialize it. This is a common practice in Javascript; Does anyone know how to do this in 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 {
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" /> </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>
Here's a link to the github repository. https://github.com/mircerlancerous/cordova-plugin-keyboard/