Detect if WM_MOUSEMOVE is caused by touch/pen

前端 未结 1 2005
鱼传尺愫
鱼传尺愫 2020-12-06 23:17

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 solu

相关标签:
1条回答
  • 2020-12-06 23:54

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

    0 讨论(0)
提交回复
热议问题