Skip to content

Instantly share code, notes, and snippets.

@macshome
Last active July 22, 2025 17:54
Show Gist options
  • Select an option

  • Save macshome/70c7d9759a7a82aa00d134bc8817ab09 to your computer and use it in GitHub Desktop.

Select an option

Save macshome/70c7d9759a7a82aa00d134bc8817ab09 to your computer and use it in GitHub Desktop.
Swift Playground to measure how much pressure you are putting on your trackpad
// A Cocoa based Playground to show how to use pressure events in a macOS application.
import AppKit
import PlaygroundSupport
class PressureView: NSView {
private let pressureLabel = NSTextField(labelWithString: "Pressure: 0.0")
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
pressureLabel.frame = NSRect(x: 20, y: 20, width: 200, height: 40)
pressureLabel.font = NSFont.systemFont(ofSize: 24)
addSubview(pressureLabel)
wantsLayer = true
layer?.backgroundColor = NSColor.white.cgColor
}
// We miss you Interface Builder... :(
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Override to handle pressure changes. Pressure is shown as a value between 0.0 and 1.0.
override func pressureChange(with event: NSEvent) {
super.pressureChange(with: event)
let pressure = event.pressure
pressureLabel.stringValue = String(format: "Pressure: %.2f", pressure)
}
}
// Make our view. You need to press in the view to see pressure changes.
let view = PressureView(frame: NSRect(x: 0, y: 0, width: 300, height: 100))
PlaygroundPage.current.liveView = view
@macshome
Copy link
Author

Demo of the PressurePoint playground.

PressurePoint.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment