请问怎么知道/dev/input/eventX对应的是什么设备?

时间:2022-06-29 07:17:33
我需要读取/dev/input/eventX设备的流,但是我怎么知道这些event分别对应什么设备呢?比如哪个是鼠标,哪个是键盘,哪个是触摸屏?有人有idea吗?

18 个解决方案

#1


linux kernel中的文档有说明:https://www.kernel.org/doc/Documentation/input/input.txt

  You can use blocking and nonblocking reads, also select() on the
/dev/input/eventX devices, and you'll always get a whole number of input
events on a read. Their layout is:

struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value;
};

  'time' is the timestamp, it returns the time at which the event happened.
Type is for example EV_REL for relative moment, EV_KEY for a keypress or
release. More types are defined in include/linux/input.h.

  'code' is event code, for example REL_X or KEY_BACKSPACE, again a complete
list is in include/linux/input.h.

  'value' is the value the event carries. Either a relative change for
EV_REL, absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for
release, 1 for keypress and 2 for autorepeat.

#2


可以考虑参考下
cat /proc/bus/input/devices 
的输出

#3


引用 2 楼 linlan999 的回复:
可以考虑参考下
cat /proc/bus/input/devices 
的输出


如果cat得出的是如下内容,如何判断呢?

130|root@android:/ # cat /proc/bus/input/handlers                              
N: Number=0 Name=kbd
N: Number=1 Name=sysrq (filter)
N: Number=2 Name=evdev Minor=64
N: Number=3 Name=keychord
root@android:/ # cat /proc/bus/input/devices                                   
I: Bus=0019 Vendor=0001 Product=0001 Version=0100
N: Name="rk29-keypad"
P: Phys=gpio-keys/input0
S: Sysfs=/devices/platform/rk29-keypad/input/input0
U: Uniq=
H: Handlers=kbd event0 keychord 
B: PROP=0
B: EV=3
B: KEY=8000 1c0000 0 0 0

I: Bus=0018 Vendor=0000 Product=0000 Version=0000
N: Name="gslX680"
P: Phys=
S: Sysfs=/devices/platform/rk30_i2c.2/i2c-2/2-0040/input/input1
U: Uniq=
H: Handlers=event1 
B: PROP=2
B: EV=10000b
B: KEY=0
B: ABS=2658000 0

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="gsensor"
P: Phys=
S: Sysfs=/devices/platform/rk30_i2c.0/i2c-0/0-004c/input/input2
U: Uniq=
H: Handlers=event2 
B: PROP=0
B: EV=9
B: ABS=7

#4


#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=3 , 3=2+1, 也就是 (2<<1+1)  说明是设备有按键 EV_KEY/EV_SYNC;
EV=b, 11= 2<<3+2<<1+1 , 也就是设备支持 EV_ABS/EV_KEY/EV_SYNC 设备有按键,触摸屏/板;
MISC 设备不好判断;

#5


http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/
http://*.com/questions/2547616/how-can-i-translate-linux-keycodes-from-dev-input-event-to-ascii-in-perl
http://blog.csdn.net/lanmanck/article/details/8423669

may those help.

#6


引用 5 楼 linlan999 的回复:
http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/
http://*.com/questions/2547616/how-can-i-translate-linux-keycodes-from-dev-input-event-to-ascii-in-perl
http://blog.csdn.net/lanmanck/article/details/8423669

may those help.


兄弟实在太厉害了。解答了我困扰已久的问题。我先研究研究。

#7


引用 4 楼 linlan999 的回复:
#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=3 , 3=2+1, 也就是 (2<<1+1)  说明是设备有按键 EV_KEY/EV_SYNC;
EV=b, 11= 2<<3+2<<1+1 , 也就是设备支持 EV_ABS/EV_KEY/EV_SYNC 设备有按键,触摸屏/板;
MISC 设备不好判断;


兄弟,这个办法好像也有问题,比如多个eventX的EV值都是3,就难以判断到底谁是keypad, 比如我的设备:

shell@android:/ $ cat /proc/bus/input/devices
I: Bus=0019 Vendor=2454 Product=6575 Version=0010
N: Name="mtk-kpd"
P: Phys=
S: Sysfs=/devices/platform/mtk-kpd/input/input0
U: Uniq=
H: Handlers=event0 
B: PROP=0
B: EV=3
B: KEY=1c0000 0 0 0

