本文介绍了Mac Cocoa:如何区分NSScrollWheel事件是来自鼠标还是触控板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想滚动发生,只有滚动鼠标操作,而不是从触摸板上的两个手指手势。基本上,我试图确定scrollWheelEvent是否从鼠标或触控板生成 - (void)scrollWheel:(NSEvent *)theEvent方法。从我迄今为止所知道的,似乎没有直接的方法来完成这一点。

In my application, I want the scrolling to happen, only with scroll wheel action from a mouse and not from the two finger gesture on a trackpad. Basically, I am trying to determine if the scrollWheelEvent is generated from the mouse or trackpad, inside - (void)scrollWheel:(NSEvent *)theEvent method. From what I know so far, it seems like there is no straightforward way to accomplish this.

我尝试了一个将布尔变量设为true和false的工作 - (void)beginGestureWithEvent:(NSEvent *)event;和 - (void)endGestureWithEvent:(NSEvent *)event;但这不是一个解决方案,因为scrollWheel:方法被调用几次,在endGestureWithEvent:方法被调用后。

I tried a work around of setting a boolean variable to true and false inside -(void)beginGestureWithEvent:(NSEvent *)event; and -(void)endGestureWithEvent:(NSEvent *)event; But this is not a solution because the scrollWheel: method is getting called several times, after the endGestureWithEvent: method is called.

这是我的代码:

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

我知道这是不是标准的东西,但这是我的要求。有没有人知道一种方法这样做?谢谢!

I know this is something that is not standard, but this is my requirement. Does anyone know a way to do this?? Thanks!

推荐答案

- [NSEvent momentumPhase] 因此,从beginGesture和endGesture事件之间的触控板生成的事件会为 - [NSEvent phase] 返回 NSEventPhaseNone c>和endGesture事件之后生成的触控板事件为 - [NSEvent momentumPhase] NSEventPhaseNone 以外的值>。代码如下,

-[NSEvent momentumPhase] is the solution. So, the events generated from the trackpad between the beginGesture and endGesture events returns a value other than NSEventPhaseNone for -[NSEvent phase] and the trackpad events that are generated after the endGesture event returns a value other than NSEventPhaseNone for -[NSEvent momentumPhase]. The code is below,

 - (void)scrollWheel:(NSEvent *)theEvent
    {
       if(([theEvent momentumPhase] != NSEventPhaseNone) || [theEvent phase] != NSEventPhaseNone))
       {
          //theEvent is from trackpad
       }
       else
       {
         //theEvent is from mouse
       }
    }

这篇关于Mac Cocoa:如何区分NSScrollWheel事件是来自鼠标还是触控板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:04