在android中可以使用getevent/sendevent命令获取和模拟系统的输入事件。
getevent可以获取android设备的事件信息,具体用法可参考帮助:
shell@m2note:/ $ getevent -h
Usage: getevent [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]
-t: show time stamps
-n: don't print newlines
-s: print switch states for given bits
-S: print all switch states
-v: verbosity mask (errs=1, dev=2, name=4, info=8, vers=16, pos. events=32, props=64)
-d: show HID descriptor, if available
-p: show possible events (errs, dev, name, pos. events)
-i: show all device info and possible events
-l: label event types and names in plain text
-q: quiet (clear verbosity mask)
-c: print given number of events then exit
-r: print rate events are received
实际使用的一个例子
shell@m2note:/ $ getevent -l
add device 1: /dev/input/event6
name: "gpio-keys"
add device 2: /dev/input/event5
name: "mtk-tpd-kpd"
add device 3: /dev/input/event4
name: "mtk-tpd"
add device 4: /dev/input/event3
name: "LTR559_ps"
add device 5: /dev/input/event2
name: "LTR559_als"
add device 6: /dev/input/event1
name: "ACCDET"
add device 7: /dev/input/event0
name: "mtk-kpd"
/dev/input/event0: EV_KEY KEY_POWER DOWN
/dev/input/event0: EV_SYN SYN_REPORT 00000000
/dev/input/event0: EV_KEY KEY_POWER UP
/dev/input/event0: EV_SYN SYN_REPORT 00000000
/dev/input/event0: EV_KEY KEY_POWER DOWN
/dev/input/event0: EV_SYN SYN_REPORT 00000000
/dev/input/event0: EV_KEY KEY_POWER UP
/dev/input/event0: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_TRACKING_ID 0000097b
/dev/input/event4: EV_ABS ABS_PRESSURE 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 0000028d
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000664
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_PRESSURE 00000002
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 0000028e
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 0000065f
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 00000290
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000655
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 00000295
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000643
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 0000029b
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 0000062e
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002a2
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000619
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002a8
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000601
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002ad
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000005e7
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002b0
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000005cb
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002b4
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000005ac
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002b8
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000584
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002be
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 0000055a
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002c4
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 0000052c
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002ca
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000004fb
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002d0
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000004cc
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002d7
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000497
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002da
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000460
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002de
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000428
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000003ed
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002db
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000003a8
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002d9
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000360
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 0000031a
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002df
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000002da
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002ee
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000294
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 000002fe
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 00000255
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 00000310
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 0000021a
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 0000031d
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000001f9
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_TRACKING_ID ffffffff
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_TRACKING_ID 0000097c
/dev/input/event4: EV_ABS ABS_PRESSURE 00000000
/dev/input/event4: EV_ABS ABS_MT_POSITION_X 00000277
/dev/input/event4: EV_ABS ABS_MT_POSITION_Y 000004cb
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_PRESSURE 00000002
/dev/input/event4: EV_SYN SYN_REPORT 00000000
/dev/input/event4: EV_ABS ABS_MT_TRACKING_ID ffffffff
/dev/input/event4: EV_SYN SYN_REPORT 00000000
通过如上我们可以看到得到的数据格式:
格式为 device: type code value,即 设备、输入设备类型、按键扫描码、附加码,具体定义在kernel/include/linux/input.h中(android源码);
type: 输入设备类型,在手机系统中经常使用的键盘(keyboard)和小键盘(kaypad)属于按键设
备EV_KEY,轨迹球属于相对设备EV_REL,触摸屏属于绝对设备EV_ABS
code: 按键扫描码,区别于ASCII码和SDK中KeyEvent的键码
value: 附加码,1/0 down/up
第1行:可理解为一次touch的开始
第2行:可理解为点击开始
第3行:触摸点x坐标
第4行:触摸点y坐标
第5行:可理解为触摸点大小
第6行:事件同步(点击结束)
第7行:一次touch结束
第8行:事件同步(事件结束)
一次touch此8行是必须的,如果是longTouch呢,在touch的基础上,重复若干次第2~6行,即看起
sendevent
shell@m2note:/ $ sendevent -h
use: sendevent device type code value
值得注意的是使用getevent获取的数值都是16进制的,而sendevent使用的是10进制的,需要进行转换;
参考链接
--
adb shell /system/bin/uiautomator events
02-23 18:00:51.287 EventType: TYPE_VIEW_CLICKED; EventTime: 4441494; PackageName: com.meizu.safe; MovementGranularity: 0; Action: 0 [ ClassName: com.meizu.common.widget.Switch; Text: []; ContentDescription: null; ItemCount: -1; CurrentItemIndex: -1; IsEnabled: true; IsPassword: false; IsChecked: true; IsFullScreen: false; Scrollable: false; BeforeText: null; FromIndex: -1; ToIndex: -1; ScrollX: -1; ScrollY: -1; MaxScrollX: -1; MaxScrollY: -1; AddedCount: -1; RemovedCount: -1; ParcelableData: null ]; recordCount: 1
02-23 18:00:51.487 EventType: TYPE_WINDOW_CONTENT_CHANGED; EventTime: 4441693; PackageName: com.meizu.safe; MovementGranularity: 0; Action: 0 [ ClassName: com.meizu.common.widget.Switch; Text: []; ContentDescription: null; ItemCount: -1; CurrentItemIndex: -1; IsEnabled: true; IsPassword: false; IsChecked: false; IsFullScreen: false; Scrollable: false; BeforeText: null; FromIndex: -1; ToIndex: -1; ScrollX: -1; ScrollY: -1; MaxScrollX: -1; MaxScrollY: -1; AddedCount: -1; RemovedCount: -1; ParcelableData: null ]; recordCount: 1