views:

213

answers:

3

Is there any way to capture the time interval between mouse wheel scroll start and stop? Actually I want to capture the interval between the scrolling start and stop when I very quickly scroll the mouse wheel.

I have already looked at MouseWheel event but it don't fulfill my requirement. In senes that it always gives a value of Delta 120 or -120 but i want to call a function depending on the speed of the mouse scroll for example when i scroll the mouse normally i want to perform function 1 and when i scrolled the mouse very quickly i want to perform the function 2. In other words is there any way to distinguish between the mouse scroll high and normal speed.

Any advice will be appreciated.

+1  A: 

Take a look at the Control.MouseWheel event.

David Neale
I have looked at it but it don't fulfill my requirement. In senes that it always gives a value of Delta 120 or -120 but i want to call a function depending on the speed of the mouse scroll for example when i scroll the mouse normally i want to perform function 1 and when i scrolled the mouse very quickly i want to perform the function 2. In other words is there any way to distinguish between the mouse scroll high and normal speed.
Rahat Ali
+1  A: 

can't you capture the mouse wheel events and see how long between them. Basically start a timer when you get a mouse wheel event and then in the next event see what the timer is at (and so how long has elapsed between the events) to determine the speed the wheel is being turned at? If the elapsedtime is smaller than a certain threshold, perform function2 and if it is faster than a certain threshold perform function 1.

You will probably have to set it to perform function 1 if the timer goes off in case they only do a single scroll.

In fact you might be able to do it this way:

start a timer (with an interval that indicates slow mouse wheeling) in the mouse wheel event, then if the timer goes off perform function 1. If the mouse wheel event happens again before the timer has gone off then reset the timer and increment a counter (to keep track of the number in wheel events since you did stuff) then start a second (longer) timer. if the counter is greater then a certain threshold perform function 2. When the second timer elapses, reset the counter. Something along those lines should give you the ability to fire function 1 when slow wheel turning and function 2 when the wheel is rapidly turned through a few 'clicks'.

this code should give a (very dirty) indication of the sort of thing I was thinking of. After playing a little I'm not really sure it's 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;
    }
Sam Holder
Can you kindly point me to an example of such stuff because i want to do this for each scroll not each wheel turn i.e. after one complete quick scroll i want to refresh the timer.
Rahat Ali
can you clearify that "then if the timer goes off perform function 1" what will be the interval of that timer. Thankx for your help.
Rahat Ali
I don't have an example, I just proposed a solution. you can't tell the speed of the wheel turn from a single event, you need to have multiple events to determine if the wheel is turning quickly or slowly. I suggest starting by setting up something to determine the time elapsed between two events and if it is < a certain value print "quick" to the console and if > a certain value print "slow"
Sam Holder
Thankx a lot for your help and patience. This is exactly the stuff which i am looking for. I am posting a little bit modified version of this suggestion as an answer for the help of other developers facing the same problem as mine.
Rahat Ali
A: 

As suggested by the Sam Holder i am posting here a modified verion of his advice 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;
    }
}
Rahat Ali