Throwing/Simulating KeyStrokes programatically
What you learn: You will learn how simulate KeyStrokes/KeyEvents on an Android-Device.
Difficulty: 1.5 of 5

What it will look like:
Before & After hitting the Button:


Description: The IWindowManager provides easy access to a function that simulates KeyEvents. It is called IWindowManager.injectKeyEvent(...). To simulate a complete KeyStroke, we have to simulate a KeyEvent.ACTION_DOWN AND KeyEvent.ACTION_UP on the specific KeyCode.
In this tutorial we will simulate a KeyStroke to the Menu-Button, to make the Menu popup.
And this is how it works:
Using java Syntax Highlighting
- 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();
- }
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="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>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
That's it 

If anything is still unclear, feel free to ask

Regards,
plusminus








. i think its because of the sdk update itself that some functions are depricated. anyway, for this tutorial, i m getting the following errors, plz help me 
