Show me the code! – By Davanum Srinivas

December 19, 2007

Android – Simulate key strokes

Filed under: Uncategorized — Davanum Srinivas @ 11:10 am

Here are the before and after screen shot(s).



We can use IWindowManager’s injectKeyEvent to simulate a series of key strokes

package org.apache.hello;

import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.ServiceManager;
import android.util.Log;
import android.view.IWindowManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

public class HelloApp extends Activity {

    final IWindowManager windowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));

     * Called with the activity is first created.
    public void onCreate(Bundle icicle) {
        Button fill = (Button) this.findViewById(;
        fill.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Thread t = new Thread(new Runnable() {
                    public void run() {
                        try {
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_N));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_N));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_D));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_D));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_R));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_R));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_O));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_O));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_I));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_I));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_D));
                            sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_D));
                        } catch (Exception e) {
                            Log.e("HelloApp", e.getMessage(), e);

    public void sendKeySync(KeyEvent event) throws DeadObjectException {
        windowManager.injectKeyEvent(event.isDown(), event.getKeyCode(), event.getRepeatCount(), event.getDownTime(), event.getEventTime(), true);


Download Source and APK from here –


  1. I am not able to download Can you please post the new link to the same.


    Comment by Savita Singh — January 22, 2009 @ 6:54 pm

  2. Is it possible to post the key strokes form background app?

    Comment by alsher — September 24, 2009 @ 1:43 pm

  3. Hi, i cudn’t find the IWindowManager class .. and i m getting import error.
    Please help .. how to do..?

    Comment by Anand — July 9, 2010 @ 6:41 am

  4. This app is a fake IWindowManager not exist.

    Comment by A — August 23, 2011 @ 10:45 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at


Get every new post delivered to your Inbox.

Join 136 other followers

%d bloggers like this: