[Android Software Development] GermainZ:
How to use sendevent in LMT (works for scripts outside of LMT, too...e.g. Tasker)

Seeing the demand.. I'm writing this quick tutorial. Everything will be done from the device itself, because using adb for this is kinda overkill. Heh.

EDIT: You might want to check this script I wrote. It does everything automagically.

Why it's a good idea:

It eliminates the lag, allows you to emulate anything you want, etc.

What you'll need:

A brain;

A terminal emulator;

(Optional) SManager to easily test our final script (it has an integrated terminal emulator, too);

(Optional) A web browser or a calculator that can convert from hex to decimal (you could also do it mentally).

Obviously, LMT.

What we'll do, in order:

Use getevent to get the events;

Convert the output from hex to decimal;

Write the script that will emulate key presses / long presses / touches using sendevent;

Test it;

Configure LMT to use it.

Refrain from asking stupid questions if it doesn't work - use your brain and Google first;

1- Use getevent to get the events.

Open a terminal emulator and enter this command, as root:

getevent

You should see some output straight away. Try pressing a few buttons (e.g. the Menu button). How many lines do you see every time you press a button? Probably 4. To make our life easier, we'll set getevent to only return 4 lines and exit, instead of flooding us.

getevent -c 4

If you have no idea how many lines are being outputted, or if you want to emulate a series of key presses/taps, use this command instead:

getevent -t

This will show you the timestamps as well so you can figure out which lines are for which key.

The output should look like this:

/dev/input/event6: 0001 008b 00000001
/dev/input/event6: 0000 0000 00000000
/dev/input/event6: 0001 008b 00000000
/dev/input/event6: 0000 0000 00000000

There are plenty of bits and pieces around the internet explaining what each of these lines is. You can look it up if you're interested.

2- Convert the output from hex to decimal:

The output getevent returned uses hexadecimal numbers. sendevent accepts decimal numbers.

getevent's example output:

/dev/input/event6: 0001 008b 00000001
/dev/input/event6: 0000 0000 00000000
/dev/input/event6: 0001 008b 00000000
/dev/input/event6: 0000 0000 00000000

sendevent's example syntax:

sendevent /dev/input/event6 1 139 1
sendevent /dev/input/event6 0 0 0
sendevent /dev/input/event6 1 139 0
sendevent /dev/input/event6 0 0 0

So, how do we convert hex to decimal numbers? The simplest way is to use some online website that does it for us.

Convert each of the numbers (e.g. 0000, 008b, 0001) to decimal numbers and build the sendevent commands. Check the example.

3- Write the script that will emulate key presses / long presses / touches using sendevent:

We already did most of this in step 2.

Here a few things you should keep in mind:

If you want to emulate screen taps, you probably want to add some delays between them. Use the sleep command for that (e.g. "sleep 1" waits for 1 second).

To emulate long key presses, you also need to wait. Add a "sleep 1" between the first two lines and the last two lines, for example:

sendevent /dev/input/event6 1 139 1
sendevent /dev/input/event6 0 0 0
sleep 1
sendevent /dev/input/event6 1 139 0
sendevent /dev/input/event6 0 0 0

It's very important to get it all right. The part in bold is easy to forget about - make sure to double check it with getevent:

sendevent /dev/input/event[b]6[/b] 1 139 1

4- Test it.

Test it using SManager:

Create a new script;

Paste your commands;

Run it as root.

You might want to add a delay on the first line.

5- Configure LMT to use it.

This is pretty much straight forward. Pick the gesture/pie element you want, then paste the script in "Script". Simple, huh?

6- Refrain from asking stupid questions if it doesn't work - use your brain and Google first.

Really.

Examples (working for the Galaxy Ace i):

Menu button:

sendevent /dev/input/event6 1 139 1
sendevent /dev/input/event6 0 0 0
sendevent /dev/input/event6 1 139 0
sendevent /dev/input/event6 0 0 0

Back button:

sendevent /dev/input/event6 1 158 1
sendevent /dev/input/event6 0 0 0
sendevent /dev/input/event6 1 158 0
sendevent /dev/input/event6 0 0 0

Home long press:

sendevent /dev/input/event5 1 102 1
sendevent /dev/input/event5 0 0 0
sleep 1
sendevent /dev/input/event5 1 102 0
sendevent /dev/input/event5 0 0 0

Power button:

sendevent /dev/input/event7 1 116 1
sendevent /dev/input/event7 0 0 0
sendevent /dev/input/event7 1 116 0
sendevent /dev/input/event7 0 0 0