gpt4 book ai didi

android - 用于将 keyUp 事件的键码传递给应用程序的 Cordova 插件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:42:02 24 4
gpt4 key购买 nike

我正在尝试创建一个 cordova 插件,它将“监听”任何 onKeyUp 事件,并将 keyCode 传递给回调函数。

目的是检测来自外部键盘/条形码扫描仪的任何击键 - 任何字符(例如 0,1,2,3...a,b,c,...)

我的问题是:如何添加 onKeyUp 监听器?

这是我目前所拥有的:

package il.co.pnc.cordova.keystrokes;

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 android.view.View;
import android.view.View.OnKeyListener;
import android.view.KeyEvent;

public class keystrokes extends CordovaPlugin {
private CallbackContext callback = null;

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

// Defining the callback
if ("register".equals(action)) {
this.callback = callbackContext;
}

return true;
}

}

// *** My problem is - I don't know where to put this:
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// Grab the "Key" character
String key = "";
if (event != null) {
key = String.valueOf((char)event.getUnicodeChar());
} else {
key = String.valueOf(Character.toChars(keyCode)[0]);
}
// Submit it back to the Javascript Callback function
/*PluginResult result = new PluginResult(PluginResult.Status.OK, key);
result.setKeepCallback(true);
this.callback.sendPluginResult(result);*/
// Pass on the event to Android
return super.onKeyUp(keyCode, event);
}

因此,我不在何处放置 onKeyUp。据我所知 - 它应该是主要 Activity 的一部分......?

最佳答案

我和你有同样的问题。我正在开发一个 PDA 项目。我从 https://github.com/mircerlancerous/cordova-plugin-keyboard/ 关注你到这里,哈哈。这是同样的问题Cordova Plugin - Keyboard Events Android .现在可以肯定的是,仅当来自硬件键的按键事件时,我们不能使用onkeydown,onkeyup,OnKeyListener来抓取按键事件。经过我的测试,我们可以通过重写dispatchkeyevent来抓取“up down left right enter backspace back menu VolumeButtons”但只能在 Activity 中,或者只是我不知道如何覆盖 CordovaPlugin 中的 Activity 功能。还有另一种方法通过实现 OnKeyListener,但只能获取“VolumeButtons 菜单”```

package com.manueldeveloper;
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 android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.widget.Toast;
public class VolumeButtonsListener extends CordovaPlugin implements OnKeyListener {
private static final String VolumeButtonsListener_LOG= "VolumeButtonsListener";
private CallbackContext volumeCallbackContext;
public VolumeButtonsListener(){
volumeCallbackContext= null;
}
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() { cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE); cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});
// Check the action
if( action.equals("start") ){
// Check if the plugin is listening the volume button events
if( this.volumeCallbackContext != null ){
callbackContext.error("Volume buttons listener already running");
return true;
}
// Get the reference to the callbacks and start the listening process
this.volumeCallbackContext= callbackContext;
this.webView.getView().setOnKeyListener(this);
// Don't return any result now
PluginResult pluginResult= new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
this.volumeCallbackContext.sendPluginResult(pluginResult);
return true;
}
else if( action.equals("stop") ){
// Erase the callbacks reference and stop the listening process
sendSignal(new JSONObject(), false); // release status callback in Javascript side
this.volumeCallbackContext= null;
this.webView.getView().setOnKeyListener(null);
callbackContext.success();
return true;
}
return false;
}
public void onDestroy(){
// Stop the listening process
this.webView.getView().setOnKeyListener(null);
}
public void onReset(){
// Stop the listening process
this.webView.getView().setOnKeyListener(null);
}
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
keyCode = keyEvent.getKeyCode();
JSONObject infoA= new JSONObject();
try{
infoA.put("signal", new String("keyCode:"+keyCode));
sendSignal(infoA, true);
return true;
}
catch(JSONException ex){
Log.e(VolumeButtonsListener_LOG, ex.getMessage());
}
// Check if the event is equal to KEY_DOWN
if( keyEvent.getAction() == KeyEvent.ACTION_UP )
{
// Check what button has been pressed
if( keyCode == KeyEvent.KEYCODE_SPACE ){//KEYCODE_VOLUME_UP
// Create a new JSONObject with the information and send it
JSONObject info= new JSONObject();
try{
info.put("signal", new String("volume-up"));
sendSignal(info, true);
return true;
}
catch(JSONException ex){
Log.e(VolumeButtonsListener_LOG, ex.getMessage());
}
}
else if( keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ){//KEYCODE_VOLUME_DOWN
// Create a new JSONObject with the information and send it
JSONObject info= new JSONObject();
try{
info.put("signal", new String("volume-down"));
sendSignal(info, true);
return true;
}
catch(JSONException ex){
Log.e(VolumeButtonsListener_LOG, ex.getMessage());
}
}
}
return true;
}
private void sendSignal(JSONObject info, boolean keepCallback)
{
if( this.volumeCallbackContext != null ){
PluginResult result= new PluginResult(PluginResult.Status.OK, info);
result.setKeepCallback(keepCallback);
this.volumeCallbackContext.sendPluginResult(result);
}
}
public boolean dispatchKeyEvent(KeyEvent event) {
/*if (event.getAction() == KeyEvent.ACTION_UP){
Log.e("activity=","ACTION_UP"+event.getKeyCode());
return true;
}*/
//return super.dispatchKeyEvent(event);
return true;
}
}

```有一个方法没有被测试。 DispatchKeyEvent to listen for Spacebar being pressed

关于android - 用于将 keyUp 事件的键码传递给应用程序的 Cordova 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41862667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com