即使在macOS应用程序的窗口外,也会检测到3个手指平移手势

时间:2017-02-15 21:13:03

标签: objective-c swift macos gesture pan

如何在屏幕上的任何地方(不仅在我的应用程序窗口中)检测到我的mac触控板上的3指平移手势(不关注我的应用程序窗口)?

谢谢!

4 个答案:

答案 0 :(得分:4)

您可以使用GlobalMonitorForEvents -

  

安装一个事件监视器,用于接收发布到的事件的副本   其他应用。事件以异步方式发送到您的应用   你只能观察事件

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

在回调中添加一个计数器,用于按下次数。

答案 1 :(得分:2)

M5MultitouchSupport 可以解决您的问题。

首先安装并向您的项目添加库:pod 'M5MultitouchSupport'(或visit projectsite here),然后创建这样的代码(+ #import <M5MultitouchSupport.h>):

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

输出将是:

"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"

现在我还没有测试它,但理论上它应该可以工作。

编辑:

另外获取另一个信息:

/** 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;

所以:

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

答案 2 :(得分:1)

“三个手指在一个共同方向上刷过触控板表面是一种轻扫手势”

您可以使用下面的代码段。

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

“触控板”首选项窗格包含滚动手势选项:两个手指可以移动滚动视图的内容视图。从技术上讲,滚动手势不是特定的手势,而是鼠标事件。与手势不同,滚轮事件(即NSScrollWheel类型的事件)可以同时具有phase属性和momentumPhase属性。 momentumPhase属性可帮助您检测动量滚动,即使用户不再进行物理滚动,硬件也会继续发出滚轮事件。 Magic Mouse和Multi-Touch触控板等设备可实现动量滚动。

您可以查看更多信息HandlingTouchEvents

答案 3 :(得分:1)

要允许监控全局事件,您需要将应用程序设置为受信任的。它不适用于沙盒环境,因此也不适用于应用程序商店。

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

测试:

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

这是一个全面的insight