简体   繁体   中英

Simulating Keyboard Events in Android

I have a project and I am trying to fire Keyboard events. I am using the Instrumentation Class, and it is working perfectly on the emulator, but when I launch it on my device, the call doesn't work, is there any way to make it work?

Also, I searched on the web, and I found some posts about the IWindowManager and the internal APIs, I fully understand the risk of using them, but my project is a research and is not intended to be published, so I don't mind using those APIs in my porject if they can solve my problem, but the problem is that I can't find the JAR that contains these classes.

TLDR : I need a solution that helps me fire keyboard events on my Android device, any ideas are appreciated

Actually, there is an easier method, that I found Using Activity class or WebView class. Both have the same function. The code looks something like this:

webView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_1));

In instrumentation, there is the api to send keycode:

getInstrumentation().sendCharacterSync(int keycode);

If you have root access to the device, you can check the code here:

https://code.google.com/p/androidscreencast/source/browse/AndroidScreencastClient/src/net/srcz/android/screencast/client/ClientHandler.java ;

to use IWindowManager. The IWindowManager is apart of android system, you can find the class in android.jar, the source code can be downloaded from android official website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM