How to call keyevent from another class

416 views Asked by At

I'm struggling with this issue. How I can call key event from another java class (keyboard.java) to run activity in this codereader.java class

I detect the key on keyboard.java by this

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();
    playClick(primaryCode);
    switch (primaryCode) {
        case -101:
            //do something with the key -101
            break;

And I need to call it here and run activity in codereader.java

public class QRscanner extends Activity implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onClick (View v){
    mScannerView = new ZXingScannerView(this);
    setContentView(mScannerView);
    mScannerView.setResultHandler(this);
    mScannerView.startCamera();

}

I have try codereader by it own and works perfectly when I call onCLick from activity_main.xml button.

2

There are 2 answers

0
Julius On

You can send an event via EventBus: https://github.com/greenrobot/EventBus

0
Mean Coder On

That is where Interfaces comes in ... create a custom interface ... implement it in the class which have the onkey method... Instantiate an object of your custom interface in activity class by casting the class implementing the interface to you custom interface ... call the interface method by interface method and you are done ... If the onKey is a callback method then there might be a better way ...