Sending Android Keyevents via Leonardo


I hope I am posting this to the right forum, if not, can someone point me to a more appropriate one.

I am trying to send keyevent commands from my Leonardo to my Android tablet, part of a head unit. I can send the commands via ADB shell on my laptop, but I cannot figure out how to send them via the arduino.

I have been looking for weeks on how to do this, I have gotten some close hits ( but they are in the area of sending the standard ASCII keyboard codes, and I am looking to send screen diming/brighten and sleep/wake.

Has anyone had experience in this area?

Thanks, -Erik

Ping.... Does anyone happen to have any ideas?


Can you connect a USB keyboard to your Android tablet and send the commands you want? That is what the Leonardo is emulating so you connect it the same way.

It appears that you use a "USB OTG" (On The Go) adapter to connect a USB device to an Android tablet. Connect the MicroUSB side of the OTG adapter to the tablet and connect a plain USB cable from the OTG adapter to the Arduino Leonardo (or Arduino Micro).

Thank you so much for your reply, helps keep me going.

Yes I can connect a USB keyboard to the Android and send the keyevents to a serial terminal, although it seems that the Android will not execute the input commands I send, but that might have to do with the Android not being rooted. It works if I use adb shell on my laptop.

What I am not certain about is how I use the Leonardo to send an input keyevent to the android. For example, how would I send "input keyevent WAKEUP”? Do I send this as a text message to the android serial terminal and it will see it as a command?

I have been able to sleep the android via the Leonardo by sending the key codes specified in the Arduino systemAPI.h but I am not able to wake it back up, nor are there any commands for screen brightness.

Since I am able to do all the functions I want by executing the keycodes define in the android generic.kl file I am trying to focus on that direction, but, if I am missing something I would be thrilled to hear it.

I am connecting the OTG cable as you mention.

I hope this made sense.

Thanks again, -Erik

Erik63: What I am not certain about is how I use the Leonardo to send an input keyevent to the android. For example, how would I send "input keyevent WAKEUP”?

Unless that is a key on the 104-key PC keyboard, you probably can't without modifications to the Keyboard library. The Keyboard library only deals with keys on "Page 7" of the USB standard. That is basically just the 104-key PC keyboard. If you want other pages, like media controls and power controls, you will need to find out what USB "Page" they are on and make a version of the library that can send messages from that code page. The USB standard for Human Interface Devices is:


Yes, what I want is not part of the standard keyboard, so I stopped going in that direction. Instead I am trying to send the commands that the Android uses when a button is pressed on the device (those include all the functions I want).

I can access those via the input command in a serial terminal, in essense I am generating the ISR for that command. In order to do that I need to send the Android an "input keyevent "keycode" as described here

So what I am interested in is if there is a way to send the Android the command "input keyevent "keycode" via the leonardo. Can this be sent as a string?

This might be out of the range of this group since there is a lot of Android involvement but I thought I would ask.

Thanks, -Erik

There is the Arduino Mega ADK but I have no idea if it will help. It does have a USB host port so perhaps it can be an ADB shell client.

The USB Host Shield library has a few ADK examples.

Another option is to try a small Linux box such as a Raspberry Pi.

The Leonardo, like most Arduinos, can emulate a USB serial port and send text over Serial. I don't know if the Android OS always treats serial ports as a serial console but I would hope not. You can probably issue a command one a serial console telling the Android OS to connect a serial port to the console.

I think you are right in that this is more an Android question than an Arduino question. If you can do something with a serial terminal over USB then most any Arduino an do it. If you can do something with a USB keyboard or mouse then the Leonardo or Micro can do that as well.

Yes, I looked at the ADK but I could not figure out I could make that work, maybe I am missing something, if you have any idea I would be very happy to hear them.

As John mentioned I thought that since I seemed to be able to access the commands via a keyboard a Leonardo might work the best.

At this point I am asking the Android forum why I am unable to run the input keyevent on a serial terminal, seems like it might be a permissions issue.

Thanks again for all the help and ideas!