objective-c – 检测按住鼠标

objective-c – 检测按住鼠标,第1张

概述我试图能够检测到何时按住鼠标而不是单击鼠标.这就是我所拥有的,但我希望能够检测到被按住的鼠标,而不是点击次数. -(void)mouseDown:(NSEvent *)event;{ //instead of clickCount I want my if statement to be // if the mouse is being held down. if ([ 我试图能够检测到何时按住鼠标而不是单击鼠标.这就是我所拥有的,但我希望能够检测到被按住的鼠标,而不是点击次数.

-(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)    {    }}
解决方法 据推测,您想要检测鼠标是否被按下一段时间.这非常简单;它只需要一个计时器.

在你的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;    // etc.}
总结

以上是内存溢出为你收集整理的objective-c – 检测按住鼠标全部内容,希望文章能够帮你解决objective-c – 检测按住鼠标所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1071130.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存