Force Touch trackpad declares?

Hi there,
Having got myself a snazzy new MacBook Pro, are there any declares yet to work with Force Touch trackpads? I can think of a few things where it’d be useful to use them…
Cheers,
Hamish

You will need the NSGestureRecognizer and NSEvent classes. It allows you to install a recognizer for force touches and receive an event when a touch occurs. You’ll need to create a dynamic subclass of the gesture recognizer and then receive the pressureChangeWithEvent: event and proceed with processing the values from the event from there. It shouldn’t be super hard if you know what you are doing with declares, otherwise it will be very hard/impossible. My development machine remains Mavericks (don’t really like Yosemite and all of its issues) so unfortunately I can’t provide any assistance since these classes and properties were release in 10.10 and 10.10.3 respectively.

Relevant docs:
https://developer.apple.com/library/mac/documentation/AppKit/Reference/NSGestureRecognizer_Class/#//apple_ref/occ/instm/NSGestureRecognizer/pressureChangeWithEvent:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/index.html#//apple_ref/occ/instp/NSEvent/stage
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/index.html#//apple_ref/occ/instp/NSEvent/pressure

Thanks for this, Jason! Given I’ve not worked with Declares before and regard them with the same sort of awe as servicing a car’s engine (i.e. I know it’s possible; I know that I could probably do it if I learned how, but with no knowledge it’s just baffling), I may wait and see if anyone else comes up with any examples. It’s not something I need - just something that might be fun to play with, that’s all.

If you’re willing to experiment a little, try this…

create a project with 2 buttons in a window.

In Button1:

[code]Sub Open()
Declare Sub setButtonType lib “Cocoa” selector “setButtonType:” (obj_ref as ptr,aType as integer)

const NSAcceleratorButton = 8

setButtonType(ptr(me.Handle),NSAcceleratorButton)
End Sub
[/code]

Sub Action()
  Declare Function doubleValue lib "Cocoa" Selector "doubleValue" (obj as ptr) as Double
  
  me.Caption=str(doubleValue(ptr(me.Handle)))
  
  
End Sub

and in Button2:

[code]Sub Open()
Declare Sub setButtonType lib “Cocoa” selector “setButtonType:” (obj_ref as ptr,aType as integer)
Declare Sub setMaxAcceleratorLevel lib “Cocoa” selector “setMaxAcceleratorLevel:” (obj_ref as ptr,maxLevel as integer)

const NSMultiLevelAcceleratorButton = 9

setButtonType(ptr(me.Handle),NSMultiLevelAcceleratorButton)
setMaxAcceleratorLevel(ptr(me.Handle),5)
End Sub[/code]

Here I just get 1 and 0 which is what Apple says should happen on a non-pressure sensitive system…

[code]Sub Action()
Declare Function doubleValue lib “Cocoa” Selector “doubleValue” (obj as ptr) as Double

me.Caption=str(doubleValue(ptr(me.Handle)))

End Sub
[/code]

Button1 should register the continuos change in pressure, while Button2 should register 5 stepped levels and give you a little click as you pass each level.

I don’t have a force touch, so this is purely theoretical from my perspective.

You can also try this in a canvas’s mouseDrag event… In real world use, I think you’d want to doubleCheck that the event is a mouseDrag before accessing the pressure property.

[code] declare function NSClassFromString lib “Cocoa” (classname as CFStringRef) as ptr
declare function sharedApplication lib “Cocoa” selector “sharedApplication” (obj as ptr) as ptr
declare function currentEvent lib “Cocoa” selector “currentEvent” (obj as ptr) as ptr
declare function pressure lib “Cocoa” selector “pressure” (obj as ptr) as single

dim thePressure as double=pressure(currentEvent(sharedApplication(NSClassFromString(“NSApplication”))))
[/code]

Hey there,
Thanks so much for this, Jim! Really appreciate you taking the time. Afraid neither of them are working, though.
The first example gives me just 0 and 1 in the labels for both the buttons, so I think I get the same response as you.
For the second, I added a label (Label1) to the window, and then after the final line put

label1.text = cStr(thePressure)

but it didn’t change from its default value of ‘Untitled’.

[quote=180451:@Hamish Symington]but it didn’t change from its default value of ‘Untitled’.

[/quote]
Did you return true from mousedown? I get the label changing to “1”.

I’m wondering though, if force touch requires 64 bit. The NSEventTypePressure and NSEventMaskPressure are both only available with 64bit…

Ah, returning True from MouseDown on the canvas does make it change to 1.
No idea about 64-bit-ness, but it would definitely explain it…
Thanks again for the help!