codingdir logo sitemap sitemap |

Handling mouse wheel and keyboars events while a mouse button is pressed

By : , Category : c#

Instead of a global hook, you can go one level up and use IMessageFilter() instead. This will work for when your application is focused only. Not sure what level you need to work at...

public partial class MyUserControl : UserControl

    private MyFilter filter;

    public MyUserControl()
        filter = new MyFilter();
        filter.LButtonScroll += new MyFilter.LBUTTONSCROLLDELEGATE(filter_LButtonScroll);

    private void filter_LButtonScroll()
        Console.WriteLine("WM_MOUSEWHEEL while LBUTTONDOWN");

    private class MyFilter : IMessageFilter
        private bool LBUTTONDOWN = false;
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_LBUTTONUP = 0x202;
        private const int WM_MOUSEWHEEL = 0x20a;

        public delegate void LBUTTONSCROLLDELEGATE();
        public event LBUTTONSCROLLDELEGATE LButtonScroll;

        public bool PreFilterMessage(ref Message m)
            switch (m.Msg)
                case WM_LBUTTONDOWN:
                    LBUTTONDOWN = true;

                case WM_MOUSEWHEEL:
                    if (LBUTTONDOWN)
                        if (LButtonScroll != null)

                case WM_LBUTTONUP:
                    LBUTTONDOWN = false;
            return false;

ReLated :

For all practical purposes, the Tap and Click events are equivalent for a Button.

The Click event was originally defined in Silverlight for desktop Windows and it is only defined for the Button control (and derivatives such as HyperlinkButton). You can think of the Click event as the "traditional" way to handle a Button press.

The Tap event was added to the framework in Windows Phone 7.1 (Mango). Tap is defined in the UIElement class, which is the parent of many types of controls. You can handle a Tap event in a TextBlock, Image, and many other controls. Button is subclassed from UIElement as well, and thus can also receive Tap events. It is redundant that a Button can receive both Tap and Click events.


if u also read the second answer u can get some more info

In addition to scroll events, Webkit browsers fire mousemove events, since the cursor can be in a different position after the scrolling.

The solution is to manually check, whether the position changed, or not:

var timer;
var stoppedElement=document.getElementById("stopped");   // store element for faster access

function mouseStopped(){                                 // the actual function that is called
    stoppedElement.innerHTML="Mouse stopped";

    if ((window.lastX !== e.clientX && window.lastX !== undefined)
        || (window.lastY !== e.clientY && window.lastY !== undefined)) {
        stoppedElement.innerHTML="Mouse moving";
    window.lastX = e.clientX
    window.lastY = e.clientY
<div id="stopped"></div>

Expanding on what I said in my comment:

  1. Your window class forwards WM_MOUSEWHEEL to the edit control
  2. If the control key is pressed, the edit control ignores the WM_MOUSEWHEEL message and passes it to DefWindowProc.
  3. DefWindowProc passes the message up the parent chain (this behavior is documented in the WM_MOUSEWHEEL documentation).
  4. Your window receives the forwarded message and loops back to step #1

Eventually you run out of stack and your process is terminated.

There are three ways you could fix this:

The first (and probably the safest) is to use a flag to prevent the recursive loop; e.g.:

static bool fInForwardMsg; // if you have multiple windows you would want to make this a local variable

    if (!fInForwardMsg) {
        fInForwardMsg = true;
        SendMessage(hEdit, uMsg, wParam, lParam);
        fInForwardMsg = false;

The second solution relies on the fact that the edit control looks at the wParam value to see if the control key is down (it also checks for shift as well, incidentally). This is internal undocumented behavior and subject to change, so you shouldn't rely on it, but you should be able to prevent the problem by not forwarding the original value of wParam. E.g.:

    SendMessage(hEdit, msg, wParam & ~0xffff, lParam);

The third solution is also the simplest; since the edit control doesn't actually do anything if shift or control are held down, simply don't forward the message in those cases:

    if (!(wParam & (MK_SHIFT | MK_CONTROL))
        SendMessage(hEdit, msg, wParam, lParam);

You can get the built-in browser event by looking at transsmittedEvent.originalEvent.

This has clientX and pageX in Firefox as well as Chrome.


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .