Menu

Detecting a three-finger trackpad click on macOS: MultitouchSupport frames, a CGEventTap, and why the late finger turns into a right-click

#swift#redditswift#click#fingers#finger
Reading 0:00
15s threshold

I wanted a physical three-finger click on the trackpad to open a panel in my menu bar app. There's no public API for that: NSEvent gives you clicks and gestures, but not "how many fingers were down when the button went down". Here's what ended up working, and the part that made my first version unreliable.

Reading the fingers. The private MultitouchSupport.framework (the one MiddleClick and Jitouch use) is loaded with dlopen. MTDeviceCreateList returns the trackpads, MTRegisterContactFrameCallback delivers a frame of contacts about 100 times a second while fingers are down, and MTDeviceStart starts it. Each contact is a 96-byte struct. I only read identifier (offset 16), state (20) and size (48). States go from 1 (in range) through 4 (touching) to 7 (gone). If identifier or state look like garbage, which would mean a different struct layout in a future macOS, it falls back to counting raw contacts.

Catching the click.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.