I: Bus=0019 Vendor=0000 Product=0000 Version=0000
N: Name="ACCDET"
P: Phys=
S: Sysfs=/devices/virtual/input/input1
U: Uniq=
H: Handlers=event1 
B: PROP=0
B: EV=3
B: KEY=80 0 78 0 40c0000 0 0 0

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="hwmdata"
P: Phys=
S: Sysfs=/devices/virtual/input/input2
U: Uniq=
H: Handlers=event2 
B: PROP=0
B: EV=5
B: REL=2

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="mtk-tpd"
P: Phys=
S: Sysfs=/devices/virtual/input/input3
U: Uniq=
H: Handlers=mouse0 event3 
B: PROP=2
B: EV=b
B: KEY=400 0 0 0 0 1000 40000800 0 0 0 0
B: ABS=2e38000 1000003

I: Bus=0019 Vendor=0001 Product=0001 Version=0100
N: Name="mtk-tpd-kpd"
P: Phys=
S: Sysfs=/devices/virtual/input/input4
U: Uniq=
H: Handlers=event4 
B: PROP=0
B: EV=3
B: KEY=1000 40000800 0 0 0 0

#8


3 就是 按键啊;
PAD/TP  的EVENT 的类型为 EV_ABS 绝对坐标,对应的参数为绝对坐标;
鼠标的EVENT 类型为 EV_REL 相对坐标,对应的参数为相对坐标,是当前位置的偏移量;
设备可能是多种EVENT 类型的组合。
比如  TP 上面可能有固定按键的,那就是  EV_KEY+EV_ABS了;

另外 ,你在应用层没有必要判断这些啊,你只要根据从  /dev/input/eventX 中读出的数据
判断EVENT的类型就可以了。

#9


引用 8 楼 linlan999 的回复:
3 就是 按键啊;
PAD/TP  的EVENT 的类型为 EV_ABS 绝对坐标,对应的参数为绝对坐标;
鼠标的EVENT 类型为 EV_REL 相对坐标,对应的参数为相对坐标,是当前位置的偏移量;
设备可能是多种EVENT 类型的组合。
比如  TP 上面可能有固定按键的,那就是  EV_KEY+EV_ABS了;

另外 ,你在应用层没有必要判断这些啊,你只要根据从  /dev/input/eventX 中读出的数据
判断EVENT的类型就可以了。
其实我是想往设备中注入事件,模拟一些操作,需要知道哪个才是目标设备,有什么好办法吗。

#10


#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>


#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 SYN_REPORT 0
#define SYN_CONFIG 1
#define SYN_MT_REPORT 2


void simulate_mouse(int fd)
{
    struct input_event event;
        memset(&event, 0, sizeof(event));
        gettimeofday(&event.time, NULL);
        event.type = EV_REL;
        event.code = REL_X;
        event.value = 10;
        write(fd, &event, sizeof(event));


        event.type = EV_REL;
        event.code = REL_Y;
        event.value = 10;
        write(fd, &event, sizeof(event));


        event.type = EV_SYN;
        event.code = SYN_REPORT;
        event.value = 0;
        write(fd, &event, sizeof(event));
}
//cat  /proc/bus/input/device
//mouse_simu  /dev/input/eventxx
int main(int argc,char ** argv)
{


int fd ;
int i = 0;


if(argc<2) {fprintf(stdout,"\r pls give me mouse event file\n");return -2;}


fd = open(argv[1],O_RDWR);


if(fd<0) {fprintf(stdout,"\r can not open file:%s \n",argv[1]);return -1;}


  
    for(i=0; i< 10; i++)
    {
        simulate_mouse(fd);  
        sleep(1);
    }


close(fd);
return 0;
}


lz 还是熟悉下kernel中的input 子系统。

#11


引用 10 楼 linlan999 的回复:
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>


#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 SYN_REPORT 0
#define SYN_CONFIG 1
#define SYN_MT_REPORT 2


