检测按住的鼠标 [英] Detect mouse being held down
问题描述
我想要能够检测何时按住鼠标而不是点击。这是我的,但是我想要能够检测被按下的鼠标,而不是点击计数。
(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屋!