检测按住的鼠标 [英] Detect mouse being held down

查看:259
本文介绍了检测按住的鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够检测何时按住鼠标而不是点击。这是我的,但是我想要能够检测被按下的鼠标,而不是点击计数。

  (void)mouseDown:(NSEvent *)event; 
{
//而不是clickCount我想我的if语句是
//如果鼠标被按下。
if([event clickCount]< 1)
{

}
else if([event clickCount]> 1)
{

}
}


解决方案>推测你想要检测鼠标是否被按下一段时间。这很简单;



在您的 mouseDown:中,您将启动一个计时器, 。你需要把它粘贴到一个ivar中,因为你也可以在 mouseUp中引用它:

   - (void)mouseDown:(NSEvent *)theEvent {
mouseTimer = [NSTimer scheduledTimerWithTimeInterval:mouseHeldDelay
target:self
selector:@selector(mouseWasHeld :)
userInfo:theEvent
repeats:NO];
}

mouseUp: ,销毁计时器:

   - (void)mouseUp:(NSEvent *)theEvent {
[mouseTimer invalidate];
mouseTimer = nil;
}

如果定时器触发,则知道鼠标按钮已按下您可以采取任何您喜欢的操作:

   - (void)mouseWasHeld:(NSTimer *) tim {
NSEvent * mouseDownEvent = [tim userInfo];
mouseTimer = nil;
//等
}


I am trying to be able to detect when a mouse is held down instead of clicked. This is what I have, but instead of a click count I want to be able to detect the mouse being held down.

-(void)mouseDown:(NSEvent *)event;
{
    //instead of clickCount I want my if statement to be 
    // if the mouse is being held down.
    if ([event clickCount] < 1) 
    {

    }
    else if ([event clickCount] > 1)
    {

    }
}

解决方案

Presumably you want to detect whether the mouse is being held down for a certain period of time. This is pretty straightforward; it just requires a timer.

In your mouseDown:, you start a timer which will fire after your chosen period. You need to stick this into an ivar, because you will also refer to it in mouseUp:

- (void)mouseDown: (NSEvent *)theEvent {
    mouseTimer = [NSTimer scheduledTimerWithTimeInterval:mouseHeldDelay
                                                  target:self
                                                selector:@selector(mouseWasHeld:)
                                                userInfo:theEvent
                                                 repeats:NO];
}

In mouseUp:, destroy the timer:

- (void)mouseUp: (NSEvent *)theEvent {
    [mouseTimer invalidate];
    mouseTimer = nil;
}

If the timer fires, then you know that the mouse button has been held down for your specified period of time, and you can take whatever action you like:

- (void)mouseWasHeld: (NSTimer *)tim {
    NSEvent * mouseDownEvent = [tim userInfo];
    mouseTimer = nil;
    // etc.
}

这篇关于检测按住的鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