提问人:FranzKafka Yu 提问时间:9/21/2023 最后编辑:FranzKafka Yu 更新时间:9/25/2023 访问量:36
如何从 /dev/input/event0 获取准确的坐标
how to get the accurate coordinates from /dev/input/event0
问:
我正在用我自己的程序在没有Eventhub&InputReader的情况下在Android中处理输入事件,并且代码在单点触摸屏中工作,而在多点触摸屏中无法工作,以下是我的代码(部分):
int readSize = read(mInputTouchfd, mEventData, EVS_READ_EVENT_MAX_NUM * sizeof(struct input_event));
if (readSize >= (int)sizeof(struct input_event))
{
int eventNum = readSize / sizeof(struct input_event);
LOG1("HandleTouchEvent has read %d input event", eventNum);
for (int i = 0; i < eventNum; i++)
{
LOG1("number %d event,details:type:[0x%X] code:[0x%X (%d)] value:[0x%X]", i, mEventData[i].type, mEventData[i].code, mEventData[i].code, mEventData[i].value);
if (mEventData[i].type == EV_ABS && mEventData[i].code == ABS_MT_POSITION_X)
{
realx = mEventData[i].value;
}
else if (mEventData[i].type == EV_ABS && mEventData[i].code == ABS_MT_POSITION_Y)
{
realy = mEventData[i].value;
}
else if (mEventData[i].type == EV_KEY && mEventData[i].code == BTN_TOUCH)
{
if (mEventData[i].value == TOUCH_EVENT_DOWN)
{
buttonEvent = TOUCH_EVENT_DOWN;
}
else if (mEventData[i].value == TOUCH_EVENT_UP)
{
buttonEvent = TOUCH_EVENT_UP;
}
}
else if (mEventData[i].type == EV_SYN && mEventData[i].code == SYN_REPORT)
{
LOG1("parse Event succeed:Event[%s],x[%d],y[%d]", toString(buttonEvent), realx, realy);
if (spHomePage)
{
spHomePage->dispatchTouchEvent(buttonEvent, realx, realy);
}
// reset event type after handle finished
buttonEvent = TOUCH_EVENT_MOVE;
}
}
}
我发现多点触摸屏中的事件代码、类型和值不同,我使用命令来捕获此类事件:getevent
/dev/input/event0: EV_KEY BTN_TOUCH UP
/dev/input/event0: EV_MSC MSC_TIMESTAMP 00032c80
/dev/input/event0: EV_SYN SYN_REPORT 00000000
/dev/input/event0: EV_ABS ABS_MT_TRACKING_ID 0000004d
/dev/input/event0: EV_ABS ABS_MT_POSITION_X 00003dad
/dev/input/event0: EV_ABS ABS_MT_POSITION_Y 000012bc
/dev/input/event0: EV_KEY BTN_TOUCH DOWN
/dev/input/event0: EV_ABS ABS_X 00003dad
/dev/input/event0: EV_ABS ABS_Y 000012bc
/dev/input/event0: EV_MSC MSC_TIMESTAMP 00034bc0
/dev/input/event0: EV_SYN SYN_REPORT 00000000
/dev/input/event0: EV_MSC MSC_TIMESTAMP 00036b00
当code=EV_ABS,type=ABS_MT_POSITION_X时,值为00003dad(hex),等于15789(Dec),比1920大得多(我的触摸屏是10英寸,16:9和1080p),我很困惑如何获得准确的坐标(包括x和y)?我认为值的单位是像素,而似乎不是,任何人都可以帮我?
我搜索了 ChatGPT/Google bard,但得到了错误的答案。
答:
0赞
FranzKafka Yu
9/25/2023
#1
最后我找到了这个问题的答案,在Android中,我们可以用来获取包括X / Y间隔在内的输入协议详细信息,例如:getevent
a1000b:/ $ getevent -lp /dev/input/event0
add device 1: /dev/input/event0
name: "ILITEK ILITEK-TP"
events:
KEY (0001): BTN_TOUCH
ABS (0003): ABS_X : value 11767, min 0, max 16384, fuzz 0, flat 0, resolution 34
ABS_Y : value 2656, min 0, max 9600, fuzz 0, flat 0, resolution 36
ABS_MT_SLOT : value 0, min 0, max 9, fuzz 0, flat 0, resolution 0
ABS_MT_POSITION_X : value 0, min 0, max 16384, fuzz 0, flat 0, resolution 34
ABS_MT_POSITION_Y : value 0, min 0, max 9600, fuzz 0, flat 0, resolution 36
ABS_MT_TRACKING_ID : value 0, min 0, max 65535, fuzz 0, flat 0, resolution 0
MSC (0004): MSC_TIMESTAMP
input props:
INPUT_PROP_DIRECT
a1000b:/ $
这里我们得到X值区间:0-16384,Y值区间:0-9600;
我们得到了这样的事件(也是我们的命令):getevent
130|a1000b:/ $ getevent -l /dev/input/event0
EV_ABS ABS_MT_TRACKING_ID 0000000b
EV_ABS ABS_MT_POSITION_X 00002e95
EV_ABS ABS_MT_POSITION_Y 00000f32
EV_KEY BTN_TOUCH DOWN
EV_ABS ABS_X 00002e95
EV_ABS ABS_Y 00000f32
EV_MSC MSC_TIMESTAMP 00000000
EV_SYN SYN_REPORT 00000000
EV_MSC MSC_TIMESTAMP 00001f40
这里我们得到x(ABS_MT_POSITION_X)=2e95(HEX),等于11925(DEC),我们转换为像素(我的屏幕是1920*1080):
11925/16384*1920=1397.4609375≈1397,so can got X(in Pixel)is 1397,and the Y value is same calculation.
评论