How to Use ADB to Control Keypress events on Android

Ads by Google

WebYou might be wondering why on earth would someone want to control his Android phone using the ADB commands. Well, as long as all your device hardware keys are working fine and the touch screen panel works flawlessly, you won’t find the article of any use.. until the day arrives when one of the back or menu buttons go kaput. That’s when you’ll visit your bookmarks to look up this article (hint: bookmark this!).

I will be covering how you can control your Android using virtual hardware keys on some of the applications on your device later in a separate post. But before that I will have to make arrangements so that you can install those apps on your device even when the keys are not working. Come on, no one keeps such apps installed on their Android beforehand, right? This post is for the users who already have non-functioning Android hardware keys.

Before we continue, install Android SDK on your computer and enable ADB Platform tools. You can have a look at our article on Chrome for Android debugging where we have discussed it in the first two steps. Having done that, add the path of ADB files to Windows Environment Variables. If you don’t know how to do that, refer to out article on Windows keyword and have a look at steps 3 to 6. This makes sure you can use the ADB command throughout Windows. Also make sure that your device drivers are installed as well.

Waking Up Phone.

adb reboot

Before we use any of the key events, you will have to wake up your phone first and with the power button not working, it would not be an easy task. Sadly I was not able to find any command that could wake the phone but you can always reboot your phone using the command adb reboot and once the phone reboots, use the following keypress events to install the applications.

Use Key Events

After the phone wakes up, you can use the following syntax to control: use the command adb shell input keyevent <keycode>.

adb shell input

Just replace the keycode with the numbers corresponding to the events you would like to perform as mentioned in the table.

Key Code

Events

0

KEYCODE_UNKNOWN

1

KEYCODE_MENU

2

KEYCODE_SOFT_RIGHT

3

KEYCODE_HOME

4

KEYCODE_BACK

5

KEYCODE_CALL

6

KEYCODE_ENDCALL

7

KEYCODE_0

8

KEYCODE_1

9

KEYCODE_2

10

KEYCODE_3

11

KEYCODE_4

12

KEYCODE_5

13

KEYCODE_6

14

KEYCODE_7

15

KEYCODE_8

16

KEYCODE_9

17

KEYCODE_STAR

18

KEYCODE_POUND

19

KEYCODE_DPAD_UP

20

KEYCODE_DPAD_DOWN

21

KEYCODE_DPAD_LEFT

22

KEYCODE_DPAD_RIGHT

23

KEYCODE_DPAD_CENTER

24

KEYCODE_VOLUME_UP

25

KEYCODE_VOLUME_DOWN

Ads by Google

26

KEYCODE_POWER

27

KEYCODE_CAMERA

28

KEYCODE_CLEAR

29

KEYCODE_A

30

KEYCODE_B

31

KEYCODE_C

32

KEYCODE_D

33

KEYCODE_E

34

KEYCODE_F

35

KEYCODE_G

36

KEYCODE_H

37

KEYCODE_I

38

KEYCODE_J

39

KEYCODE_K

40

KEYCODE_L

41

KEYCODE_M

42

KEYCODE_N

43

KEYCODE_O

44

KEYCODE_P

45

KEYCODE_Q

46

KEYCODE_R

47

KEYCODE_S

48

KEYCODE_T

49

KEYCODE_U

50

KEYCODE_V

51

KEYCODE_W

52

KEYCODE_X

53

KEYCODE_Y

54

KEYCODE_Z

55

KEYCODE_COMMA

56

KEYCODE_PERIOD

Conclusion

The above trick will help you if you have broken the hardware keys of your device. Don’t forget to tune in soon when we see how we can map virtual hardware keys on an Android phone even when the hardware keys are broken.

Before You Go...

Just wanted to let you know that we've got a special eBook on Google Chrome browser, called The Ultimate Chrome Productivity Guide where we show you how to master Google Chrome to make the most out of it. Make sure you buy it because it is worth much more than the price we're offering it for.

Get Guiding Tech articles delivered to your inbox.

Also get a free ebook when you subscribe.

We will never share your address. Unsubscribe at any time.



  • Lyman Li

    to wake up devices by adb, this post might be useful
    http://forum.xda-developers.com/showthread.php?t=865876

    • http://www.guidingtech.com Abhijeet Mukherjee

      Thanks Lyman!

  • Morzie

    i keep getting an error when using adb “device not found” how do i fix this, i have the phone pluged in

    • http://www.techpanache.com/ Ashish Mundhra

      Please install your device specific drivers and try again.

      • Morzie

        Ok, I’ll see if I can find some

  • Prasanna

    This is nice, but note that it doesn’t work (no device found till then), until “USB debugging” option is enabled in the phone’s settings. I just tried the whole thing and read numerous forums before I spotted this for sure. Seldom do ppl enable that option beforehand, until they break their screen and realize its value, like me.
    I guess, at this point only option for me is to connect a mouse using an OTG cable, get across the lock screen and enable the USB debugging and then use the adb commands or just a plain emulator tool.

  • Daniel Rodriguez

    how do like make click one something you have selected

  • nitish

    can this be used to enter recovery & download mode in samsung galaxy r?? currently unable to do so as volume keys dont work