java 如何以编程方式生成按键操作 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4967178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Generate Key Presses Programmatically Android
提问by sjor
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:
在我的应用程序中,当用户按下 DPAD_LEFT 时,我想生成两个 DPAD_UP 按下。我知道可以使用这样的方法来完成:
@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));
}
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?
但是,为了能够使用“getCurrentInputConnection()”方法,我需要扩展 InputMethodService 并且这是不可能的,因为我的应用程序已经扩展了另一个类。有没有其他方法可以解决这个问题?
回答by Vladimir Ivanov
Create another class which is extending InputMethodService and call it from your application.
创建另一个扩展 InputMethodService 的类并从您的应用程序中调用它。
回答by n0sferat0k
If you want to generate a key event that gets passed through the event pipeline and is fully handled (characters as well as special keys such as arrows - resulting in change focus) you must tap into the base of things. you can create an implementation of input method service but this is laborious. An easyer way is to get the handler of the current view hierarchy and send a message to it like this:
如果您想生成一个通过事件管道传递并被完全处理的键事件(字符以及特殊键,如箭头 - 导致焦点变化),您必须深入了解事物的基础。您可以创建输入法服务的实现,但这很费力。一种更简单的方法是获取当前视图层次结构的处理程序并向其发送消息,如下所示:
public final static int DISPATCH_KEY_FROM_IME = 1011;
KeyEvent evt = new KeyEvent(motionEvent.getAction(), keycode);
Message msg = new Message();
msg.what = DISPATCH_KEY_FROM_IME;
msg.obj = evt;
anyViewInTheHierarchy.getHandler().sendMessage(msg);
Simple :)
简单的 :)
回答by Hayden Thring
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
回答by Nanne
I have not tried this (can't right now, sorry), and it does feel a bit like a hack, but couldn't you do something like this:
我还没有尝试过这个(现在不能,抱歉),它确实感觉有点像黑客,但你不能做这样的事情:
@Override private boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
super.onKeyDown(KeyEvent.KEYCODE_DPAD_UP,event);
return super.onKeyDown(KeyEvent.KEYCODE_DPAD_UP,event);
}
return super.onKeyDown(keyCode,event);
}
Ofcourse, you could create another class and call that from your application.
当然,您可以创建另一个类并从您的应用程序中调用它。