Mouse wheel scrolling - how can I capture the time interval between the start and stop of scrolling?

Is there a way to capture the time interval between scrolling and stopping the mouse wheel? I actually want to capture the interval between the start of the scroll and the stop when I scroll the mouse wheel very quickly.

I already looked at the MouseWheel event, but it doesn't match my requirement. In senes that it always gives a Delta value of 120 or -120, but I want to call a function depending on the scrolling speed of the mouse, for example, when I scroll my mouse, I want to execute function 1, and when I scroll the mouse very quickly I want to execute Function 2. In other words, is there a way to distinguish between high and normal mouse scrolling.

Any advice would be appreciated.

+2


a source to share


3 answers


You cannot capture mouse wheel events and see how long it takes between them. Basically start the timer when you get the mouse wheel event, and then in the next case see that the timer is located (and how long has elapsed between events) to determine the wheel speed? If the elapsed time is less than a certain threshold, execute function 2, and if it is faster than a certain threshold, execute function 1.

You may have to set it up to do function 1 if the timer goes out if only one scroll.

You could actually do it like this:



start the timer (with an interval that indicates a slow mouse wheel) on the mouse wheel event and then if the timer goes out, execute function 1. If the mouse wheel event happens again before the timer goes out, then reset the timer and increment the counter (to keep track of number in wheel events since you were doing things), then start a second (longer) timer. if the counter is greater, then the defined threshold performs function 2. When the second timer expires, reset the counter. Something along these lines should give you the ability to shoot function 1 when turning the wheel slowly and function 2 when the wheel turns quickly after a few clicks.

this code should give a (very dirty) indication of what I was thinking. After playing around a bit, I'm not sure if this is a good solution though ....

private void mouseWheelHandler (object sender, MouseEventArgs e)
    {
    slowTimer.Enabled = false;
    slowTimer.Stop ();            
    slowTimer.Interval = 200;
    slowTimer.Start();
    slowTimer.Enabled = true;
    m_counter++;
    Trace.WriteLine(string.Format("counter={0}", m_counter));
    if (fastTimer.Enabled==false)
        {
        fastTimer.Enabled = true;
        fastTimer.Interval = 150;
        fastTimer.Start ();
        }
    if (m_counter>5)
        {
        Trace.WriteLine("called method 2");
        m_counter = 0;
        fastTimer.Stop ();
        slowTimer.Enabled = false;
        slowCheckTimer.Stop ();                
        slowCheckTimer.Interval = 250;
        slowCheckTimer.Start();
        slowCheckTimer.Enabled = true;
        }
    }

private void slowTimer_Tick(object sender, EventArgs e)
    {
    Trace.WriteLine("slow timer ticked");
    if (slowCheckTimer.Enabled==false)
        {
        Trace.WriteLine ("called method 1");
        }

    slowTimer.Enabled = false;
    }

private void fastTimer_Tick(object sender, EventArgs e)
    {
    fastTimer.Enabled = false;
    Trace.WriteLine("fast timer ticked");
    m_counter = 0;
    fastTimer.Stop ();
    }

private void slowCheckTimer_Tick(object sender, EventArgs e)
    {
    Trace.WriteLine("slow check timer ticked");
    slowCheckTimer.Stop ();
    slowCheckTimer.Enabled = false;
    }

      

+2


a source


Take a look at the Control.MouseWheel event.



+1


a source


As suggested by Sam Holder, I am posting a modified version of his advice here to help other programmers facing the same problem.

public partial class Form1 : Form
{
    int m_counter = 0;

    public Form1()
    {
        InitializeComponent();

        // Attach Mouse Wheel Event
        this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
    }

    void Form1_MouseWheel(object sender, MouseEventArgs e)
    {
        // Refresh Slow Timer
        slowTimer.Enabled = false;
        slowTimer.Stop();
        slowTimer.Interval = 150;
        slowTimer.Start();
        slowTimer.Enabled = true;

        // Incremenet counter
        m_counter++;

        // Start Fast Timer
        if (fastTimer.Enabled == false)
        {
            fastTimer.Enabled = true;
            fastTimer.Interval = 50;
            fastTimer.Start();
        }

        // If this returns true call
        // the fast scroll implementation
        if (m_counter > 4)
        {
            Console.WriteLine("Quick Method Called");
            m_counter = 0;
            fastTimer.Stop();
            slowTimer.Enabled = false;
            slowCheckTimer.Stop();
            slowCheckTimer.Interval = 200;
            slowCheckTimer.Start();
            slowCheckTimer.Enabled = true;
        }
    }

    private void slowTimer_Tick(object sender, EventArgs e)
    {            
        if (slowCheckTimer.Enabled == false)
        {
            Console.WriteLine("Slow Method Called");
        }

        slowTimer.Enabled = false;
    }

    private void fastTimer_Tick(object sender, EventArgs e)
    {
        fastTimer.Enabled = false;            
        m_counter = 0;
        fastTimer.Stop();
    }

    private void slowCheckTimer_Tick(object sender, EventArgs e)
    {            
        slowCheckTimer.Stop();
        slowCheckTimer.Enabled = false;
    }
}

      

+1


a source







All Articles