PDA 监听物理按键插件
This commit is contained in:
37
README.md
Normal file
37
README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# PDA 监听物理按键插件
|
||||
*青春塔项目PDA(AUTOID UTouch X3)设备监听物理按键插件*
|
||||
|name|code|
|
||||
|:--:|:--:|
|
||||
|F1|252|
|
||||
|音量+|115|
|
||||
|音量-|114|
|
||||
|UHF|248|
|
||||
|SCAN|249|
|
||||
|扫描键(手柄上的)|250|
|
||||
|
||||
|
||||
## 安装插件
|
||||
`cordova plugin add git+https://m.shuto.cn:8681/center/cordova-plugin-hardwareKey.git`
|
||||
|
||||
## 引入插件
|
||||
`declare const cordova: any;`
|
||||
|
||||
## 使用方法
|
||||
### 注册按键监听.
|
||||
keycode:需要监听的按键keycode数组,可不传,默认监听所有按键
|
||||
```
|
||||
cordova.plugin.keystroke.register({
|
||||
keycode: [249,250],
|
||||
success: res => console.log(res),
|
||||
error: err => console.error(err)
|
||||
});
|
||||
```
|
||||
返回值:`{action: 'keyup', keycode: 250}`
|
||||
|
||||
### 取消按键监听.
|
||||
```
|
||||
cordova.plugin.keystroke.register({
|
||||
success: res => console.log(res),
|
||||
error: err => console.error(err)
|
||||
});
|
||||
```
|
||||
17
package.json
Normal file
17
package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "cordova-plugin-hardwarekey",
|
||||
"version": "1.0.0",
|
||||
"description": "hardwareKey plugin",
|
||||
"cordova": {
|
||||
"id": "cordova-plugin-hardwareKey",
|
||||
"platforms": [
|
||||
"android"
|
||||
]
|
||||
},
|
||||
"keywords": [
|
||||
"cordova",
|
||||
"plugin"
|
||||
],
|
||||
"author": "yc",
|
||||
"license": "Apache-2.0"
|
||||
}
|
||||
21
plugin.xml
Normal file
21
plugin.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<plugin id="cordova-plugin-hardwareKey" version="0.0.1"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android" xmlns="http://apache.org/cordova/ns/plugins/1.0">
|
||||
<name>HardwareKey Plugin</name>
|
||||
<js-module name="cordova-plugin-hardwareKey" src="www/hardwareKey.js">
|
||||
<clobbers target="cordova.plugin.hardwareKey"/>
|
||||
</js-module>
|
||||
<platform name="android">
|
||||
<config-file parent="/*" target="res/xml/config.xml">
|
||||
<feature name="HardwareKey">
|
||||
<param name="android-package" value="cordova.plugin.hardwareKey.HardwareKey"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
|
||||
<source-file src="src/android/cordova/plugin/hardwareKey/HardwareKey.java"
|
||||
target-dir="cordova/plugin/hardwareKey"/>
|
||||
|
||||
<lib-file src="src/android/libsref/scankey.jar" />
|
||||
|
||||
</platform>
|
||||
</plugin>
|
||||
115
src/android/cordova/plugin/hardwareKey/HardwareKey.java
Normal file
115
src/android/cordova/plugin/hardwareKey/HardwareKey.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package cordova.plugin.hardwareKey;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.seuic.scankey.IKeyEventCallback;
|
||||
import com.seuic.scankey.ScanKeyService;
|
||||
|
||||
|
||||
public class HardwareKey extends CordovaPlugin {
|
||||
private final ScanKeyService scanKeyService = ScanKeyService.getInstance();
|
||||
private CallbackContext callbackContext = null;
|
||||
private boolean hasReceiver = false; // 是否有注册按键监听
|
||||
private final String KEY_CODE_DEFAULT = "114,115,248,249,250,252"; // 需监听按键的code
|
||||
private String keycodes = KEY_CODE_DEFAULT;
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
switch (action) {
|
||||
case "register":
|
||||
this.registerReceiver(callbackContext, args);
|
||||
return true;
|
||||
case "unregister":
|
||||
this.unregisterReceiver(callbackContext);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private final IKeyEventCallback keyEventCallback = new IKeyEventCallback.Stub() {
|
||||
@Override
|
||||
public void onKeyDown(int keyCode) {
|
||||
try {
|
||||
PluginResult result = new PluginResult(PluginResult.Status.OK,
|
||||
new JSONObject().put("action", "keydown").put("keycode", keyCode));
|
||||
result.setKeepCallback(true);
|
||||
callbackContext.sendPluginResult(result);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKeyUp(int keyCode) {
|
||||
try {
|
||||
PluginResult result = new PluginResult(PluginResult.Status.OK,
|
||||
new JSONObject().put("action", "keyup").put("keycode", keyCode));
|
||||
result.setKeepCallback(true);
|
||||
callbackContext.sendPluginResult(result);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void registerReceiver(CallbackContext callbackContext, JSONArray args) {
|
||||
try {
|
||||
JSONArray codeList = args.getJSONArray(0);
|
||||
if(codeList.length() > 0) {
|
||||
keycodes = codeList.join(",");
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
// 没有传keycode,监听所有按键
|
||||
}
|
||||
this.callbackContext = callbackContext;
|
||||
scanKeyService.registerCallback(keyEventCallback, keycodes);
|
||||
hasReceiver = true;
|
||||
Log.d("HardwareKey_Plugin", "开始监听物理按键!");
|
||||
}
|
||||
|
||||
private void unregisterReceiver(CallbackContext callbackContext) {
|
||||
Log.d("HardwareKey_Plugin", "停止监听物理按键!");
|
||||
closeCallback();
|
||||
callbackContext.success("ok");
|
||||
}
|
||||
|
||||
private void closeCallback() {
|
||||
scanKeyService.unregisterCallback(keyEventCallback);
|
||||
if (callbackContext != null) {
|
||||
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
|
||||
result.setKeepCallback(false);
|
||||
callbackContext.sendPluginResult(result);
|
||||
callbackContext = null;
|
||||
}
|
||||
hasReceiver = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause(boolean multitasking) {
|
||||
super.onPause(multitasking);
|
||||
if(hasReceiver) {
|
||||
scanKeyService.unregisterCallback(keyEventCallback);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume(boolean multitasking) {
|
||||
super.onResume(multitasking);
|
||||
if(hasReceiver) {
|
||||
scanKeyService.registerCallback(keyEventCallback, keycodes);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
closeCallback();
|
||||
}
|
||||
}
|
||||
BIN
src/android/libsref/scankey.jar
Normal file
BIN
src/android/libsref/scankey.jar
Normal file
Binary file not shown.
13
www/hardwareKey.js
Normal file
13
www/hardwareKey.js
Normal file
@@ -0,0 +1,13 @@
|
||||
var exec = require('cordova/exec');
|
||||
|
||||
var hardwareKey = function HardwareKey() {};
|
||||
|
||||
hardwareKey.register = function({success, error, keycode}) {
|
||||
exec(success, error, 'HardwareKey', 'register', [keycode]);
|
||||
};
|
||||
|
||||
hardwareKey.unregister = function({success, error}) {
|
||||
exec(success, error, 'HardwareKey', 'unregister');
|
||||
};
|
||||
|
||||
module.exports = hardwareKey;
|
||||
Reference in New Issue
Block a user