How To: Detect Force Touch-Capable Devices on the Mac

In an effort to show preferences for configuring Force Touch in my apps (in particular, Yoink) only when a Force Touch device is actually available, I had to find a way to figure out how to detect Force Touch devices.

If this was iOS, I’d be done by now

On iOS, Apple provides a simple API for this:

UIForceTouchCapabilityUnavailable and UIForceTouchCapabilityAvailable

which you can check by calling UIView’s – (UIForceTouchCapability)traitCollection;. Lo and behold, a simple API like this is sadly not available on OS X. On the Mac, you have to do it yourself.

IOKit is where it’s at

Using IOKit, you can sort of set up a set of properties you’re looking for in a device and see if it returns anything. This does not only include an external or internal keyboard, mouse or trackpad, but also graphic cards, for example. To find out what the right properties are, I downloaded Apple’s Hardware IO Tools for Xcode 7.1 from their developer downloads site (Apple Developer account required) and launched the app IORegistryExplorer.

Digging down the IO Registry

I do have a Magic Trackpad 2 “attached” to my Mac via Bluetooth, so I tried searching for the term “Trackpad”, and sure enough, I saw my internal and external ones:

IORegistry TrackpadsThe Trackpads attached to my Mac, either via Bluetooth or internally.

Having found the Magic Trackpad 2, the next step is to see what properties it offers and if they are unique to the class of the device:

Trackpad PropertiesJackpot!

Sure enough, there it is – ForceSupported: True. I could not find such a key in the internal trackpad’s properties, hinting that it might be exclusive to devices that do support Force Touch. There’s also an entry for “Manufacturer”, which is “Apple Inc.”. Perfect.

Looking for Devices with IOKit

Now all I have to do is filtering devices by the Manufacturer – “Apple Inc.” -, iterate over the resulting devices, and filter out devices that have a DefaultMultitouchProperties key, containing a ForceSupported key with a value of true. If such a device is found, it means a device with Force Touch capabilities is available.

Code Listing #1

In this method, I create a dictionary mDict that is used to find matching devices. In this case, I’m looking for devices with the Manufacturer set to “Apple Inc.”. I query for possible devices using IOServiceGetMatchingServices. I can then iterate over the returned io_iterator_t iterator and recursively over the children in the core of all of this: – (BOOL)_containsForceTouchDevice:io_iterator_t)iterator;.

Code Listing #2

Here, we iterate recursively over iterator’s objects, checking for the DefaultMultitouchProperties key and, subsequently the ForceSupported key (and value).

Testing it with other Trackpads

The code you see above is final. However, in a previous version, all I could use to test it with was my Magic Trackpad 2 – an internal Force Touch Trackpad (the likes of which the new MacBooks and MacBook Pros feature) was not available to me directly. So I sent the first draft of the code to my friend (and fellow developer) Maurice Kelly (@mauricerkelly on twitter) who was kind enough to volunteer as my “test subject”; and – of course – it didn’t work. Turns out I shouldn’t assume the vendorID to be the same (which I used in the first draft just like the Manufacturer to filter the possible results). After leaving it out, it worked fine over all currently available Force Touch Trackpads.

Hot (Un-)Plugging

Sometimes you might want to get notified when devices are plugged in to or unplugged from the Mac. In Yoink, I’d like to display preferences specific to Force Touch if according hardware is available, but not show them if there isn’t any hardware connected that supports it. Also, as a nice touch, I’d like to hide the preferences if according hardware is disconnected and show it again when it is connected. We can accomplish this like that:

Code Listing #3


The code works just as well in the Sandbox environment, these entitlements have to be set, though, for the notifications to work:

Sandbox Entitlements and

Future Proof?

I’m not sure this code is future proof. The basic APIs will stay around, sure, but it all depends on the keys DefaultMultitouchProperties, ForceSupported and their according values which are not defined in the IOKit header files, which I unsuccessfully searched for constants pertaining to force/pressure. So, depending on this, the code might break with future versions of the Force Touch Trackpad and I’ll have to keep testing it as Apple releases new hardware. Nevertheless, I’m quite happy with the code and it works very well.

Getting the Source Code

I’ve uploaded a sample project to my server, you can download it here. The category on NSApplication in which this is available can be downloaded from Github. The example app is pretty simple:

Force Touch Device Detection Example App Screenshot

At launch, it asks if a Force Touch capable device is available and displays an according message. If subsequently the availability status changes, the message will be updated accordingly.

If you’d like to get in touch, you can mail me or write me on twitter. I’m looking forward to hearing from you :)

What I… (October 2015)

… Did

Interview for MacFormat about Metal (click)
Craig Grannell (@CraigGrannell on twitter) sent me and a couple of other developers a couple of questions about what we thought Metal would bring to the Mac. I’m very proud his article starts by quoting me:

