Search code examples
windowstouchdesktop

RegisterPointerInputTarget not consuming all input


I'm working in Windows 10 and I am trying to write a C++ program that intercepts all touch screen input, however some input is still getting through.

My code calls RegisterPointerInputTarget with PT_TOUCH to intercept touch input. This mostly seems to work, however the results are inconsistent. As a test I have added code that uses SendInput to move the mouse slowly to the right whenever touch input is detected. With my program running I can, for example, open up MS Paint and touch the screen. So long as I hold my finger still on the cursor moves slowly to the right as expected. The moment I move my finger however, the cursor snaps to the position under my finger, just as it would if my program were not running at all.

To give another example, if I try the same thing in visual studio, again the cursor moves slowly to the right until I move my finger at which point the cursor follows my fingers' movement but slowly, lagging be behind it with a significant delay.

The code to set up my window looks like this;

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable
   static const char* class_name = "DUMMY_CLASS";
   WNDCLASSEX wx = {};
   wx.cbSize = sizeof(WNDCLASSEX);
   wx.lpfnWndProc = WndProc;        // function which will handle messages
   wx.hInstance = hInst;
   wx.lpszClassName = class_name;
   HWND hWnd = 0;
   if (RegisterClassEx(&wx)) {
       hWnd = CreateWindowEx(0, class_name, "dummy_name", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
   }

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   if (RegisterTouchWindow(hWnd, 0) &&
       RegisterPointerInputTarget(hWnd, PT_TOUCH))
   {
     ...

and my message handling code looks like this;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_TOUCH:
    {
        INPUT Inputs[1] = { 0 };

        Inputs[0].type = INPUT_MOUSE;
        Inputs[0].mi.dx = 1;
        Inputs[0].mi.dy = 0;
        Inputs[0].mi.dwFlags = MOUSEEVENTF_MOVE;

        SendInput(1, Inputs, sizeof(INPUT));

Ideally this test code would simply move the cursor for any touch input. Any help in fixing or just understanding this would be greatly appreciated!


Solution

  • I have made some progress with this but have hit other, related, problems I will ask about in a separate question. I will add a comment here when that question is live. The key to sorting out this initial issue however was to make sure I am returning 0, without calling DefWindowProc from all WM_POINTERENTER, WM_POINTERLEAVE, WM_POINTERUP, WM_POINTERDOWN, WM_POINTERUPDATE and WM_TOUCH messages, and to put my SendInput call into the code processing the WM_UPDATE message.