How to monitor for swipe gesture globally in OS X

青春壹個敷衍的年華 提交于 2019-12-06 08:35:31

问题


I'd like to make an OSX application that runs in the background and performs some function when a swipe down with four fingers is detected on the trackpad.

Seems easy enough. Apple's docs show almost exactly this here. Their example monitors for mouse down events. As a simple test, I put the following in applicationDidFinishLaunching: in my AppDelegate.

void (^handler)(NSEvent *e) = ^(NSEvent *e) {
  NSLog(@"Left Mouse Down!");
};

[NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDownMask handler:handler];

This works as expected. However, changing NSLeftMouseDownMask to NSEventMaskSwipe does not work. What am I missing?


回答1:


Well, the documentation for NSEvent's +addGlobalMonitorForEventsMatchingMask:handler: gives a list of event it supports and NSEventMaskSwipe is not listed so... it's to be expected that it not work.

While the API obviously supports the tracking of gesture locally within your own application (through NSResponder), I believe gestures can't be track globally by design. Unlike key combinations, there are much lower forms/types of gestures... essentially only:

  • pinch in/out (NSEventTypeMagnify)
  • rotations (NSEventTypeRotation)
  • directional swipes with X amount of fingers (NSEventTypeSwipe)

There's not as much freedom. With keys, you have plenty of modifiers (control, option, command, shift) and the whole alphanumeric keys making plenty of possible combinations so it'd be easier to reduce the amount of conflicts with local-events and global-events. Similarly, mouse events are region-based; clicking in one region can easily be differenciated from clicking in another region (from both the program's and user's point-of-view).

Because of this lower possible combination of touch events, I believe Apple might purposely be restricting global (as in, one app, responding to one or more gestures for the whole system) usage for its own usage (Mission Control, Dashboard, etc.)



来源:https://stackoverflow.com/questions/17152287/how-to-monitor-for-swipe-gesture-globally-in-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!