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

4

13

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 !

Kinetics answered 15/2, 2017 at 21:13 Comment(0)
S
4

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.

Santasantacruz answered 19/2, 2017 at 9:34 Comment(2)
Special Considerations In OS X v 10.6, event monitors are only able to monitor the following event types: NSLeftMouseDragged NSRightMouseDragged NSOtherMouseDragged NSLeftMouseUp NSRightMouseUp NSOtherMouseUp NSLeftMouseDown NSRightMouseDown NSOtherMouseDown NSMouseMoved NSFlagsChanged NSScrollWheel NSTabletPoint NSTabletProximity NSKeyDown (Key repeats are determined using the isARepeat property.)Nudd
I didn't have the time to try yet. Based on the NSEventMask available, that seems possible. I will let you know. developer.apple.com/reference/appkit/nseventmaskKinetics
D
3

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
Demimondaine answered 25/2, 2017 at 12:46 Comment(0)
D
2

"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

Degas answered 25/2, 2017 at 7:53 Comment(0)
M
1

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.

Mar answered 25/2, 2017 at 8:2 Comment(1)
He didn't mention he uses NSEvent monitor. Also, documentation mentions that you need privileges for Key-related events, not touches or gestures.Tungusic

© 2022 - 2024 — McMap. All rights reserved.