void simulate_mouse(int fd)
{
    struct input_event event;
        memset(&event, 0, sizeof(event));
        gettimeofday(&event.time, NULL);
        event.type = EV_REL;
        event.code = REL_X;
        event.value = 10;
        write(fd, &event, sizeof(event));


        event.type = EV_REL;
        event.code = REL_Y;
        event.value = 10;
        write(fd, &event, sizeof(event));


        event.type = EV_SYN;
        event.code = SYN_REPORT;
        event.value = 0;
        write(fd, &event, sizeof(event));
}
//cat  /proc/bus/input/device
//mouse_simu  /dev/input/eventxx
int main(int argc,char ** argv)
{


int fd ;
int i = 0;


if(argc<2) {fprintf(stdout,"\r pls give me mouse event file\n");return -2;}


fd = open(argv[1],O_RDWR);


if(fd<0) {fprintf(stdout,"\r can not open file:%s \n",argv[1]);return -1;}


  
    for(i=0; i< 10; i++)
    {
        simulate_mouse(fd);  
        sleep(1);
    }


close(fd);
return 0;
}


lz 还是熟悉下kernel中的input 子系统。
最近做了些研究,模拟事件我已经实现了,现在的问题是,每个终端的touchpad设备不一定对应到哪个/dev/input/eventX.照您的办法,好像还是需要cat  /proc/bus/input/device然后肉眼找到目标设备,再行模拟。我需要能普遍适应的办法,怎样自动找到目标设备。

#12


