Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect 3 fingers pan gesture even outside a macOS app's window perpetually

How can I detect a 3 fingers pan gesture on my mac trackpad everywhere on the screen (not only in my app's window) perpetually (without having the focus on my app's window) ?

Thanks !

like image 534
Jonathan Avatar asked Feb 15 '17 21:02

Jonathan


4 Answers

You can use GlobalMonitorForEvents -

installs an event monitor that receives copies of events posted to other applications. Events are delivered asynchronously to your app and you can only observe the event

https://developer.apple.com/reference/appkit/nsevent/1535472-addglobalmonitorforevents

And add a counter in the callback for the number of presses.

like image 53
Roee84 Avatar answered Oct 11 '22 12:10

Roee84


M5MultitouchSupport is can be solution for your problem.

First install and add library to your project: pod 'M5MultitouchSupport' (or visit projectsite here), and then create code like this (+ #import <M5MultitouchSupport.h>):

[M5MultitouchManager.sharedManager addListenerWithCallback:^(M5MultitouchEvent *event) {
  NSLog(event.description);
}];

And output will be:

"ID: 3, State: 4 (Touching), Position: [0.251363, 0.475246], Velocity: [0.009912, -0.003619], Minor Axis: 8.160000, Major Axis: 9.920000, Angle: 1.911052, Size: 0.750000",
"ID: 6, State: 4 (Touching), Position: [0.618595, 0.839751], Velocity: [-0.007434, -0.014476], Minor Axis: 8.230000, Major Axis: 9.220000, Angle: 1.570796, Size: 0.625000",
"ID: 8, State: 4 (Touching), Position: [0.410051, 0.792415], Velocity: [0.008673, 0.018095], Minor Axis: 7.660000, Major Axis: 8.890000, Angle: 1.570796, Size: 0.628906"

Now I am not test it yet, but in theory it should work.

EDIT:

Also to get another info:

/** Identifier of touch, persistent/applicable across events. */
@property (assign, readonly) int identifier;

/** Current state of touch. 0 is not touching, anything else is some kind of touching. */
@property (assign, readonly) M5MultitouchTouchState state;

/** Coordinates of touch (each value 0 -> 1, so basically percent of touch surface). */
@property (assign, readonly) float posX, posY;

/** Coordinates of touch (each value 0 -> 1, so basically percent of touch surface). */
@property (assign, readonly) float velX, velY;

/** Minor and major axis of touch. */
@property (assign, readonly) float minorAxis, majorAxis;

/** Angle of touch (angle of finger tip from north). */
@property (assign, readonly) float angle;

/** Size of touch (finger tip surface area touching). */
@property (assign, readonly) float size;

So:

NSLog(event.size); // Get size
NSLog(event.angle); // Get angle
NSLog(event.state); // Get state (4 = touching)
NSLog(event.posX); // get position x or y
like image 34
Samuel Tulach Avatar answered Oct 11 '22 13:10

Samuel Tulach


"Three fingers brushing across the trackpad surface in a common direction is a swipe gesture"

You can use below code snippet.

-(NSSet *)touchesMatchingPhase:(NSTouchPhase)phase inView:(NSView *)view;

The Trackpad preference pane includes an option for scroll gestures: two fingers moving the content view of a scroll view around. Technically, scroll gestures are not specific gestures but mouse events. Unlike a gesture, a scroll wheel event (that is, an event of type NSScrollWheel) can have both a phase property and a momentumPhase property. The momentumPhase property helps you detect momentum scrolling, in which the hardware continues to issue scroll wheel events even though the user is no longer physically scrolling. Devices such as Magic Mouse and Multi-Touch trackpad enable momentum scrolling.

You can see for more info HandlingTouchEvents

like image 2
Emre Gürses Avatar answered Oct 11 '22 11:10

Emre Gürses


To allow monitor global Events you need to enable your application as trusted. It won't work in a sandbox environment and so in the app store.

 NSDictionary *options = @{(__bridge id) kAXTrustedCheckOptionPrompt : @YES};
 AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef) options);

To test for:

BOOL trusted = AXIsProcessTrusted(); NSLog(@"Trusted Process: %d", trusted);

Here a comprehensive insight.

like image 1
voltae Avatar answered Oct 11 '22 11:10

voltae