pra*_*mil 5 macos trackpad objective-c
我正在尝试使用以下代码获得 MacBook Pro 触控板压力:
CGEventRef eventTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef eventRef, void *refcon) {
NSEvent *event = [NSEvent eventWithCGEvent:eventRef];
NSLog(@"%lld", (int64_t)CGEventGetIntegerValueField(eventRef, kCGMouseEventPressure)); // returns 0
NSLog(@"%lld", (int64_t)CGEventGetIntegerValueField(eventRef, kCGTabletEventPointPressure)); // returns 0
NSLog(@"%lld", (int64_t)CGEventGetIntegerValueField(eventRef, kCGTabletEventTangentialPressure)); // returns 0
NSLog(@"%f", [event pressure]); // Assertion failure
return eventRef;
}
Run Code Online (Sandbox Code Playgroud)
你知道怎么做吗?
如果设备支持(即压力触摸触控板),则压力数据可用。大约自 2015 年起,MacBook 上就配备了压力触控板。Magic Trackpad 上也提供了压力触控功能。
这篇博文有一个检测压力触摸设备的方法,虽然我没有尝试过。
我的机器是 2016 款 MacBook Pro,配有压力触控板。我可以使用以下代码获得压力:
[self.window trackEventsMatchingMask:NSEventMaskPressure
timeout:NSEventDurationForever
mode:NSEventTrackingRunLoopMode
handler:^(NSEvent * _Nonnull event, BOOL * _Nonnull stop) {
NSLog(@"%f", event.pressure);
}];
Run Code Online (Sandbox Code Playgroud)
输出:
2018-02-09 18:16:10.986036-0800 forcetouch[4587:4164200] 0.437820
2018-02-09 18:16:10.993772-0800 forcetouch[4587:4164200] 0.457504
2018-02-09 18:16:11.001883-0800 forcetouch[4587:4164200] 0.476486
2018-02-09 18:16:11.010654-0800 forcetouch[4587:4164200] 0.494812
2018-02-09 18:16:11.017738-0800 forcetouch[4587:4164200] 0.512497
2018-02-09 18:16:11.028129-0800 forcetouch[4587:4164200] 0.529556
2018-02-09 18:16:11.033769-0800 forcetouch[4587:4164200] 0.546021
2018-02-09 18:16:11.042117-0800 forcetouch[4587:4164200] 0.561905
2018-02-09 18:16:11.049869-0800 forcetouch[4587:4164200] 0.577240
Run Code Online (Sandbox Code Playgroud)
但是,我看到您正在使用事件水龙头。
我尝试了你的代码,当我检查时kCGMouseEventPressure我得到了1。当我检查时event.pressure我也得到了1。所以我收到了一个你没有的值 - 我猜你没有强制触摸硬件?但我没有收到实际的压力值。
我不知道如何使用事件点击来获取它。
这适用于我的 2016 款 MacBook Pro。请注意,这台机器有一个压力触摸触控板。我不确定在没有强制触摸触控板的机器上这会返回什么。但希望这能给您带来更多想法。