如何将关键事件从服务发送到其他应用

时间:2019-05-26 18:51:41

标签: android

我想将keyEvent从InputMethodService发送到手机中的其他应用程序

我想制作一个游戏手柄应用。它安装在2手机中并与蓝牙连接。它几乎完成了,但是sendkeyEvent没有发送到其他应用程序。

public class EDMTKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private KeyboardView kv;
    private Keyboard keyboard;

    private  boolean isCaps = false;
    private Intent intent;
    public InputConnection myconnection;


    @Override
    public void onStart(Intent intent, int startid) {
        String Receive = intent.getStringExtra("Command");
        Toast.makeText(this, "Service Started "+Receive, Toast.LENGTH_LONG).show();
        InputConnection ic = myconnection;
        if(Receive=="SHOOT"){
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_B));
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_B));
        }
        if(Receive=="SWITCH"){
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_X));
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_X));
        }
        if(Receive=="Forward"){
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP));
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP));
        }
        if(Receive=="Backward"){
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
        }
        if(Receive=="Left"){
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_LEFT));
        }
        if(Receive=="Right"){
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_RIGHT));
        }

    }
    public void onBindInput (){
        Toast.makeText(this, "Service Started onBindInput", Toast.LENGTH_LONG).show();
        myconnection =getCurrentInputConnection();


    }

0 个答案:

没有答案