Android 自带了 getevent 和 sendevent,分别用于获取设备报送的事件和模拟产生事件,前者用于辅助设备驱动的验证,后者用于模拟事件进行上层应用的验证。
关于event设备的获取,在 /proc/bus/input/devices 下面:
$ adb shell cat /proc/bus/input/devices
0 getevent和sendevent用法:
$ adb shell 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
$ adb shell sendevent -h
use: sendevent device type code value
1、模拟发送一个Power键:
$ export KEYCODE_POWER=26
$ input keyevent ${KEYCODE_HOME}
或者:
先获取gpio-keys event的设备接口:
$ adb shell cat /proc/bus/input/devices | grep -A 5 gpio-keys | grep event
H: Handlers=event5
# Press power button(Don't release)
$ adb shell sendevent /dev/input/event5 1 $((0x74)) 1
$ adb shell sendevent /dev/input/event5 0 0 0
# Release power button
$ adb shell sendevent /dev/input/event5 1 $((0x74)) 0
$ adb shell sendevent /dev/input/event5 0 0 0
2、模拟获取Power键的事件
先执行
$ adb shell getevent /dev/input/event5
然后按下power键并观察输出:
按下时:
0001 0074 00000001
0000 0000 00000000
弹起来时:
0001 0074 00000000
0000 0000 00000000
可以看到更好跟我们上面模拟的一样,注,上面那些值是十六进制的,如果想要 更加直观地分析获取的上报事件可以参考 在 Android 通过 get_event 获得 input 设备 上报event
http://blog.csdn.net/hellowxwworld/article/details/10733567
getevent/sendevent源码
这两个命令的源码在system/core/toolbox/下,sendevent.c getevent.c
getevent
使用getevent获得/dev/input/eventX设备汇报的事件,这个命令还会输出所有event设备的基本信息,如下:
add device 1: /dev/input/event1
name: "mxc_ts"
add device 2: /dev/input/event0
name: "mxckpd"
表明系统有两个event设备,分别对应着input设备touchscreen,keyboard
Android可以使用sendevent来模拟触屏,键盘以及其他类型的event事件,
sendevent /dev/input/eventX type code value
/dev/input/eventX 对应一个event设备,可以通过getevent获得可用的event设备
type, code, value的定义可参看kernel/include/linux/input.h
type如下定义
- /*
- * Event types
- */
- #define EV_SYN 0x00
- #define EV_KEY 0x01
- #define EV_REL 0x02
- #define EV_ABS 0x03
- #define EV_MSC 0x04
- #define EV_SW 0x05
- #define EV_LED 0x11
- #define EV_SND 0x12
- #define EV_REP 0x14
- #define EV_FF 0x15
- #define EV_PWR 0x16
- #define EV_FF_STATUS 0x17
- #define EV_MAX 0x1f
- #define EV_CNT (EV_MAX+1)
一般来说,常用的是EV_KEY, EV_REL, EV_ABS, EV_SYN
分别对应keyboard, 相对坐标, 绝对坐标, 同步事件
EV_SYN则表示一组完整事件已经完成,需要处理,EV_SYN的code定义事件分发的类型
EV_SYN对应的code如下
- /*
- * Synchronization events.
- */
- #define SYN_REPORT 0
- #define SYN_CONFIG 1
- #define SYN_MT_REPORT 2
EV_KEY的code比较多,这里就不列出来了,可参照input.h
EV_REL对应的code
- /*
- * Relative axes
- */
- #define REL_X 0x00
- #define REL_Y 0x01
- #define REL_Z 0x02
- #define REL_RX 0x03
- #define REL_RY 0x04
- #define REL_RZ 0x05
- #define REL_HWHEEL 0x06
- #define REL_DIAL 0x07
- #define REL_WHEEL 0x08
- #define REL_MISC 0x09
- #define REL_MAX 0x0f
- #define REL_CNT (REL_MAX+1)
EV_ABS对应的code
- /*
- * Absolute axes
- */
-
- #define ABS_X 0x00
- #define ABS_Y 0x01
- #define ABS_Z 0x02
- #define ABS_RX 0x03
- #define ABS_RY 0x04
- #define ABS_RZ 0x05
- #define ABS_THROTTLE 0x06
- #define ABS_RUDDER 0x07
- #define ABS_WHEEL 0x08
- #define ABS_GAS 0x09
- #define ABS_BRAKE 0x0a
- #define ABS_HAT0X 0x10
- #define ABS_HAT0Y 0x11
- #define ABS_HAT1X 0x12
- #define ABS_HAT1Y 0x13
- #define ABS_HAT2X 0x14
- #define ABS_HAT2Y 0x15
- #define ABS_HAT3X 0x16
- #define ABS_HAT3Y 0x17
- #define ABS_PRESSURE 0x18
- #define ABS_DISTANCE 0x19
- #define ABS_TILT_X 0x1a
- #define ABS_TILT_Y 0x1b
- #define ABS_TOOL_WIDTH 0x1c
- #define ABS_VOLUME 0x20
- #define ABS_MISC 0x28
-
- #define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
- #define ABS_MT_TOUCH_MINOR 0x31 /* Minor axis (omit if circular) */
- #define ABS_MT_WIDTH_MAJOR 0x32 /* Major axis of approaching ellipse */
- #define ABS_MT_WIDTH_MINOR 0x33 /* Minor axis (omit if circular) */
- #define ABS_MT_ORIENTATION 0x34 /* Ellipse orientation */
- #define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
- #define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
- #define ABS_MT_TOOL_TYPE 0x37 /* Type of touching device */
- #define ABS_MT_BLOB_ID 0x38 /* Group a set of packets as a blob */
- #define ABS_MT_TRACKING_ID 0x39 /* Unique ID of initiated contact */
- #define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */
-
- #define ABS_MAX 0x3f
- #define ABS_CNT (ABS_MAX+1)
input keyevent
如果想模拟按键,send event用起来比较繁琐,可以用input keyevent代替
下面是input keyevent几个比较常用的用法:
input keyevent 3 // Home
input keyevent 4 // Back
input keyevent 19 //Up
input keyevent 20 //Down
input keyevent 21 //Left
input keyevent 22 //Right
input keyevent 23 //Select/Ok
input keyevent 24 //Volume+
input keyevent 25 // Volume-
input keyevent 82 // Menu 菜单
用senevent 模拟触屏事件
sendevent /dev/input/event1 0003 0000 0000015e // ABS x 坐标
sendevent /dev/input/event1: 0003 0001 000000df // ABS y 坐标
sendevent /dev/input/event1: 0001 014a 00000001 // BTN touch事件 值为1
sendevent /dev/input/event1: 0003 0018 00000000 // ABS pressure事件
sendevent /dev/input/event1: 0001 014a 00000000 // BTN touch事件 值为0
sendevent /dev/input/event1: 0000 0000 00000000 // sync事件
http://blog.csdn.net/kickxxx/article/details/7482392
1. getevent
Usage: getevent [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-p] [-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) -p: show possible events (errs, dev, name, pos. events) -q: quiet (clear verbosity mask) -c: print given number of events then exit -r: print rate events are received
例如:我们想看看触摸屏的上报速率
# getevent -r /dev/input/event0 getevent -r /dev/input/event0 0003 003e 0305a449 0003 003e 00000000 0000 0000 00000000 0003 003e 0365a04e 0003 003e 00000000 0000 0000 00000000 rate 38 0003 003e 0375a051 0003 003e 00000000 0000 0000 00000000 rate 84 0003 003e 0345a057 0003 003e 00000000 0000 0000 00000000 rate 80 0003 003e 1295a05e 0003 003e 00000000 0000 0000 00000000 rate 79 0003 003e 0345a068 0003 003e 00000000 0000 0000 00000000 rate 79 0003 003e 03b59c70
2. input
usage: input [text|keyevent] input text <string> input keyevent <event_code>
例如,输入一串字母 input text "hello" 输入一个MENU按键 input keyevent 82
3.sendevent
usage: sendevent [device] [type] [code] [value]
这条命令可以输入所有的input event事件
如在屏幕的x坐标为40,y坐标为210的点上touch一下,命令如下
adb shell sendevent /dev/input/event0 3 0 40 //ABS X 40 adb shell sendevent /dev/input/event0 3 1 210 // ABS Y 210 adb shell sendevent /dev/input/event0 1 330 1 //KEY BTN_TOUCH 1 adb shell sendevent /dev/input/event0 0 0 0 //SYNC EVENT adb shell sendevent /dev/input/event0 1 330 0 //KEY BTN_TOUCH 0 adb shell sendevent /dev/input/event0 0 0 0 //SYNC EVENT
http://blog.sina.com.cn/s/blog_503e61a80100n8x5.html