首页 > 解决方案 > 如何从 WM_INPUT 原始数据中获取触摸屏协调

问题描述

我在 RegisterRawInputDevices 注册了我的触摸屏显示器。当我用一根手指点击时,我会收到许多 WM_INPUT 事件。

如何从 WM_INPUT HID(触摸)事件(坐标、触摸类型)中获取可读数据?

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
case WM_INPUT:
do
        {
            // determine the size of the input data
            UINT data_size(0);
            GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL,
                &data_size, sizeof(RAWINPUTHEADER));
            // preallocate our buffer
            vector<BYTE> data;
            data.resize(data_size);
            // and then read the input data in
            if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, &data[0],
                &data_size, sizeof(RAWINPUTHEADER)) != data_size)
            {
                // handle the error gracefully
                mylog("ERROR");
                break;
            }
            // the RAWINPUT structure starts at the beginning of our data array
            RAWINPUT* raw = (RAWINPUT*)(&data[0]);
            // make sure keyboard/mouse HID data didn't somehow sneak its way in here
            if (raw->header.dwType == RIM_TYPEHID)
            {
                // for each packet received..
                for (DWORD index(0); index < raw->data.hid.dwCount; ++index)
                {
                    mylog(".............HID packet .............");
                    // reinterpret the data as our nicely formatted digitizer-specific structure
                    DigitizerData* result((DigitizerData*)&raw->data.hid.bRawData[raw->data.hid.dwSizeHid * index]);
                    // for each touch registered...
                    for (BYTE touch_index(0); touch_index < result->active_touch_count; ++touch_index)
                    {
                        // insert touch handler code here
                        int touch_x(result->touch[touch_index].X());
                        int touch_y(result->touch[touch_index].Y());

                        mylog(string("Touch point ") + std::to_string(touch_x) + string(" x ") + std::to_string(touch_y));

                    }
                }
            }
        } while (0);
return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
}

数字化仪数据和代码来自https://www.codeproject.com/Articles/381673/Using-the-RawInput-API-to-Process-MultiTouch-Digit

一根手指点击时此代码的输出:

在此处输入图像描述

标签: c++winapitouchtouchscreenraw-input

解决方案


推荐阅读