鼠标滚轮滚动 - 如何捕获滚动开始和停止之间的时间间隔?

发布于 2024-09-02 06:43:23 字数 254 浏览 5 评论 0原文

有没有办法捕获鼠标滚轮滚动开始和停止之间的时间间隔?实际上,我想捕获当我快速滚动鼠标滚轮时滚动开始和停止之间的间隔。

我已经看过 MouseWheel 事件,但它不能满足我的要求。在某种意义上,它总是给出 Delta 120 或 -120 的值,但我想根据鼠标滚动的速度调用一个函数,例如,当我正常滚动鼠标时,我想执行函数 1,当我非常滚动鼠标时,我想执行函数 1。我想快速执行功能2。换句话说,有什么方法可以区分鼠标滚动的高速和正常速度。

任何建议将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北笙凉宸 2024-09-09 06:43:23

你不能捕获鼠标滚轮事件并查看它们之间的时间间隔吗?基本上,当您收到鼠标滚轮事件时启动计时器,然后在下一个事件中查看计时器的位置(以及事件之间经过了多长时间)以确定滚轮转动的速度?如果经过的时间小于某个阈值,则执行功能 2,如果快于某个阈值,则执行功能 1。

如果计时器关闭,您可能必须将其设置为执行功能 1,以防它们只执行一次滚动。

事实上,您可以这样做:

在鼠标滚轮事件中启动一个计时器(具有指示缓慢鼠标滚轮的间隔),然后如果计时器关闭,则执行功能 1。如果鼠标滚轮事件在计时器已关闭,然后重置计时器并增加计数器(以跟踪自您所做的事情以来车轮事件的数量),然后启动第二个(更长的)计时器。如果计数器大于某个阈值,则执行功能 2。当第二个定时器到期时,重置计数器。按照这些思路,您应该能够在车轮缓慢转动时启动功能 1,在车轮快速转动时通过几次“点击”来启动功能 2。

这段代码应该给出(非常肮脏的)我正在考虑的事情的指示。玩了一会儿之后,我不太确定这是一个好的解决方案......

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;
    }

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;
    }
稀香 2024-09-09 06:43:23

Take a look at the Control.MouseWheel event.

同尘 2024-09-09 06:43:23

根据 Sam Holder 的建议,我在这里发布了他的建议的修改版本,以帮助其他面临同样问题的程序员。

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文