问题
How do I distinguish whether the event -(void)scrollWheel:(NSEvent *)event
was triggered by a Magic Mouse or a trackpad?
The reason I'm asking this question is because I want to assign a different action to the scrolling event when a trackpad is used because the user can pinch to zoom on the trackpad. On the magic mouse, however, the user can't pinch easily, so I want to use the scrolling function as a substitute for pinching.
I can distinguish between a normal mouse and a Magic Mouse using this line:
if (([event momentumPhase] != NSEventPhaseNone) || [event phase] != NSEventPhaseNone)
However this test is passed for both, trackpad and Magic Mouse.
回答1:
I was able to distinguish between a scroll wheel on a mouse (not a Magic Mouse, but I suspect it will still work) and a trackpad using NSEvent's subtype:
enum {
NSMouseEventSubtype = NX_SUBTYPE_DEFAULT,
NSTabletPointEventSubtype = NX_SUBTYPE_TABLET_POINT,
NSTabletProximityEventSubtype = NX_SUBTYPE_TABLET_PROXIMITY
NSTouchEventSubtype = NX_SUBTYPE_MOUSE_TOUCH
};
回答2:
You should handle scrollWheel
for the Magic Mouse and add a NSMagnificationGestureRecognizer
for the pinch gesture on the trackpad. The two do not conflict with each-other but the swipe-scroll on the trackpad will trigger scrollWheel
.
来源:https://stackoverflow.com/questions/14461529/nsevent-and-magic-mouse