[Android]getevent,sendevent,input命令的使用

adb shell 进入avd或者真机后台: 

getevent 
getevent -r -q 监控设备的sendevent事件
root@android:/ # 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


显示格式说明 

root@android:/ # getevent
/dev/input/event5: 0005 0002 00000001
device的名字:事件类型    键码类别  具体的数值

/dev/input/event5: 0000 0000 00000000

表示一次输入结束


/* 
* 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)


二. 具体应用, 比如需要查看audio jack的事件,也就是耳机的插入 

1. 首先确认耳机插入的事件是啥 
r
oot@android:/ # getevent -i                                                 
add device 1: /dev/input/event2
  bus:      0000
  vendor    0000
  product   0000
  version   0000
  name:     "pmic8xxx_pwrkey"
  location: "pmic8xxx_pwrkey/input0"
  id:       ""
  version:  1.0.1
  events:
    KEY (0001): 0074 
  input props:
    <none>
.....

add device 5: /dev/input/event10
  bus:      0000
  vendor    0000
  product   0000
  version   0000
  name:     "msm8960-snd-card Headset Jack"
  location: "ALSA"
  id:       ""
  version:  1.0.1
  events:
    SW  (0005): 0002* 0004* 0006  000e  000f  0010 
  input props:
    <none>
add device 6: /dev/input/event9
  bus:      0000
  vendor    0000
  product   0000
  version   0000
  name:     "msm8960-snd-card Button Jack"
  location: "ALSA"
  id:       ""
  version:  1.0.1
  events:
    KEY (0001): 0100  0101  0102  0103  0104  0105  0106  0107 
  input props:
    <none>

2. 输出 audio jack事件 

用的event type是#define EV_SW            0x05
/*
 * Switch events
 */

#define SW_LID            0x00  /* set = lid shut */
#define SW_TABLET_MODE        0x01  /* set = tablet mode */
#define SW_HEADPHONE_INSERT    0x02  /* set = inserted */
#define SW_RFKILL_ALL        0x03  /* rfkill master switch, type "any"
                     set = radio enabled */
#define SW_RADIO        SW_RFKILL_ALL    /* deprecated */
#define SW_MICROPHONE_INSERT    0x04  /* set = inserted */
#define SW_DOCK            0x05  /* set = plugged into dock */
#define SW_LINEOUT_INSERT    0x06  /* set = inserted */
#define SW_JACK_PHYSICAL_INSERT 0x07  /* set = mechanical switch set */
#define SW_VIDEOOUT_INSERT    0x08  /* set = inserted */
#define SW_CAMERA_LENS_COVER    0x09  /* set = lens covered */
#define SW_KEYPAD_SLIDE        0x0a  /* set = keypad slide out */
#define SW_FRONT_PROXIMITY    0x0b  /* set = front proximity sensor active */
#define SW_ROTATE_LOCK        0x0c  /* set = rotate locked/disabled */
#define SW_LINEIN_INSERT    0x0d  /* set = inserted */
#define SW_HPHL_OVERCURRENT    0x0e  /* set = over current on left hph */
#define SW_HPHR_OVERCURRENT    0x0f  /* set = over current on right hph */
#define SW_UNSUPPORT_INSERT    0x10  /* set = unsupported device inserted */
#define SW_MAX            0x20
#define SW_CNT            (SW_MAX+1)
root@android:/ # getevent  /dev/input/event10 

0005 0002 00000001 (0002  表示earphone)
0005 0004 00000001 (0004  表示microphone)
0000 0000 00000000

 	插入	拔出
Headset	0005 0002 00000001
0005 0004 00000001
0000 0000 00000000	0005 0002 00000000
0005 0004 00000000
0000 0000 00000000
Headphone	0005 0002 00000001
0000 0000 00000000	0005 0002 00000000
0000 0000 00000000
invalid	0005 0010 00000001
0000 0000 00000000	0005 0010 00000000
0000 0000 00000000


三、具体应用, 比如需要查看touch的事件 
1、确认touch事件路径 

