Google Groups

Re: I figured out how to emulate key presses


HypnoToad Feb 4, 2011 12:16 PM
Posted in group: Tasker
Using sendevent to emulate a long press on the menu button is much
more complex than emulating a press of the power button. The reason
why is that the power button is a hardware key so there isn't much too
it. One command to press it down, another command to release it. The
menu button, as well as the back, home, and search, buttons are
actually part of the touch screen. At least this is the case for my
phone (Nexus One) and most other phones. This means that you need to
emulate a touch on the screen. There is almost no documentation for
the getevent/sendevent commands the best I could find was here:

http://groups.google.com/group/android-beginners/browse_thread/thread/8a5d8fa9229114d2/ce6e604f52b5318f?pli=1

(If you find any more please let me know).

This is the method that I use to figure out the commands I need to
emulate a touch. Keep in mind that my PC is running Linux so you will
also need a Linux box if you want to use the same commands that I use
to sort and convert a lot of this data.

Please note that I upgraded to CM7 yesterday and the sendevent command
is broken on it so I'm currently unable to test my final results.

Here we go.

1. Fire up ADB on your PC and connect your phone.

2. Turn your phone's screen on and unlock it.

3. While your phone is unlocked with the screen on and nobody/nothing
touching the screen, run the following command

     adb shell getevent

4. If you see a bunch of messages flying by the screen then take note
of what event# they're going to. I get a bunch of stuff that looks
like this:

     /dev/input/event2: 0003 0000 00000000
     /dev/input/event2: 0003 0001 fffffd1f
     /dev/input/event2: 0000 0000 00000000
     /dev/input/event2: 0003 0000 00000003

     As you can see, it's all going to event2. I have no idea what
these events mean but they have nothing do do with what I'm looking
for so I filter them with grep to make my life easier. So the command
I use to get the data I need is:

     adb shell getevent | grep -v event2

     NOTE: This will only work on Linux or some other variant of UNIX
that has grep installed.

5. With the getevent command running, do what you're trying to emulate
and only what you're trying to emulate. For this example I'm going to
try to emulate a press of the menu key so I navgate home, start
getevent on my PC, press and release the menu key (we just have to
press and release it. We can emulate holding it down with a sleep
command later), then stop getevent with a Ctrl+c. You will see a bunch
more commands streaming by. I will will now try and make sense of some
of these commands.

     /dev/input/event3: 0003 0000 0000070f

     /dev/input/event3: 0003 0001 00001a22
     /dev/input/event3: 0000 0000 00000000

Anything that starts with a 0003 0000 is the X position of a touch,
the 0000070f is the position itself.
Anything that starts with a 0003 0001 is the Y position of a touch,
the 00001a22 is the position itself.
These commands are always followed by 0000 0000 00000000. I'm not sure
what this means but we'll emulate it just to be safe.

     /dev/input/event3: 0001 014a 00000001
     /dev/input/event3: 0000 0000 00000000
     /dev/input/event3: 0001 014a 00000000
     /dev/input/event3: 0000 0000 00000000

0001 014a 00000001 is the command for a touch of the screen.
I still have no Idea what the 0000 0000 00000000 is all about but it
shows up after every press so we'll emulate it.
0001 014a 000000010 is the command for an untouch (that's not a word
but I don't know what else to call it).

Note: You'll most likely get a lot of the x position and y position
commands because your finger will move around a bit but there all
going to be in the area that you need so you can just pick 2 for your
emulation.

6. Convert the results of the getevent command to a sendevent command.
For some reason the output of the get event command is given in hex
but the sendevent command requires decimal. So, for example, /dev/
input/event3: 0001 014a 00000001
 would translate to sendevent /dev/input/event3 1 330 1. You should
only have to convert 7 commands and the one that are all zero's will
be extra easy. Still, I'm lazy so I save the commands I'm going to
emulate to a file and run the following command on my PC:

gawk --non-decimal-data '{print $1, ("0x"$2)+0, ("0x"$3)+0,
("0x"$4)+0}' name-of-file.txt | sed 's/://g;s/^/sendevent /g'