http://blog.csdn.net/lanmanck/article/details/8423669
ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask); 
#define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8))) 
for (j = 0; j < EV_MAX; j++) {  
                if (test_bit(j)) {  
                    const char *type = "unknown";  
                    switch(j) {  
                    case EV_KEY: type = "keys/buttons"; break;  
                    case EV_REL: type = "relative";     break;  
                    case EV_ABS: type = "absolute";     break;  
                    case EV_MSC: type = "reserved";     break;  
                    case EV_LED: type = "leds";         break;  
                    case EV_SND: type = "sound";        break;  
                    case EV_REP: type = "repeat";       break;  
                    case EV_FF:  type = "feedback";     break;  
                    }  

#13


Thank you, 这个答案是我想要的。但是还有一个问题,就是这样用ioctl检测设备的能力,结果会有多个设备拥有同样的能力,比如都有EV_ABS这个能力,那么我还是难以判断到底是哪个设备在起作用,有什么办法吗?

引用 12 楼 linlan999 的回复:
http://blog.csdn.net/lanmanck/article/details/8423669
ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask); 
#define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8))) 
for (j = 0; j < EV_MAX; j++) {  
                if (test_bit(j)) {  
                    const char *type = "unknown";  
                    switch(j) {  
                    case EV_KEY: type = "keys/buttons"; break;  
                    case EV_REL: type = "relative";     break;  
                    case EV_ABS: type = "absolute";     break;  
                    case EV_MSC: type = "reserved";     break;  
                    case EV_LED: type = "leds";         break;  
                    case EV_SND: type = "sound";        break;  
                    case EV_REP: type = "repeat";       break;  
                    case EV_FF:  type = "feedback";     break;  
                    }  

#14


没有必要判断
你的目的不就是模拟输入吗,只要它支持这个event type ,向其中写就可以了
就像你的笔记本电脑,有touchpad,PS/2 mouse,USB mouse, 你操作任何一个设备都可以实现移动光标的目的

#15


引用 14 楼 linlan999 的回复:
没有必要判断
你的目的不就是模拟输入吗,只要它支持这个event type ,向其中写就可以了
就像你的笔记本电脑,有touchpad,PS/2 mouse,USB mouse, 你操作任何一个设备都可以实现移动光标的目的
你说的有道理,太厉害了。

#16


引用 14 楼 linlan999 的回复:
没有必要判断
你的目的不就是模拟输入吗,只要它支持这个event type ,向其中写就可以了
就像你的笔记本电脑,有touchpad,PS/2 mouse,USB mouse, 你操作任何一个设备都可以实现移动光标的目的
但有个问题,是否只要设备支持这个能力,向其中写入,都确定会被内核接收呢?

#17


如果软件实现看能不能通过每个event的name使用通配符实现,我也正准备尝试

#18


这帖子不错,不过我觉得我转载这篇文章更加实用些
http://blog.csdn.net/lqxandroid2012/article/details/51578313

#1


linux kernel中的文档有说明:https://www.kernel.org/doc/Documentation/input/input.txt

  You can use blocking and nonblocking reads, also select() on the
/dev/input/eventX devices, and you'll always get a whole number of input
events on a read. Their layout is:

struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value;
};

  'time' is the timestamp, it returns the time at which the event happened.
Type is for example EV_REL for relative moment, EV_KEY for a keypress or
release. More types are defined in include/linux/input.h.

  'code' is event code, for example REL_X or KEY_BACKSPACE, again a complete
list is in include/linux/input.h.

  'value' is the value the event carries. Either a relative change for
EV_REL, absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for
release, 1 for keypress and 2 for autorepeat.

#2


可以考虑参考下
cat /proc/bus/input/devices 
的输出

#3


引用 2 楼 linlan999 的回复:
可以考虑参考下
cat /proc/bus/input/devices 
的输出


如果cat得出的是如下内容,如何判断呢?

130|root@android:/ # cat /proc/bus/input/handlers                              
N: Number=0 Name=kbd
N: Number=1 Name=sysrq (filter)
N: Number=2 Name=evdev Minor=64
N: Number=3 Name=keychord
root@android:/ # cat /proc/bus/input/devices                                   
I: Bus=0019 Vendor=0001 Product=0001 Version=0100
N: Name="rk29-keypad"
P: Phys=gpio-keys/input0
S: Sysfs=/devices/platform/rk29-keypad/input/input0
U: Uniq=
H: Handlers=kbd event0 keychord 
B: PROP=0
B: EV=3
B: KEY=8000 1c0000 0 0 0

I: Bus=0018 Vendor=0000 Product=0000 Version=0000
N: Name="gslX680"
P: Phys=
S: Sysfs=/devices/platform/rk30_i2c.2/i2c-2/2-0040/input/input1
U: Uniq=
H: Handlers=event1 
B: PROP=2
B: EV=10000b
B: KEY=0
B: ABS=2658000 0

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="gsensor"
P: Phys=
S: Sysfs=/devices/platform/rk30_i2c.0/i2c-0/0-004c/input/input2
U: Uniq=
H: Handlers=event2 
B: PROP=0
B: EV=9
B: ABS=7

#4


#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=3 , 3=2+1, 也就是 (2<<1+1)  说明是设备有按键 EV_KEY/EV_SYNC;
EV=b, 11= 2<<3+2<<1+1 , 也就是设备支持 EV_ABS/EV_KEY/EV_SYNC 设备有按键,触摸屏/板;
MISC 设备不好判断;

#5


http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/
http://*.com/questions/2547616/how-can-i-translate-linux-keycodes-from-dev-input-event-to-ascii-in-perl
http://blog.csdn.net/lanmanck/article/details/8423669

may those help.

#6


引用 5 楼 linlan999 的回复:
http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/
http://*.com/questions/2547616/how-can-i-translate-linux-keycodes-from-dev-input-event-to-ascii-in-perl
http://blog.csdn.net/lanmanck/article/details/8423669

may those help.


兄弟实在太厉害了。解答了我困扰已久的问题。我先研究研究。

#7


引用 4 楼 linlan999 的回复:
#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=3 , 3=2+1, 也就是 (2<<1+1)  说明是设备有按键 EV_KEY/EV_SYNC;
EV=b, 11= 2<<3+2<<1+1 , 也就是设备支持 EV_ABS/EV_KEY/EV_SYNC 设备有按键,触摸屏/板;
MISC 设备不好判断;


兄弟,这个办法好像也有问题,比如多个eventX的EV值都是3,就难以判断到底谁是keypad, 比如我的设备:

shell@android:/ $ cat /proc/bus/input/devices
I: Bus=0019 Vendor=2454 Product=6575 Version=0010
N: Name="mtk-kpd"
P: Phys=
S: Sysfs=/devices/platform/mtk-kpd/input/input0
U: Uniq=
H: Handlers=event0 
B: PROP=0
B: EV=3
B: KEY=1c0000 0 0 0

I: Bus=0019 Vendor=0000 Product=0000 Version=0000
N: Name="ACCDET"
P: Phys=
S: Sysfs=/devices/virtual/input/input1
U: Uniq=
H: Handlers=event1 
B: PROP=0
B: EV=3
B: KEY=80 0 78 0 40c0000 0 0 0

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="hwmdata"
P: Phys=
S: Sysfs=/devices/virtual/input/input2
U: Uniq=
H: Handlers=event2 
B: PROP=0
B: EV=5
B: REL=2

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="mtk-tpd"
P: Phys=
S: Sysfs=/devices/virtual/input/input3
U: Uniq=
H: Handlers=mouse0 event3 
B: PROP=2
B: EV=b
B: KEY=400 0 0 0 0 1000 40000800 0 0 0 0
B: ABS=2e38000 1000003

I: Bus=0019 Vendor=0001 Product=0001 Version=0100
N: Name="mtk-tpd-kpd"
P: Phys=
S: Sysfs=/devices/virtual/input/input4
U: Uniq=
H: Handlers=event4 
B: PROP=0
B: EV=3
B: KEY=1000 40000800 0 0 0 0

#8


3 就是 按键啊;
PAD/TP  的EVENT 的类型为 EV_ABS 绝对坐标,对应的参数为绝对坐标;
鼠标的EVENT 类型为 EV_REL 相对坐标,对应的参数为相对坐标,是当前位置的偏移量;
设备可能是多种EVENT 类型的组合。
比如  TP 上面可能有固定按键的,那就是  EV_KEY+EV_ABS了;

另外 ,你在应用层没有必要判断这些啊,你只要根据从  /dev/input/eventX 中读出的数据
判断EVENT的类型就可以了。

#9


引用 8 楼 linlan999 的回复:
3 就是 按键啊;
PAD/TP  的EVENT 的类型为 EV_ABS 绝对坐标,对应的参数为绝对坐标;
鼠标的EVENT 类型为 EV_REL 相对坐标,对应的参数为相对坐标,是当前位置的偏移量;
设备可能是多种EVENT 类型的组合。
比如  TP 上面可能有固定按键的,那就是  EV_KEY+EV_ABS了;

另外 ,你在应用层没有必要判断这些啊,你只要根据从  /dev/input/eventX 中读出的数据
判断EVENT的类型就可以了。
其实我是想往设备中注入事件,模拟一些操作,需要知道哪个才是目标设备,有什么好办法吗。

#10


#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>


#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 SYN_REPORT 0
#define SYN_CONFIG 1
#define SYN_MT_REPORT 2


void simulate_mouse(int fd)
{
    struct input_event event;
        memset(&event, 0, sizeof(event));
        gettimeofday(&event.time, NULL);
        event.type = EV_REL;
        event.code = REL_X;
        event.value = 10;
        write(fd, &event, sizeof(event));


        event.type = EV_REL;
        event.code = REL_Y;
        event.value = 10;
        write(fd, &event, sizeof(event));


        event.type = EV_SYN;
        event.code = SYN_REPORT;
        event.value = 0;
        write(fd, &event, sizeof(event));
}
//cat  /proc/bus/input/device
//mouse_simu  /dev/input/eventxx
int main(int argc,char ** argv)
{


int fd ;
int i = 0;


if(argc<2) {fprintf(stdout,"\r pls give me mouse event file\n");return -2;}


fd = open(argv[1],O_RDWR);


if(fd<0) {fprintf(stdout,"\r can not open file:%s \n",argv[1]);return -1;}


  
    for(i=0; i< 10; i++)
    {
        simulate_mouse(fd);  
        sleep(1);
    }


close(fd);
return 0;
}


lz 还是熟悉下kernel中的input 子系统。

#11


引用 10 楼 linlan999 的回复:
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>


#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 SYN_REPORT 0
#define SYN_CONFIG 1
#define SYN_MT_REPORT 2


void simulate_mouse(int fd)
{
    struct input_event event;
        memset(&event, 0, sizeof(event));
        gettimeofday(&event.time, NULL);
        event.type = EV_REL;
        event.code = REL_X;
        event.value = 10;
        write(fd, &event, sizeof(event));


        event.type = EV_REL;
        event.code = REL_Y;
        event.value = 10;
        write(fd, &event, sizeof(event));


        event.type = EV_SYN;
        event.code = SYN_REPORT;
        event.value = 0;
        write(fd, &event, sizeof(event));
}
//cat  /proc/bus/input/device
//mouse_simu  /dev/input/eventxx
int main(int argc,char ** argv)
{


int fd ;
int i = 0;


if(argc<2) {fprintf(stdout,"\r pls give me mouse event file\n");return -2;}


fd = open(argv[1],O_RDWR);


if(fd<0) {fprintf(stdout,"\r can not open file:%s \n",argv[1]);return -1;}


  
    for(i=0; i< 10; i++)
    {
        simulate_mouse(fd);  
        sleep(1);
    }


close(fd);
return 0;
}


lz 还是熟悉下kernel中的input 子系统。
最近做了些研究,模拟事件我已经实现了,现在的问题是,每个终端的touchpad设备不一定对应到哪个/dev/input/eventX.照您的办法,好像还是需要cat  /proc/bus/input/device然后肉眼找到目标设备,再行模拟。我需要能普遍适应的办法,怎样自动找到目标设备。

#12


http://blog.csdn.net/lanmanck/article/details/8423669
ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask); 
#define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8))) 
for (j = 0; j < EV_MAX; j++) {  
                if (test_bit(j)) {  
                    const char *type = "unknown";  
                    switch(j) {  
                    case EV_KEY: type = "keys/buttons"; break;  
                    case EV_REL: type = "relative";     break;  
                    case EV_ABS: type = "absolute";     break;  
                    case EV_MSC: type = "reserved";     break;  
                    case EV_LED: type = "leds";         break;  
                    case EV_SND: type = "sound";        break;  
                    case EV_REP: type = "repeat";       break;  
                    case EV_FF:  type = "feedback";     break;  
                    }  

#13


Thank you, 这个答案是我想要的。但是还有一个问题,就是这样用ioctl检测设备的能力,结果会有多个设备拥有同样的能力,比如都有EV_ABS这个能力,那么我还是难以判断到底是哪个设备在起作用,有什么办法吗?

引用 12 楼 linlan999 的回复:
http://blog.csdn.net/lanmanck/article/details/8423669
ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask); 
#define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8))) 
for (j = 0; j < EV_MAX; j++) {  
                if (test_bit(j)) {  
                    const char *type = "unknown";  
                    switch(j) {  
                    case EV_KEY: type = "keys/buttons"; break;  
                    case EV_REL: type = "relative";     break;  
                    case EV_ABS: type = "absolute";     break;  
                    case EV_MSC: type = "reserved";     break;  
                    case EV_LED: type = "leds";         break;  
                    case EV_SND: type = "sound";        break;  
                    case EV_REP: type = "repeat";       break;  
                    case EV_FF:  type = "feedback";     break;  
                    }  

#14


没有必要判断
你的目的不就是模拟输入吗,只要它支持这个event type ,向其中写就可以了
就像你的笔记本电脑,有touchpad,PS/2 mouse,USB mouse, 你操作任何一个设备都可以实现移动光标的目的

#15


引用 14 楼 linlan999 的回复:
没有必要判断
你的目的不就是模拟输入吗,只要它支持这个event type ,向其中写就可以了
就像你的笔记本电脑,有touchpad,PS/2 mouse,USB mouse, 你操作任何一个设备都可以实现移动光标的目的
你说的有道理,太厉害了。

#16


引用 14 楼 linlan999 的回复:
没有必要判断
你的目的不就是模拟输入吗,只要它支持这个event type ,向其中写就可以了
就像你的笔记本电脑,有touchpad,PS/2 mouse,USB mouse, 你操作任何一个设备都可以实现移动光标的目的
但有个问题,是否只要设备支持这个能力,向其中写入,都确定会被内核接收呢?

#17


如果软件实现看能不能通过每个event的name使用通配符实现,我也正准备尝试

#18


这帖子不错,不过我觉得我转载这篇文章更加实用些
http://blog.csdn.net/lqxandroid2012/article/details/51578313