8

Can anyone explain the following command fully:

adb shell sendevent [device] [type] [code] [value]

I am trying to write a script for touch events using send event command.

Aduait Pokhriyal
  • 1,529
  • 14
  • 30
Jugal
  • 121
  • 1
  • 1
  • 10
  • FYI the values used are potentially device / build specific. – Chris Stratton Jun 23 '13 at 18:00
  • @Chris, only touchscreen device name is device specific. Touch event protocol and include/linux/input.h constants have not changed that much. But personally I do prefer `input tap` command over `sendevent`for touch emulation. – Alex P. Jun 23 '13 at 18:14
  • 1
    Sorry, but that is simply not true. The numeric codes are **quite** different on my various devices. – Chris Stratton Jun 23 '13 at 20:45
  • @Chris, would you mind posting the list of your devices? I would be very interested to find out if there are any non-legacy (i.e. ics4.0+ kernel 3.0+) devices which do not support the event codes from my answer below. I have got a couple of dozen devices myself and all of them use the same event codes. – Alex P. Jun 24 '13 at 05:14
  • You forget that much of the installed base runs older versions. I don't think they've even stopped selling GB in major markets yet. – Chris Stratton Jun 24 '13 at 12:21
  • @AlexP. Can "input tap" handle multiple swipe events to draw pattern and unlock. – Talespin_Kit Dec 28 '16 at 17:20
  • @Talespin_Kit I believe the command there would be "input swipe". (multiple of them) Not sure if it works, but it's a starting point. – Venryx Dec 28 '17 at 00:04

2 Answers2

10

First you need to find out the name of the touchscreen device on your phone or tablet. You can use this command in adb shell session:

getevent -pl 2>&1 | sed -n '/^add/{h}/ABS_MT_TOUCH/{x;s/[^/]*//p}'

Let's say the input device name is /dev/input/event0 and you want to emulate a quick tap at coordinates x=300, y=400:

sendevent /dev/input/event0 3 53 300
sendevent /dev/input/event0 3 54 400
sendevent /dev/input/event0 3 48 5
sendevent /dev/input/event0 3 58 50
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0

The long touch (let's say 1sec long) at the same coordinates would be:

sendevent /dev/input/event0 3 53 300
sendevent /dev/input/event0 3 54 400
sendevent /dev/input/event0 3 48 5
sendevent /dev/input/event0 3 58 50
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
sleep 1
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0

For the explanation what those commands mean and do exactly please read Emulating touchscreen interaction with sendevent in Android.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
  • Hello, your answer helped a lot, I wonder if you know something about this: https://stackoverflow.com/questions/73683955/how-to-translate-device-screen-position-to-sendevent-position – Cesar Sep 12 '22 at 03:23
0

Im using ZTE blade(1.gen) CyanagenMod 7. The screen resolution is 480x800 After some trial and error I figured out that in order to press coordinates 240x 725y I actually had to implement the script like this: 988 = 240x, 2768 = 725y

sendevent /dev/input/event0 3 53 988
sendevent /dev/input/event0 3 54 2768
sendevent /dev/input/event0 3 48 5
sendevent /dev/input/event0 3 58 50
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0

I was previously recording the events with get event and values it was giving were correct I just did not know how to interpret them = ).

Andy W
  • 1
  • 1