MacOS Get Trackpad Pressure Globally

I am trying to get the pressure of a MacBook Pro trackpad with the following code:

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;
}

      

Do you have any idea how to do this?

+3


source to share


2 answers


Pressure data is available if the device supports it (i.e. trackpads with touch force). Boosted touch trackpads have been shipping on MacBooks since about 2015. Force Touch is also available on the Magic Trackpad.

This blog post has a method for detecting force sensing devices, although I haven't tried it.

My machine is a 2016 MacBook Pro with touch trackpad. I can get the pressure using this code:

[self.window trackEventsMatchingMask:NSEventMaskPressure 
    timeout:NSEventDurationForever 
    mode:NSEventTrackingRunLoopMode 
    handler:^(NSEvent * _Nonnull event, BOOL * _Nonnull stop) {
        NSLog(@"%f", event.pressure);
    }];

      

Output:

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

      



However, I can see that you are using an event response.

I have tried your code and when I check kCGMouseEventPressure

I get 1

. When I check event.pressure

, I also get 1

. So I get a value when you don't - I assume you don't have any power equipment? But I am not getting the actual pressure reading.

I'm not sure how to do this using an event.

This works for me on a 2016 MacBook Pro. Please note that this unit has a touchscreen trackpad. I am not that this will return on a machine without a touch trackpad. However, hopefully this gives you some more ideas.

Force touch for developers

NSEvent - pressure

+1


source


The MacBook built into the trackpad is not pressure sensitive, it only measures the width of the affected area, which usually increases as the pressure increases to simulate pressure.



There are some Apple developer docs (linked in the comment above) that relate to pressure, but they only apply to external tablets that measure pressure, such as Wacom tablets.

0


source







All Articles