cat /proc/bus/input/devices
I: Bus=0019 Vendor=0001 Product=0002 Version=0100
N: Name="Nexell Keypad"
P: Phys=nexell/input0
S: Sysfs=/devices/platform/nxp-keypad/input/input0
U: Uniq=
H: Handlers=event0
B: PROP=0
B: EV=13
B: KEY=100 0 100000 0 0 0
B: MSC=10
I: Bus=0013 Vendor=dead Product=beef Version=0101
N: Name="fa_ts_input"
P: Phys=input(ts)
S: Sysfs=/devices/virtual/input/input1
U: Uniq=
H: Handlers=mouse0 event1
B: PROP=0
B: EV=b
B: KEY=400 0 0 0 0 0 0 0 0 0 0
B: ABS=1000003
会发现不同的USB口的 Phys是不同的,下面是同一个设备插到不同USB口的时候的Sysfs
因此可以通过找到USB口对应的Sysfs值,然后找出对应的event
qt代码如下
bool getEvent(string sUSBName, char* pEventNo)
{
pEventNo[0] = 0;
FILE* pfile = NULL;
char szbuf[1024] = {0};
pfile = fopen("/proc/bus/input/devices", "r");
if(NULL == pfile)
{
fprintf(stderr, "fopen /proc/bus/input/devices error
");
return false;
}
while(!feof(pfile))
{
fgets(szbuf, 1024, pfile);
string stmp = szbuf;
if(stmp.find("Phys", 0) != string::npos && stmp.find(sUSBName, 0) != string::npos)
{
fgets(szbuf, 1024, pfile); ///跳过Sysfs
fgets(szbuf, 1024, pfile); ///跳过Uniq
fgets(szbuf, 1024, pfile); ///读取Handlers
stmp = szbuf;
if(stmp.find("Handlers", 0) == string::npos)
{
continue;
}
int iPos = stmp.find("event", 0);
if(iPos == string::npos)
{
continue;
}
strncpy(pEventNo, szbuf + iPos, 6);
fclose(pfile);
return true;
}
}
fclose(pfile);
return false;
}