添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to simulate keypresses in my android device using getevent/sendevent mechanism. I see one of the batch file that is working. so I know the adb shell sendevent mechanism works.

So I did adb shell Getvent and did some keypresses, which gave me events which look like following:

dev/input/event0 0003 0039 0000006c
 /dev/input/event0 0001 014a 00000001
 /dev/input/event0 0001 0145 00000001
 /dev/input/event0 0003 0035 000003f8
 /dev/input/event0 0003 0036 00000153
 /dev/input/event0 0003 0030 00000001
 /dev/input/event0 0003 0031 00000000
 /dev/input/event0 0000 0000 00000000
 /dev/input/event0 0001 014a 00000000
 /dev/input/event0 0001 0145 00000000
 /dev/input/event0 0000 0000 00000000
 /dev/input/event0 0003 0039 0000006d
 /dev/input/event0 0001 014a 00000001
 /dev/input/event0 0001 0145 00000001
 /dev/input/event0 0003 0035 00000278
 /dev/input/event0 0003 0036 0000022c
 /dev/input/event0 0003 0030 00000004
 /dev/input/event0 0003 0031 00000004
 /dev/input/event0 0000 0000 00000000
 /dev/input/event0 0003 0039 ffffffff
 /dev/input/event0 0001 014a 00000000
 /dev/input/event0 0001 0145 00000000
 /dev/input/event0 0000 0000 00000000

So I copied the same events and made a batch file from that looks like following:

adb shell sendevent  /dev/input/event0 0003 0039 0000006c
adb shell sendevent /dev/input/event0 0001 014a 00000001
adb shell sendevent /dev/input/event0 0001 0145 00000001
adb shell sendevent /dev/input/event0 0003 0035 000003f8
adb shell sendevent /dev/input/event0 0003 0036 00000153
adb shell sendevent /dev/input/event0 0003 0030 00000001
adb shell sendevent /dev/input/event0 0003 0031 00000000
adb shell sendevent /dev/input/event0 0000 0000 00000000
adb shell sendevent /dev/input/event0 0003 0039 ffffffff
adb shell sendevent /dev/input/event0 0001 014a 00000000
adb shell sendevent /dev/input/event0 0001 0145 00000000
adb shell sendevent /dev/input/event0 0000 0000 00000000
adb shell sendevent /dev/input/event0 0003 0039 0000006d
adb shell sendevent /dev/input/event0 0001 014a 00000001
adb shell sendevent /dev/input/event0 0001 0145 00000001
adb shell sendevent /dev/input/event0 0003 0035 00000278
adb shell sendevent /dev/input/event0 0003 0036 0000022c
adb shell sendevent /dev/input/event0 0003 0030 00000004
adb shell sendevent /dev/input/event0 0003 0031 00000004
adb shell sendevent /dev/input/event0 0000 0000 00000000
adb shell sendevent /dev/input/event0 0003 0039 ffffffff
adb shell sendevent /dev/input/event0 0001 014a 00000000
adb shell sendevent /dev/input/event0 0001 0145 00000000
adb shell sendevent /dev/input/event0 0000 0000 00000000

But when I run this batch file, events are not simulated. Is there anything that I am missing here?

Open a separate adb session in a different window and run getevent in that while you try your batch file – Chris Stratton Apr 30, 2014 at 17:53 It does not show any events. Surprisingly, my batchfile is not showing eny errors. However, I just found out that getevent results in Hex while SendEvent only takes decimal. Does it mean I have to translate all the events manually? – Lost Apr 30, 2014 at 17:58 Possibly - and I have memories of a similar constraint, though you could check the source of the tools to be sure. Shouldn't be too hard to do with a little custom program.. even a spreadsheet can sometimes be a handy tool for a one-time data conversion project. – Chris Stratton Apr 30, 2014 at 17:59 Yeah since they are handful of events, it would not take much time to convert. Will update you once I am able to convert and run – Lost Apr 30, 2014 at 18:03

I would have never thought that was the case, thanks for the suggestion. It actually works now and since I was using the script before it was just a matter of printing the hex values in a string and python would do the conversion. Note that this system is much faster than the input tap version. I can send touch events back to back with an interleaving of around 0.3 secs (I still don't understand why it's not able to handle faster sequences)

That's how I did it in python on a Nexus 5 running Android 6.1:

import os
EV_ABS             = 0x0003
EV_SYN             = 0x0000
ABS_MT_POSITION_X  = 0x0035
ABS_MT_POSITION_Y  = 0x0036
ABS_MT_PRESSURE    = 0x003a
ABS_MT_TOUCH_MAJOR = 0x0030
SYN_REPORT         = 0x0000
ABS_MT_TRACKING_ID = 0x0039
touch_event_id = 1
def touch(x, y):
    global touch_event_id
    event_string =  "sendevent /dev/input/event1 %d %d %d\n" % (EV_ABS, ABS_MT_TRACKING_ID, touch_event_id)
    event_string += "sendevent /dev/input/event1 %d %d %d\n" % (EV_ABS, ABS_MT_POSITION_X,  x)
    event_string += "sendevent /dev/input/event1 %d %d %d\n" % (EV_ABS, ABS_MT_POSITION_Y,  y)
    event_string += "sendevent /dev/input/event1 %d %d %d\n" % (EV_ABS, ABS_MT_PRESSURE,    5)
    event_string += "sendevent /dev/input/event1 %d %d %d\n" % (EV_ABS, ABS_MT_TOUCH_MAJOR, 5)
    event_string += "sendevent /dev/input/event1 %d %d %d\n" % (EV_SYN, SYN_REPORT,         0)
    event_string += "sendevent /dev/input/event1 %d %d %d\n" % (EV_ABS, ABS_MT_TRACKING_ID, -1)
    event_string += "sendevent /dev/input/event1 %d %d %d\n" % (EV_SYN, SYN_REPORT,         0)
    touch_event_id+=1
    os.system('adb shell "%s" &' % event_string)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.