how to properly record the length of mousewheel event in WPF

246 views Asked by At

The goal of the program is to record how fast, how long and the amplitude of mouse wheel rotation. I can record the amplitude in MouseWheel event. but my confusion is that I can record how long/fast?

The way I am doing now is to implement the MouseWheel event in a window. Then in another dispatch timer's handler, I check if mousewheel event does not last for a certain period (say 1s), I record the time. Is this the correct way?

   private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        scrollVal += (Math.Abs(e.Delta) / limitScrollSpeed);
        scroll_stopwatch.Reset();     //this makes small scrolls to be one bigger scroll
        scroll_stopwatch.Start();
        scrollRecord_stopwatch.Start();     
    }


    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        if (scroll_stopwatch.ElapsedMilliseconds >= clearScrollTimeMS)  
        {               

            scroll_stopwatch.Stop();
            scroll_stopwatch.Reset();
            scrollVal = 0;

            scrollRecord_stopwatch.Stop();
            scrollDuration = (scrollRecord_stopwatch.ElapsedMilliseconds - 1000) / 1000.0f;

            scrollRecord_stopwatch.Reset();                
        }
    }
1

There are 1 answers

1
Adam Finley On

Use the MouseWheelEventArgs.Timestamp property.

Your event handler would look something like this:

  1. Record the timestamp of the first wheelevent
  2. keep running sum of the wheel delta
  3. when the scrolling stops, divide the wheel delta sum by the last timestamp minus the first one.

Only have to use the stopwatch to decide if the scrolling has "stopped."