I'm spinning my wheels trying to create a plugin for Cordova on Android that can intercept hardware keyboard events, and feed them to a Javascript listener. I've created other plugins before but I am struggling with this one as I'm not particularly strong in Java to begin with. Everything in the code snippet below compiles fine but nothing comes out on the Javascript side. Any and all help on this one is appreciated!
Update: After some more research it looks like key events are disabled in later versions of Android by default. This is due to some BS excuse about better user experience with so many different input devices and methods. The post here seems to offer the solution if you're building the app from scratch, but not for Cordova. This post has something that could work for Cordova but it seems to invasive for a plugin. I'd like to override some of the methods in the CordovaWebView but I'd rather not reinitialize it. It's common practice in Javascript; does anyone know how to do it 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 {
// 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>
Here's the link to the github repository. https://github.com/mircerlancerous/cordova-plugin-keyboard/