root@android:/ # getevent -i     
add device 13: /dev/input/event7 
  bus:      0000 
  vendor    0000 
  product   0000 
  version   0000 
  name:     "cyttsp4_mt" 
  location: "cyttsp4_mt.main_ttsp_core" 
  id:       "" 
  version:  1.0.1 
  events: 
    ABS (0003): 0000  : value 0, min 0, max 0, fuzz 0, flat 0, resolution 0 
2、touch 事件 

用的event type是#define EV_ABS            0x03 

只分析多touch事件 
#define ABS_MT_SLOT        0x2f    /* MT slot being modified */ 
#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_MT_DISTANCE        0x3b    /* Contact hover distance */ 
root@android:/ # getevent  /dev/input/event7 
单点触摸 
0003 0039 000000d5  //触点区分的唯一ID 
0003 0035 00000165 //触点的x坐标 
0003 0036 000002fa  //触点的y坐标 
0003 003a 00000025 //触点的压力,实际上是接触区域大小 
0000 0000 00000000 //结束 

多点触摸 

0003 0039 000000de 
0003 0035 00000140 
0003 0036 0000047f 
0003 003a 00000021 
0000 0000 00000000 
0003 0035 0000013f 
0003 003a 00000027 
0003 002f 00000001 //切换上报其中一点 
0003 0039 000000df 
0003 0035 000001fd 
0003 0036 000001e3 
0003 003a 00000022 
0000 0000 00000000 
0003 002f 00000000 //切换上报其中一点 
0003 0036 0000047e 
0003 003a 0000002b 
0000 0000 00000000 
0003 0036 0000047d 
0003 003a 0000002f 
0000 0000 00000000 
0003 0036 0000047b 
0003 003a 00000034 
0003 002f 00000001 //切换上报其中一点 
0003 0035 000001fc 
0003 0036 000001e6 
0003 003a 00000023 
0000 0000 00000000 

三、getevent 源代码分析 
@system/core/toolbox/getevent.c 
int getevent_main(int argc, char *argv[]) 

{ 

      const char *device_path = "/dev/input";    //读取的路径 

      .... 

     res = scan_dir(device_path, print_flags); //扫描路径下的所有文件路径 

    .... 

    while(1) { 

        ... 
        res = read(ufds[i].fd, &event, sizeof(event)); //读取事件 
       ... 

       print_event(event.type, event.code, event.value, print_flags); //打印出事件信息 
   } 
} 

四、sendevent源代码分析 

@system/core/toolbox/sendevent.c 

sendevent 
1. 模拟插入耳机, 这时候可以启动收音机了(不再有耳机没有的提示框) 
sendevent /dev/input/event10 0005 0002 00000001 

sendevent /dev/input/event10 0000 0000 00000000 


input 根据avd和真机的不同,参数可能不同 

$ input 
usage: input [text|keyevent] 
       input text <string> 
       input keyevent <event_code> 

4.可能遇到的问题 
实际实现的时候,还可能遇到问题 
一是root,getevent和sendevent需要/dev/input/event*的权限。一般应用是没有这个权限的,需要在程序里面获取su后,执行chmod 666 /dev/input/event*。 
二是设备名称。因为你不知道触摸屏或者按键到底对应的event*是多少。需要有一个初始化的过程,大致思路是往event0-event9分别写入按键和触摸信号,同时监听activity里的onkeydown和view的onclick,这样来侦测设备。 
三是厂商的实现不一样,这个没办法,只能一个一个适配了,一般来说都还是标准的,有些厂商会有单独的实现。
参考 
http://lxr.free-electrons.com/source/include/uapi/linux/input.h#L803 
http://source.android.com/tech/input/touch-devices.html 
http://cjix.info/blog/misc/internal-input-event-handling-in-the-linux-kernel-and-the-android-userspace/ 


推荐参考网址:http://www.jtben.com/document/919575
上一篇:Silverlight Events:与John Papa面对面学习Silverlight 4


下一篇:SharpDevelop学习笔记--什么是codon,与codon的创建过程