Try to imagine riding a bike with square wheels. To move, more effort is required than when using ordinary wheels. This is how Eternal Storms Software founder Matthias Gansrigler describes OpenGL [compared to Metal] on the Mac.

It’s my first interview for a print magazine, my second overall (the first being this piece on the german site MacGadget a couple of years ago about OS X 10.5 Leopard).

Took part in the iTunes Affiliate Links Program (click)
I was told a couple of times now that I should set up affiliate links at least on my webpage, and I finally got around to do it. I don’t expect a lot of profit from this, but in this day and age, and as an indie developer, every bit helps. Setup is very easy and quickly done, so if you have a couple of minutes, I suggest you set up affiliate links as well.

Brought my cousin’s iOS game ‘Reach ZEN’ to the new Apple TV (click)
It’s a simple game, so the interface-conversion was done fairly quickly. Most of the APIs used are available on the tvOS as well (with the exception of social APIs – twitter and facebook).
What took the most time, though, was to make sure the game worked naturally, intuitively with the Apple TV Siri Remote.
The Focus Engine takes a little time to get used to and if you don’t use autolayout, you can forget using UIFocusGuide to aid the Focus Engine in finding the next UI element that should be focused.
Most buttons you see in ‘Reach ZEN’ are a custom class to draw the line animation, so making them focusable was fairly easy – changing the color to white if selected or a dark purple if not.
It’s a fun platform to develop for, and I’d like to implement multiplayer into ‘Reach ZEN’ at some point when I have some spare time.
The Apple TV is a very nice platform, I think, and the stuff that’s appeared on its App Store thus far looks very promising.
Anyway, ‘Reach ZEN’ is $0.99 on the App Store – and one download gets you the game for iPhone+iPad+Apple TV. I hope you enjoy – the Apple TV app will be available this or next week :)

Wrote about Pairing the Apple TV Siri Remote with the Mac for Testing Purposes (click)
Speaking of the new Apple TV, I figured out how to un-pair the Apple TV Siri Remote from the Apple TV and pair it with the Mac so it can be used in Xcode’s Apple TV Simulator, which is very nice if you’d like to quickly test some stuff locally.

Bought the Magic Trackpad 2 (click)
As soon as Force Touch arrived in the new MacBook, I’ve been wanting to do something with it. However, purchasing a new Mac just for a new Trackpad seemed a little over the top for me, so I kept hoping Apple would release a refreshed Magic Trackpad, which it thankfully did.
It’s a very cool device, it’s much larger than the previous one (I saw a picture online that compared its size to the iPad mini’s screen, and it’s almost identical), easy to the touch, very smooth.
I like the haptic feedback when fast-forwarding or rewinding in QuickTime – that’s just something you need to experience for yourself to really grasp how (r)evolutionary this is. I can’t see myself ever going back to a non-Force-Touch trackpad.
What astounds me is that the revised Magic Mouse didn’t get Force Touch. Just as much as it baffles me that the new Magic Keyboard didn’t get the butterfly mechanism for its keys.
So in an effort to exchange my wired equipment for wireless hardware (mouse and keyboard, primarily) I got my brother’s (old) Magic Mouse (which works just as well as the new one, just with batteries, imho) and I’m planning on purchasing the new Magic Keyboard (or the old one, if there’s a good deal somewhere). I believe I’ll miss the number pad of the USB keyboard, though. It is pretty comfortable.
The new keyboard is pretty close to what I remember the MacBook’s keyboard’s keys felt like – and I liked it (I had a chance to test it at Saturn last week). But it’s not enough to convince me to shell out 50 bucks more.

Force Touch in Yoink (click)
À propos the Magic Trackpad 2, Yoink is the first app of mine that I’d like to make use of Force Touch. In what turned out to be an odyssey of a couple of weeks, I finally found out how to receive Force Touch events from an NSTableView (something that – one would think – would be easy to do, but it’s surprisingly convoluted and opaque).
In Yoink, force touching on a file will select all files to allow for easily dragging them all out at once. For now, a double-click with the option key pressed could be used, but I think Force Touch will feel more natural.
I’m also experimenting with haptic feedback, that you feel something when your drag enters Yoink’s window. I’m not convinced completely yet, but perhaps it will be an option soon.

Updated ScreenFloat to v5.1.12 (click)
As a minor update, it fixes a potential crashing bug when opening the Shots Browser, incorporates the new About panel and adds the ability to quickly create a new shot from the menu not having to dig down to the submenu but clicking on the containing menu item instead. It’s a minor thing, but it makes it a little bit faster to take a shot when not using the keyboard shortcut.
For version 2.0, naturally, with a new device like the Magic Trackpad 2, a lot of use cases come to mind, but that’s still a little time off. Plus, Force Touch is no where near being a mainstream input possibility, so that’s always something to keep in mind. In a couple of years, sure, everyone will have it, but right now, it’s very, very ‘niche’.

