APr*_*mer 7 cocoa trackpad nsevent
在我的应用程序中,我希望滚动发生,只有鼠标滚轮动作而不是触控板上的两个手指动作.基本上,我试图确定是否从鼠标或触控板生成scrollWheelEvent,内部 - (void)scrollWheel:(NSEvent*)theEvent方法.据我所知,到目前为止,似乎没有直接的方法来实现这一目标.
我尝试了将boolean变量设置为true和false in - (void)beginGestureWithEvent:(NSEvent*)event; 和 - (void)endGestureWithEvent:(NSEvent*)事件; 但这不是一个解决方案,因为在调用endGestureWithEvent:方法之后,会多次调用scrollWheel:方法.
这是我的代码:
$BOOL fromTrackPad = NO;
-(void)beginGestureWithEvent:(NSEvent *)event;
{
fromTrackPad = YES;
}
-(void) endGestureWithEvent:(NSEvent *)event;
{
fromTrackPad = NO;
}
- (void)scrollWheel:(NSEvent *)theEvent
{
if(!fromTrackPad)
{
//then do scrolling
}
else
{
//then don't scroll
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这是不标准的,但这是我的要求.有谁知道这样做的方法?谢谢!
APr*_*mer 16
-[NSEvent momentumPhase]是解决方案.因此,从触发板生成的beginGesture和endGesture事件之间的事件返回除NSEventPhaseNonefor 之外的值,-[NSEvent phase]并且在endGesture事件之后生成的触控板事件返回除NSEventPhaseNonefor 之外的值-[NSEvent momentumPhase].代码如下,
- (void)scrollWheel:(NSEvent *)theEvent
{
if(([theEvent momentumPhase] != NSEventPhaseNone) || [theEvent phase] != NSEventPhaseNone))
{
//theEvent is from trackpad
}
else
{
//theEvent is from mouse
}
}
Run Code Online (Sandbox Code Playgroud)