如何生成密钥presses编程机器人 [英] How to Generate Key Presses Programmatically Android

查看:204
本文介绍了如何生成密钥presses编程机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,当用户presses DPAD_LEFT,我想生成两个DPAD_UP presses。我知道这是可以做到用这样的方法:

In my application when the user presses DPAD_LEFT, i want to generate two DPAD_UP presses. I know it can be done using a method like this:

@Override private boolean onKeyDown(int keyCode, KeyEvent event) {

   if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {

        keyDownUp(KeyEvent.KEYCODE_DPAD_UP);

        keyDownUp(KeyEvent.KEYCODE_DPAD_UP);

        return true;

   }
   return super.onKeyDown(keyCode,event);
}

private void keyDownUp(int a) {

        getCurrentInputConnection().sendKeyEvent(

                new KeyEvent(KeyEvent.ACTION_DOWN, a));

        getCurrentInputConnection().sendKeyEvent(

                new KeyEvent(KeyEvent.ACTION_UP, a));

}

不过,要能够使用getCurrentInputConnection()方法,我需要延长InputMethodService,这是不可能的原因我的应用程序已经扩展了另一个类。有另一种方式来解决这个问题?

But, to being able to use "getCurrentInputConnection()" method, i need to extend InputMethodService and it is impossible cause my application already extends another class. Is there another way to solve this?

推荐答案

创建其扩展InputMethodService另一个类,并从应用程序中调用它。

Create another class which is extending InputMethodService and call it from your application.

这篇关于如何生成密钥presses编程机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