Coffee Machine with FrotherBought a new coffee machine
After my coffee maker bit the dust right after it’s warranty expired, I had to get a new one, and this time, with a milk frother. It’s coffee 2.0 – so good. Makes me feel like I lived in the dark ages before.

… Downloaded

OS X El Capitan IconOS X 10.11 El Capitan (click)
I don’t understand why Apple’s new version of OS X currently holds an average rating of 2½ stars – that seems very low to me.
To me, this update has been nothing but a breath of fresh air and brought new life into my retina MacBook Pro from 2012. The UI is much smoother (in Yosemite, three windows open and Mission Control would lose frames any place it could).

Plex IconPlex (click)
A natural addition to the new Apple TV. Be sure to get it.

Lara Croft GO IconLara Croft GO (click)
A marvellous puzzle-platforming-game, very nicely executed. Would be interesting to see it on the Apple TV, but for now, it’ll have to do on the iPad (which is also very nice, by the way). When I first downloaded it, I played an hour straight and had a hard time putting it down.

Beach Buggy Racing IconBeach Buggy Racing (click)
Something to test the new Apple Siri Remote with and it’s just plain fun, though controlling with the remote’s tilt-control took me a while to get used to (I also never got used to tilt-controls on the iPhone or iPad – I always switched to button controls where available). For some reason, on the Apple TV, it works better for me. Probably because the screen doesn’t turn with me turning the remote. That’s what always bugged me with tilt-controls.

… Read 

This time without a synopsis on my part as I’m pressed for time – my apologies. But the titles give you the gist of it anyway.

Why Apple Is Still Sweating The Details on iMac (click)

Lessons Learned with 3D Touch (click)

How Pixar Changed All The Rules To Make The Good Dinosaur (click)

Perfect Smooth Scrolling in UITableViews (click)

On Apple’s Incredible Platform Advantage (click)

Apple TV, iPhone, iPad: Best Controllers (click)

… Watched

It’s been a busy month, movie-wise. A lot of interesting new movies came out this month here in Austria and my girlfriend and I were trying hard to catch up ;) And it’s not over yet – The Hunger Games Finale, The Good Dinosaur, Star Wars… Pretty busy this year.

Inside Out PosterInside Out
Another Pixar masterpiece. My favorite remains Monsters, Inc. for now, but Inside Out makes for a good second, maybe third place in my personal ranking. They do try to make you cry a little too hard at the end there, but it’s a movie about emotions, so that’s to be expected. The imagery is amazing and the cast does an amazing job voicing Joy, Fear, Disgust, Anger and Sadness.
I’m wondering what took them so long to release it in Austria, though. Usually, these movies are released worldwide at the same time. They seem to be able to do it with The Good Dinosaur… Weird.

Crimson PeakCrimson Peak
An OK movie with a story not strong enough to really hold my interest. The cinematography and sets were amazing, though. A lot to see, for sure.

The Visit PosterThe Visit
M. Night Shyamalan is back, with a great, pretty scary movie. And it wouldn’t be a Shyamalan movie without a twist. What it is? Go see yourself ;)

Paranormal Activity-The Ghost Dimension PosterParanormal Activity – The Ghost Dimension
The thing about the Paranormal Activity movies that made cold sweat running down my skin was you never really got to see what actually haunted people. This changes in this (apparently final) movie of the series and takes away a certain je-ne-sais-quoi. It’s still scary, but not as primal-scary as the first couple of movies. Nevertheless, it’s a good conclusion to the series.

A Developer’s Honest Take on His Team’s Failure (click)
Remember that game that would end after a certain amount of people died in-game? Hear what went wrong, and what went right.

… Ate


Coffee Latte MacchiatoYum. Just sayin’

How To Receive Force Touch Events from an NSTableView

After having spent over a (completely fruitless) month on fiddling with this without having an actual Force Touch Trackpad to test with, having gotten one recently, it still was insanely difficult for me to finally figure out how to receive Force Touch events from an NSTableView. But I finally did it. Here’s how.

Apple Keynote - MacBook Force TouchImage Credit: Apple, Inc.

The Premise

In Yoink, the usual way to select all files (pressing ⌘-A like you would in almost any application) is not a viable option. Yoink is written to not have key focus, as it would otherwise interfere with work in other applications too much (believe me, I’ve tried).
So I had to come up with a different way to do it. What I’ve done up until now is to allow the user to hold down the option (⌥) key on the keyboard and double-clicking onto any file in Yoink to select all files.

When Force Touch was introduced, I thought that would be a very nice alternative to option-double-clicking. And it is. Once I got it working, anyway.

The Issue

