Button Pressing Emulation On Android

From DAVE Developer's Wiki
Revision as of 07:59, 1 October 2012 by DevWikiAdmin (talk | contribs) (Created page with "{{InfoBoxTop}} {{AppliesToAndroid}} {{InfoBoxBottom}} Category:HowTo == Introduction == Sometimes, during the development phases, real hardware buttons are not availab...")

(diff) ← Older revision | Approved revision (diff) | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Info Box
Android-logo.jpg Applies to Android


Introduction[edit | edit source]

Sometimes, during the development phases, real hardware buttons are not available, causing bad interaction with the Android interface. It would help to have an emulator to send events as button pressing to the Android system, and it's here that the MonkeyRunner tool shows its usefulness.

Emulating key pressing[edit | edit source]

Once the connection is established, you can run MonkeyRunner from your PC and enter the commands from the interactive shell. As an example, the following sequence emulates the "HOME" button pressing down:

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection(10,"emulator-5554")

# Presses the Home button
device.press('KEYCODE_HOME',MonkeyDevice.DOWN_AND_UP)