Saturday, March 16, 2019

Android开发笔记: 3.10.2 Capture touch event and simulate


3.10.2 Capture touch event and simulate

Refer to Android input. Android provide three utility for this: input, getevent and sendevent. Interestingly, cannot find official document about sendevent and input. Run ‘input’ and ‘sendevent’ alone will get usage info, such as usage: sendevent device type code value => Note: getevent output hex but sendevent only take decimal parameters, and no error report if pass hex value, just won’t work. Some info can be found from superuser, so and this blog: capture, convert with awk then playback: adb shell getevent | grep --line-buffered ^/ | tee /tmp/touch-events.log; Perform some touching, then ctrl-c and run: awk '{printf "%s %d %d %d\n", substr($1, 1, length($1) -1), strtonum("0x"$2), strtonum("0x"$3), strtonum("0x"$4)}' /tmp/touch-events.log | xargs -l adb shell sendevent. Note there might be timing with sendevent to simulate touch.
For touch events only 2 event types are used: EV_ABS (3) and EV_SYN (0).
Touching the display (in case of Type A protocol) will result in an input report (sequence of input events) containing the following event codes:
  • ABS_MT_TRACKING_ID (57 = 0x39) - ID of the touch (important for multi-touch reports)
  • ABS_MT_POSITION_X (53 = 0x35) - x coordinate of the touch
  • ABS_MT_POSITION_Y (54 = 0x36) - y coordinate of the touch
  • ABS_MT_TOUCH_MAJOR (48 = 0x30) - basically width of your finger tip in pixels
  • ABS_MT_PRESSURE (58 = 0x3A) - pressure of the touch
  • SYN_MT_REPORT (2) - end of separate touch data
  • SYN_REPORT (0) - end of report
Try getevent with: adb shell -- getevent -p, adb shell -- getevent -lp /dev/input/event2, adb shell -- getevent -lt /dev/input/event2
input’ implementation might be different per device, with a terminal app running, adb shell input text "hello” will get “hello” show after the shell prompt.

0 Comments:

Post a Comment