Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
362 views
Welcome To Ask or Share your Answers For Others

1 Answer

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?.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...