2017-01-25 17 views
6

मैं एक कॉर्डोबा-प्लगइन है कि किसी भी onKeyUp घटना के लिए "सुन" होगा, और उसे कॉलबैक फ़ंक्शन पर keyCode पारित करेंगे बनाने के लिए कोशिश कर रहा हूँ करने के लिए एक KeyUp घटना के कीकोड पासिंग के लिए।Cordova प्लगइन अनुप्रयोग

उद्देश्य किसी भी कीस्ट्रोक कि एक बाहरी कीबोर्ड/बारकोड स्कैनर से आता है का पता लगाने है - किसी भी चरित्र (जैसे 0,1,2,3 ... एक, ख, ग, ...)

मेरे समस्या यह है: मैं 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 जगह नहीं कर रहा हूँ:

यहाँ मैं अब तक है। जहां तक ​​मुझे पता है - यह मुख्य गतिविधि का हिस्सा होना चाहिए ...?

उत्तर

0

मुझे आपके साथ एक ही समस्या का सामना करना पड़ता है। मैं एक पीडीए प्रोजेक्ट विकसित कर रहा हूं। मैं आपको https://github.com/mircerlancerous/cordova-plugin-keyboard/ से यहां तक, हाहा का अनुसरण करता हूं। और यह वही प्रश्न Cordova Plugin - Keyboard Events Android है। अब यह निश्चित है कि हम ऑनकीडाउन, ऑनकीप, ऑनके लिस्टनर के साथ कुंजी-ईवेंट को पकड़ सकते हैं, केवल हार्डवेयर कुंजी से कुंजी-ईवेंट। मेरे परीक्षण के बाद, हम डिस्पैचकीवेंट को ओवरराइड करके "बाएं दाएं दाएं बैकस्पेस बैक मेनू वॉल्यूमबटन" दर्ज कर सकते हैं लेकिन केवल गतिविधि में, या सिर्फ यह नहीं कि मैं कॉर्डोवाप्लगिन में गतिविधि के एक समारोह को ओवरराइड करने के बारे में नहीं जानता। वहाँ भी लागू द्वारा एक और तरीका 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

संबंधित मुद्दे