Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Simulating keypress events on Android

, 20 Sep 2012 CC (ASA 3U)
Rate this:
Please Sign up or sign in to vote.
Artificially generating keyevents on any Android device.

Modern day smart-phones have already begun to migrate from the traditional "a-button-for-every-need" approach to the "huge-display-cum-touchscreen" form-factors. Android phones are no exception. But, traditional buttons are still required for a few oft-used functions (power,back,home,menu etc.) and smart-phones continue to have them alongwith the primary touch-based-UI.

Android-OS provides a very easy method to simulate key/button press/release events via software. You might ask why do we need a software to generate the events when a hardware button is already present on the device. Here's why:

  • During development/testing of the button-drivers itself.

  • To implement automated rigorous tests. ( MonkeyTest? )

  • To implement/interface additional custom software keyboards.

  • Just because we can!

Now, the basic goal of this exercise is extremely simple:

Q. How to generate a hardware-button-press event
WITHOUT actually pressing any key on the device?

Let us first understand what happens when a hardware button is pressed. Here is what happens when you press a button on an Android device:

  1. The h/w button triggers an interrupt.

  2. The ISR of the corresponding driver gets called (in kernel).

  3. In the ISR, the driver generates an input-event.

  4. Android-framework (in userspace) gets the notification of the event.

  5. Android reads the input-event code & type.

  6. Compares it with the proper "keymap/keylayout" file.

  7. The proper button is identified.

To simulate button presses we enter this procedure at STEP3.

Instead of the regular driver generating the input-event, we generate an input-event ourselves using a pre-built userspace binary. This will then be notified to the Android-framework and the rest continues as above.

So now all depends on the the pre-built userspace binary ("command" for the non-geek community), which is...

[ drum-roll... ]

...input. (How convenient!)

The syntax of input is as follows:

input keyevent <event_code> 

Now, before we try to send any keyevent, we need to find out the event-code that maps to the h/w key we want to simulate. The following table summarizes all the supported keycodes in Android:

Additionally we can look up the keylayout (.kl) file on the device. Personally, I find keylayout a misnomer, as we are not talking about different keyboards here, but different mappings of an input-event-value to its functionality.

Anyways you can always find the file in

/system/usr/keylayout/<device-name>.kl 

For each device, the android.keylayout.<device-name> system property must be set. If a keylayout file is not specified, Android will default to 

 /system/usr/keylayout/qwerty.kl 

Now to generate a event of a specific keycode, we simply execute this on the terminal/serial console:

input keyevent <keycode> 

The value of keycode can be any of the integer values from the above table. In case a serial-console is NOT available on your device, you can always run the command via adb as follows:

adb shell input keyevent <keycode> 

That's how a hardware-button-press is simulated in software!!


Further Reading

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License

Share

About the Author

TheCodeArtist
Software Developer (Senior) Nvidia Corporation
India India
Chinmay V S (a.k.a TheCodeArtist) is a Senior software engineer at NVIDIA Corporation - India. With more than 4 Years of hands-on experience in Linux Kernel and Android BSP development, he has been actively involved in developing and integrating drivers for the Linux-kernel and hardware abstraction layers for OMAP4 based Android smartphones, automotive navigation and infotainment devices.
 
In his spare time, he can be seen lurking in the shadows on StackOverflow and various Linux kernel mailing-lists. He enjoys sharing his experiences with technology as TheCodeArtist on his blog.
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 20 Sep 2012
Article Copyright 2012 by TheCodeArtist
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid