Categories
Technical

Using GPIO’s as Key Events in Android

Sometimes the smallest things you want to do can take a few hours. I have a gpio input that I am getting and I want to convert that into a keystroke in android. You would think that something like that would be the simplest thing.

But with DTS and the constant simplification of the linux kernel system you need to really search for information. Well the first thing that I found in the DTS (after I figured out that in the hundreds of files I was looking for, I was seeing the correct file) was that the keys are mapped with gpio-keys.

Some inputs…

https://www.kernel.org/doc/Documentation/devicetree/bindings/input/gpio-keys.txt

https://www.kernel.org/doc/Documentation/input/input.txt

I took a good look at the above files…and the gpio-key bindings in the dts file.

I then wanted to find the key that I wanted to send. I was looking in the file and finding entries for Volume up, which mapped to 115. But for the life of me, I was not able to find the correct values for the specific key that I wanted to input.

The documentation (input.txt above) says…

'code' is event code, for example REL_X or KEY_BACKSPACE, again a complete

list is in include/linux/input.h.

It turns out that it is not there. It is in…

<path to kernel>/include/uapi/linux/input.h

Interesting that googling did not find an obvious link. Well this has a complete list.

Even after this I realized that I was not really done. I found this out when debugging something else and ran the command…

cat /sys/kernel/debug/pinctrl/1000000.pinctrl/pinmux-pins

Unless you actually see something like this (below), you are not yet done.

pin 109 (gp-109): gpio_keys.68 gp:1011 function tlmm_gpio_key-func group tlmm_gpio_key

to do that I had to go to another file… msm8916-pinctrl.dtsi and added an entry in tlmm_gpio_key. See some of the other examples there. Dont forget to update qcom,num-grp-pins because you will still wonder why the last entry that you graciously added in the end did not show up.

The reason for all this issue is that the android event does not map to the keystroke that generates it (no issue but was key to what I wanted to do).

And after all this I took another two hours to figure out that this is not enough. Especially in android. What we have done so far is to make sure that the GPIO key is seen as a linux key event. This is still not a key event.

I first made sure my GPIO was sending the Volume Up key and it always worked. I was wondering why the actual key that I wanted to send did not work.

It turns out that there is something called a key layout file, a file that android uses to map key events to android events.

https://source.android.com/devices/input/key-layout-files.html

This turned out to be the missing link. Turns out that in /system/usr/keylayout/ there was a file called gpio-keys.kl that had some keys in it. Example below. I wanted to stuff the physical key “1” and this helped me do it. I am including the CAMERA key as an indicator.

key 766   CAMERA

key 513   1

Anyway…now you know what I know.

If you got here, do check out an Mac app I wrote that lets you use your Mac keyboard as a bluetooth keyboard for the iPhone, iPad, android devices…

2 replies on “Using GPIO’s as Key Events in Android”

Hmm…That was a while ago that I did this, and promptly forgot after I wrote it down. A reader of this blog figured it out and sent it to me. Pasting his comments.

“I managed to figure out how the numbers were assigned using getevent https://source.android.com/devices/input/getevent .

The kl file has the decimal equivalent of what getevents would print out when you press a hardware button … So I was able to reverse engineer the number that I needed for my gpio 🙂 . Still dont get the full picture , but I have a means to what I need to get done. “

Leave a comment