Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

nsevent - Mac Cocoa: How to differentiate if a NSScrollWheel event is from a mouse or trackpad?

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.

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.

Here is my code:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...