What I thought would be the easiest way to get Force Touch events from an NSTableView was to override – (void)pressureChangeWithEvent:(NSEvent *)theEvent.
Only that it doesn’t get called as it would normally be called: continuously, after – (void)mouseDown:(NSEvent *)theEvent, with every change of pressure on the trackpad.

No, in an NSView-based NSTableView (I haven’t tested cell-based tableViews), -pressureChangeWithEvent: gets called at -mouseDown: with a pressure of 0.0, and then just “dies”, not getting called again until the next -mouseDown:.
That’s interesting. And there’s more interesting-ness going on.

Once I override the tableView’s -mouseDown:, -pressureChangeWithEvent: suddenly gets called continuously, as you’d expect. So something apparently happens inside the tableView’s -mouseDown: implementation that sort of prevents -pressureChangeWithEvent: from getting called.

Finding A Solution

Knowing that the table view’s default implementation of -mouseDown: somehow interferes with the easy way of using -pressureChangeWithEvent:, my first attempt at a solution was to override -mouseDown: and re-implement its functionality myself, because then I would receive -pressureChangeWithEvent: properly. Then it dawned on me what I was in for. Dragging something out of the tableView (something kind of essential in Yoink) suddenly didn’t work anymore. I’d have to re-implement the selection mechanism (selecting with a mouse click, de-selecting with the command key pressed, adding an additional selection with either the shift- or the command key, etc.)
That alone would have taken more time than the entire feature of having force-click-to-select-all is worth.

Next, I thought NSGestureRecognizer could work. I created a subclass of it that overrides -pressureChangeWithEvent: and checked to see if it was called. But it was the same thing all over again. Adding it to the tableView, the NSTableRowView, the NSTableCellView and the underlying contentView of NSWindow, trying to figure out if they maybe swallowed the method call yielded no result or any change. It was all the same. It got called at the first -mouseDown: call, but then stopped.

Next I tried a local NSEvent monitor thinking it might do the trick, and it works, but only when the window’s contentView is force clicked, not the tableView. A global monitor doesn’t seem to work with pressure events.

So the only way that seemed to get me anywhere at all was to override NSTableView’s -mouseDown:. And that’s what I ended up doing. At first. After a day’s work, though, it still didn’t yield any results what-so-ever. The amount of time I’ve wasted on this is astounding, but apparently, when you keep going and really want to get somewhere, you eventually will.

The Solution

In OS X 10.10 Yosemite, Apple introduced a new NSEvent-tracking API in NSWindow: – (void)trackEventsMatchingMask:(NSEventMask)mask timeout:(NSTimeInterval)timeout mode:(NSString *)mode handler:(void(^)(NSEvent *event, BOOL *stop))trackingHandler that lets you, in a tracking loop, monitor for events matching the mask you provide, in this case, NSEventMaskPressure.

I actually had played around with this before, coming recommended from Markus Müller (@fafner on twitter), developer of the marvellous Mac and iOS app MindNode, who, by the way, in order for me to be able to test this stuff before I got my own Force Touch-able Trackpad, kindly invited me to their office to use his MacBook – thank you, Markus.
However, I didn’t get very far, as I didn’t know how to use the API correctly. Calling it from – (void)awakeFromNib, for example, isn’t the best idea, as it a) doesn’t appear to be able to track anything and b) locks up the app. It has to be called from inside a tracking loop (like -mouseDown: or -pressureChangeWithEvent:).

Again, after a lot of time wasted getting nowhere, this is the solution I came up with:

Source Code for how to receive Force Touch events from an NSTableView

I decided to imitate NSTableView’s -action and -doubleAction methods for handling receiving force touch events in a target.
NSTableView can be set up with a -target that should receive the selectors set in -action-doubleAction, and with this, -forceTouchAction, as seen in the code above, uses the same approach – send the specified selector to the NSTableView’s -target.

This code starts tracking Pressure-, Drag- and Mouse Up events in -pressureChangeWithEvent:, and practically waits until the Pressure event’s stage 2 is reached, which is a force touch. Then it sends the -forceTouchAction to -target.

The important lesson that I learned here is that the events have to be forwarded to the window, as they’re intercepted. So calling [self.window postEvent:event atStart:NO]; is imperative.

I watch for NSLeftMouseUp and NSLeftMouseDragged because I need to be able to stop tracking at some point, and mouseUp and mouseDragged are the perfect moments to do that.

Sample Project

I’ve uploaded a quick sample project to my server which you can download here. It requires OS X 10.10.3 (because of the APIs used). I’ve only tested it On 10.11 El Capitan, though, so your mileage may vary.

I hope this sample code is useful to you and saves you all the headaches I’ve experienced (and a lot of time) figuring this stuff out. Maybe I’m not smart enough. Or maybe this is harder than it should be. I dream of a time where the Force Touch APIs on OS X get the loving treatment 3D Touch enjoys on iOS ;)