commit 405de7314d8a61752ef17b5e12036e8f41d80bf1 Author: liangyc Date: Thu Jun 26 15:17:38 2025 +0800 PDA 监听物理按键插件 diff --git a/README.md b/README.md new file mode 100644 index 0000000..0b1eda1 --- /dev/null +++ b/README.md @@ -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) +}); +``` \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..e90c1d1 --- /dev/null +++ b/package.json @@ -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" +} \ No newline at end of file diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..65342dc --- /dev/null +++ b/plugin.xml @@ -0,0 +1,21 @@ + + + HardwareKey Plugin + + + + + + + + + + + + + + + + diff --git a/src/android/cordova/plugin/hardwareKey/HardwareKey.java b/src/android/cordova/plugin/hardwareKey/HardwareKey.java new file mode 100644 index 0000000..3864b6c --- /dev/null +++ b/src/android/cordova/plugin/hardwareKey/HardwareKey.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/android/libsref/scankey.jar b/src/android/libsref/scankey.jar new file mode 100644 index 0000000..fea2d13 Binary files /dev/null and b/src/android/libsref/scankey.jar differ diff --git a/www/hardwareKey.js b/www/hardwareKey.js new file mode 100644 index 0000000..d46b890 --- /dev/null +++ b/www/hardwareKey.js @@ -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;