简体   繁体   中英

Detect if WM_MOUSEMOVE is caused by touch/pen

I am experimenting with WM_TOUCH and want to detect if mouse events are synthesized from touch/pen events or due to an actual mouse event.

The official solution according to MSDN is to check if the result of GetMessageExtraInfo() has the upper 24 bits set to 0xFF515700 .

This works. Most of the time. If I use one finger, all is well and good, but if I use more than one, releasing the last one causes a mouse move with GetMessageExtraInfo() == 0 . Also, when the window loses focus via touch, up to 3 mouse move messages with GetMessageExtraInfo() == 0 are generated.

Is there a reliable way of disambiguating between mouse, touch and pen inputs?

The link you posted does show the only reliable way to discern between mouse messages generated by a physical mouse, and those synthesized in response to touch and pen input.

For completeness, here is the fully working code. The code relies on state that is valid only while handling a mouse message. Calling it at any other time has undefined behavior:

bool IsTouchEvent() {
    const LONG_PTR c_SIGNATURE_MASK = 0xFFFFFF00;
    const LONG_PTR c_MOUSEEVENTF_FROMTOUCH = 0xFF515700;

    LONG_PTR extraInfo = GetMessageExtraInfo();
    return ( ( extraInfo & c_SIGNATURE_MASK ) == c_MOUSEEVENTF_FROMTOUCH );
}

The additional WM_MOUSEMOVE messages you are observing, are an artifact of how the system implements its internal bookkeeping. For example, if a window is shown or hidden, the mouse cursor may be over a different window now, and needs to be recalculated. To do this, the system synthesizes an artificial WM_MOUSEMOVE message.

This effect is explained in Raymond Chen's blog: Why do I get spurious WM_MOUSEMOVE messages? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM