描述: IWindowManager 提供了模拟 KeyEvents的简单方法,名字叫做 IWindowManager.injectKeyEvent(...). 要模拟KeyStroke(按键敲击), 我们需要模拟一个 指定KeyCode 的 KeyEvent.ACTION_DOWN 和 KeyEvent.ACTION_UP
.
在这个例子里我们将要模拟 Menu按键的 KeyStroke , 以使 Menu 弹出.
下面是这个例子的源码
package org.anddev.android.simualtekeys;
import android.app.Activity;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.ServiceManager;
import android.view.IWindowManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class SimualteKeyInput extends Activity {
/* The WindowManager capable of injecting keyStrokes. */
final IWindowManager windowManager = IWindowManager.Stub
.asInterface(ServiceManager.getService("window"));
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
/* Make the button do the menu-popup. */
this.findViewById(R.id.cmd_simulate_key).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View arg0) {
/* Start the key-simulation in a thread
* so we do not block the GUI. */
new Thread(new Runnable() {
public void run() {
/* Simulate a KeyStroke to the menu-button. */
simulateKeystroke(KeyEvent.KEYCODE_SOFT_LEFT);
}
}).start(); /* And start the Thread. */
}
});
}
/** Create a dummy-menu. */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean supRetVal = super.onCreateOptionsMenu(menu);
menu.add(0, 0, "Awesome it works =)");
return supRetVal;
}
/** Wrapper-function taking a KeyCode.
* A complete KeyStroke is DOWN and UP Action on a key! */
private void simulateKeystroke(int KeyCode) {
doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyCode));
doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyCode));
}
/** This function actually handles the KeyStroke-Injection. */
private void doInjectKeyEvent(KeyEvent kEvent) {
try {
/* Inject the KeyEvent to the Window-Manager. */
windowManager.injectKeyEvent(kEvent.isDown(), kEvent.getKeyCode(),
kEvent.getRepeatCount(), kEvent.getDownTime(), kEvent
.getEventTime(), true);
} catch (DeadObjectException e) {
e.printStackTrace();
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button id="@+id/cmd_simulate_key"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Simulate Menu Key Press"
/>
</LinearLayout>
分享到:
相关推荐
这个程序是用vc编的,可以模拟键盘按键,即程序会自动按键。而且带有声音,很好的。
在Python中,可以使用第三方库...在模拟键盘操作之前,可能需要加入适当的延迟,以确保目标应用程序能够处理键盘输入。 请注意,模拟键盘操作可能会有一定的安全风险,特别是在自动化测试、远程控制等场景中,请谨
要注意利用它们提供的一些控制显示和键盘输入的函数和过程来简化输入和输出(包括动画)的设计。当然也可以利用编程语言提供的函数和过程来完成输入和输出。 (五)基本的要求和较高的要求 以下内容仅供有余力的同学...
内容索引:VC/C++源码,系统相关,语音报号,键盘钩子 模拟敲击键盘报语音的C++程序源代码,敲击键盘上的任意键,就会报出该键的声音,是一个键盘钩子程序,很不错。
好不容易搜集齐全的Arduino 37种传感器说明和测试程序:18B20温度传感器中文资料.pdf001温度传感器模块KY001.doc002震动开关模块KY002.doc003霍尔磁力传感器模块KY003.doc004按键开关模块KY004.doc005红外发射...
KBW输入模式,在这种模式下,扫描器将成为一个虚拟键盘,数据接收主机像接收真实键盘输入一样接受此虚拟键盘的输入,识读器解码得到数据后的发送过程便是敲击虚拟键盘中与数据对应的每一个按键。 2、USB DataPipe ...
11.键盘操作辅助类,提供属性访问敲击那个键,以及发送软键盘消息等操作 MyKeyboardHelper.cs。 12.Base64加密解密 MyBase64Helper.cs MD5加密等操作辅助类 MyMD5Helper.cs 13.鼠标辅助操作类,提供获取鼠标状态...
11.键盘操作辅助类,提供属性访问敲击那个键,以及发送软键盘消息等操作 MyKeyboardHelper.cs。 12.Base64加密解密 MyBase64Helper.cs MD5加密等操作辅助类 MyMD5Helper.cs 13.鼠标辅助操作类,提供获取鼠标状态以及...
键盘输入(KeyUp事件) 2.无焦点(按钮失去焦点) 3.实现优先级运算。比如直接输入1-2*3=-5,而不是微软计算器的-3 4.使用操作工厂,使用接口 5.实现菜单里的复制粘贴功能 6.可视化文本框 7.实现...
锁闭手机锁模式需要利用按键、距离感应或者双敲击解锁方式解锁,该功能模拟了系统侧滑键的功能,有震动反馈效果。静默锁不同于锁闭手机锁模式的地方时无振动反馈,该功能主要是对前两种模式的补充。以上三种模式可...