Most Macs today come with a Force Touch trackpad, and the Magic Trackpad offers it too. Besides detecting how hard you press, this trackpad can give you a small physical “click” as feedback. macOS uses it in many places: when you drag a shape in Keynote and it snaps to a guide, when you move a window edge against another one, or when a control hits a stop position. The user can feel that something happened, without looking.
With version 26.5 of the MBS Xojo Plugins we add the NSHapticFeedbackManagerMBS class, so your Xojo applications can do the same.
The class
NSHapticFeedbackManagerMBS wraps the NSHapticFeedbackManager class from Apple’s AppKit framework. It is small and easy to use:
- defaultPerformer is a shared method that returns the feedback performer for the current input device. It respects the accessibility settings and the user’s preferences, so if the user has turned off haptic feedback, nothing will be felt.
- performFeedbackPattern(pattern, performanceTime) asks the trackpad to play a feedback pattern.
For the pattern you can pick one of three constants:
| Constant | Value | Use it for |
|---|---|---|
| PatternGeneric | 0 | General feedback, when none of the other options apply. |
| PatternAlignment | 1 | Alignment of any kind: snapping to guides, best fit, etc. |
| PatternLevelChange | 2 | Changes between discrete pressure levels or steps. |
And for the performance time:
| Constant | Value | Meaning |
|---|---|---|
| PerformanceTimeDefault | 0 | Currently the same as PerformanceTimeDrawCompleted. |
| PerformanceTimeNow | 1 | Perform immediately, do not synchronize. |
| PerformanceTimeDrawCompleted | 2 | Synchronize with the next screen drawing pass, so the feel matches what the user sees. |
Usually you want PerformanceTimeDefault, so the click happens exactly when the snapped object appears on screen.
Please note that the input device may change while your app runs, e.g. when the user connects a Magic Trackpad or switches to a mouse. So please don’t keep the object around. Ask for defaultPerformer each time you want to give feedback.
The feedback is only felt when the user is touching the trackpad, e.g. while dragging. That makes it perfect for drag operations and sliders. On Windows, Linux and iOS the class does nothing, so you can keep the calls in cross platform code, but we recommend to wrap them in #If TargetMacOS.
Simple example
Here is the shortest way to give a feedback:
Var manager As NSHapticFeedbackManagerMBS = NSHapticFeedbackManagerMBS.defaultPerformer
manager.performFeedbackPattern(NSHapticFeedbackManagerMBS.PatternGeneric, NSHapticFeedbackManagerMBS.PerformanceTimeNow)
Example: Snap a slider to the middle
Let’s do something more practical. We have a DesktopSlider from 0 to 100, for example for a balance or a color temperature. When the user drags close to the center, we snap the value to 50 and give an alignment feedback. Additionally we give a level change feedback when the user crosses each 25% mark.
First, add two properties to the window:
Private LastValue As Integer = -1
Private Snapped As Boolean
Then add a helper method to the window:
Private Sub HapticFeedback(pattern As Integer)
#If TargetMacOS Then
// always ask for the current performer, as the input device may change
Var manager As NSHapticFeedbackManagerMBS = NSHapticFeedbackManagerMBS.defaultPerformer
If manager <> Nil Then
manager.performFeedbackPattern(pattern, NSHapticFeedbackManagerMBS.PerformanceTimeDefault)
End If
#EndIf
End Sub
And finally the ValueChanged event of the slider:
Sub ValueChanged()
Const kCenter = 50
Const kSnapDistance = 3
Var v As Integer = Me.Value
// snap to center when close enough
If Abs(v - kCenter) <= kSnapDistance Then
If Not Snapped Then
Snapped = True
HapticFeedback(NSHapticFeedbackManagerMBS.PatternAlignment)
End If
If v <> kCenter Then
Me.Value = kCenter // this will call ValueChanged again
Return
End If
Else
Snapped = False
// feedback when we cross a 25% step
If LastValue >= 0 And (v \ 25) <> (LastValue \ 25) Then
HapticFeedback(NSHapticFeedbackManagerMBS.PatternLevelChange)
End If
End If
LastValue = v
ValueLabel.Text = v.ToString
End Sub
Now run the app, grab the slider knob with the trackpad and move it around. You feel a light tick whenever you pass 25, 50 or 75, and a click when the slider snaps to the center. The same technique works great in a DesktopCanvas, where you let the user drag objects around and snap them to guides or to the edges of other objects.
Conclusion
Haptic feedback is a small detail, but it makes your application feel more native on macOS. Please use it with care: only give feedback for meaningful events like snapping or reaching a limit, not for every pixel the mouse moves.
The NSHapticFeedbackManagerMBS class is included in the MBS Xojo Plugins 26.5. Please try it and let us know how you use it in your projects.