July 28th, 2021

How can I prevent the mouse from moving in response to touch input?

A customer had a program that responded to touch input, but they found that when the user touched the screen, the mouse jumped to the touch point. How can they prevent that?

What you can do is make your program WM_POINTER-aware: Process the various WM_POINTER messages directly, and don’t let them go to Def­Window­Proc. It is the Def­Window­Proc function that takes unprocessed pointer messages and turns them into equivalent mouse activity.

You can take our scratch program and make these changes:

    case WM_POINTERDOWN:
    case WM_POINTERUPDATE:
    case WM_POINTERUP:
    case WM_POINTERWHEEL:
    case WM_POINTERHWHEEL:
    {
        auto pointerId = GET_POINTERID_WPARAM(wParam);
        POINTER_INPUT_TYPE type;
        if (GetPointerType(pointerId, &type) && type == PT_MOUSE) {
            return DefWindowProc(hwnd, uiMsg, wParam, lParam);
        }
        /* here is where you process the pointer message directly */
        return 0;
    }

This program checks whether the pointer message came from a mouse. If so, then it lets the message go through and be processed normally.¹ Otherwise, it handles the message. Or at least, it would handle the message once you replace that comment with code that processes the message.

The mapping between pointer messages and mouse messages is

Pointer Mouse
WM_POINTERDOWN WM_*BUTTONDOWN
WM_POINTERUPDATE WM_MOUSEMOVE
WM_POINTERUP WM_*BUTTONUP
WM_POINTERWHEEL WM_MOUSEWHEEL
WM_POINTERHWHEEL WM_MOUSEHWHEEL

There are also corresponding nonclient pointer and mouse messages, but I’m going to let those be processed normally so you can use touch to drag the window by its title bar.

¹ Mouse messages by default don’t even come in as WM_POINTER messages, but you can change that with Enable­Mouse­In­Pointer.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • Jan RingoÅ¡

    How do WM_GESTURE and WM_TOUCH come into the picture?
    Which is translated into which or does third component generate them?

  • aybe

    Hey Raymond, can you shed some light on these two quirks when using Office?

    First, open Excel, save a document in that Microsoft Excel location; you get “You can’t save here. Please choose another location.”. Why does that even exist?

    Second, if you click “Hide Folders” in that save dialog, you suddenly see more tags to assign to the document. Why is that not an obvious thing?

    Thanks!

    • Flux

      First, you'd better post this in an Excel forum, where you're more likely to get a good answer. (Preferably, avoid a Microsoft-operated forum.)

      Second, no, you can't save in the "Microsoft Excel" location. You have to browse into its nodes. If you don't see any nodes, be sure to have signed in with your Microsoft 365 account. Then, verify if the locations are set up.

      Third, "tags" as you call them, appear both when folders are shown...

      Read more