This will give us this:

sendevent /dev/input/event3 3 0 1807
sendevent /dev/input/event3 3 1 6690
sendevent /dev/input/event3 0 0 0
sendevent /dev/input/event3 1 330 1
sendevent /dev/input/event3 0 0 0
sendevent /dev/input/event3 1 330 0
sendevent /dev/input/event3 0 0 0

To emulate holding down you would put a sleep command after the touch

sendevent /dev/input/event3 3 0 1807
sendevent /dev/input/event3 3 1 6690
sendevent /dev/input/event3 0 0 0
sendevent /dev/input/event3 1 330 1
sendevent /dev/input/event3 0 0 0
sleep 1
sendevent /dev/input/event3 1 330 0
sendevent /dev/input/event3 0 0 0

7. If you want to try these commands out before you go through the
trouble of making them into a Tasker script then you can send them to
your device by typing “adb shell” into your terminal then pasting the
commands. You can paste them all at once.


NOTE: The locale execute plugin seems pretty crappy for this sort of
thing so I'll try and post a how-to for using these commands with SL4A
pretty soon.

I want to thank Kent Yip for his great post which can be found in the
link toward the top of this post.
I would also like to thank my buddy jreppiks for that fancy gawk
command to convert hex to dec.


On Feb 4, 7:33 am, Richard Kemp <richardtk...@googlemail.com> wrote:
> I'm looking to emulate a long-press of the menu button, so I guess I
> need a command similar to this mentioned above for the power button:
>
> !sendevent /dev/input/event5 1 116 1
> !sendevent /dev/input/event5 1 116 0
>
> Is there somewhere we can look up a list of codes for use with this
> command (I presume they are different to the codes you gave in the
> first post HypnoToad?)
>
> Cheers
>
> On Feb 4, 9:30 am, Muad <rgisb...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I'm on the run :) HypnoToad.
> > First try I added two actions via locale execute with
> > !sendevent /dev/input/event5 1 116 1
> > !sendevent /dev/input/event5 1 116 0
> > After first run (few seconds later) I got superuser prompt for each
> > action, with no more results (my guess superuser prompr arrived late)
> > On the second run, satisfied the required superuser permission, all I
> > got is 2 consecutive flash text (Tasker alert flash text) with each of
> > the commands... nothing else.
> > And yes, I double-checked spaces in commands.
>
> > On 3 feb, 21:32, HypnoToad <pimpdaddysh...@gmail.com>; wrote:
>
> > > I commend you for trying to figure this out yourself. I think you're
> > > right though, your going to need my help. The sleep command is "sleep
> > > <# of seconds>" so if you wanted to sleep for 1 second the command
> > > would be "sleep 1" without the quotes. This command doesn't need to be
> > > run as root so there is no need to put an ! in front of it if you're
> > > using the locale execute plugin. Your code should look like the
> > > following
>
> > > !sendevent /dev/input/event5 1 116 1
> > > !sendevent /dev/input/event5 1 116 0
> > > !sendevent /dev/input/event5 1 116 1
> > > sleep 1
> > > !sendevent /dev/input/event5 1 116 0
> > > !input keyevent 20
> > > !input keyevent 20
> > > input keyevent 23
>
> > > The first line pushes down the power button and the second one
> > > releases it. This should turn on your screen. The third line pushes
> > > down your power button, the forth line waits 1 second, the fifth line
> > > releases the power button. That should bring up the "Phone options"
> > > menu. The next three lines move the cursor down 2 spaces and then
> > > clicks.
>
> > > If the locale execute plugin is too slow which, based on a prior
> > > comment you made, I suspect it will be. Then I'll help you run this in
> > > SL4A but it's going to be a bit more complex because SL4A doesn't
> > > support running commands as root so we'll have to use a workaround.
>
> > > Let me know if this works of if you need further assistance.
>
> > > On Feb 3, 1:09 pm, Muad <rgisb...@gmail.com>; wrote:
>
> > > > Thanks HypnoToad, given my android skills, would be *absolutely*
> > > > necesary your help to achieve it. 've been googling about that sleep
> > > > command without finding anything I can manage to do.
> > > > That commands must be "sent" using locale execute or via sl4a+python?
>
> > > > Thanks in advance.
>
> > > > On 2 feb, 21:26, HypnoToad <pimpdaddysh...@gmail.com>; wrote:
>
> > > > > This is a different method from what I documented above but here are
> > > > > the commands to press the power button
>
> > > > > sendevent /dev/input/event5 1 116 1
> > > > > sendevent /dev/input/event5 1 116 0
>
> > > > > The first command presses the button down, the second command releases
> > > > > it. You will need to put a sleep command in between those commands to
> > > > > emulate pushing the button down for a few seconds. From what I can
> > > > > tell from the photo you posted in those other threads you will then
> > > > > need to emulate 2 movements down and then a click. Let me know if you
> > > > > need anymore help.
>
> > > > > On Feb 2, 2:05 am, Muad <rgisb...@gmail.com>; wrote:
>
> > > > > > I sorry to say that I've trying for a long time with different
> > > > > > approaches without success.
>
> > > > > > visit in this forum (search is my friend)
> > > > > > ->http://groups.google.com/group/tasker/msg/efb39e3ba91bb71f
> > > > > > ->http://groups.google.com/group/tasker/msg/2347b8d5f38fe799
>
> > > > > > And without APNDroid there is no way to do it (IMHO-AFAIK).
> > > > > > If anyone manage to do it without APNDroid and without losing manual
> > > > > > control of the enable/disable mobile data, please let me know.
>
> > > > > > Thanks in advance.
>
> > > > > > On 1 feb, 22:57, HypnoToad <pimpdaddysh...@gmail.com>; wrote:
>
> > > > > > > Sorry SL4A is the new name but, as I said before, everything that
> > > > > > > you're trying to do is built into Tasker already so there is no need
> > > > > > > for you to use this method.
>
> > > > > > > On Feb 1, 1:59 pm, Muad <rgisb...@gmail.com>; wrote:
>
> > > > > > > > Thanks for the tip, but I can't find it, is SL4A?
> > > > > > > > And its a little more complicated cause I'm trying to get the power
> > > > > > > > menu (holding power button) and then select 2nd option that is "enable/
> > > > > > > > disable" mobile data...
> > > > > > > > And using that SL4A... I don't need that execute plugin?
>
> > > > > > > > Thanks in advance.
>
> > > > > > > > On 1 feb, 20:55, HypnoToad <pimpdaddysh...@gmail.com>; wrote:
>
> > > > > > > > > keyevent 26 doesn't seem to do anything for me either. Tasker has the
> > > > > > > > > ability to turn your phone's screen on though so you shouldn't have to
> > > > > > > > > emulate the power button anyway. As for the speed, you might try using
> > > > > > > > > an app called ASE. It's available from Google Code and it integrates
> > > > > > > > > with Tasker. Just put all those commands into a shell script and run
> > > > > > > > > them that way. I haven't tried this myself so I don't know if it runs
> > > > > > > > > faster but I'd imagine that it would.
>
> > > > > > > > Yep, but only if you use APN Droid, which I'm trying to avoid (for no
> > > > > > > > particular reason, just don't want more apps to do it).
> > > > > > > > And I've tried without APN Droid and is a no-way. (search this forum)
>
> > > > > > > > On 1 feb, 21:01, HypnoToad <pimpdaddysh...@gmail.com>; wrote:
>
> > > > > > > > > I just looked and Tasker has the ability to enable/disable mobile data
> > > > > > > > > built in so you shouldn't have to use this method at all.