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 ;)

Pair the Apple TV Developer Kit Siri Remote with Xcode’s Simulator (Updated October 6th, 2015)

In the latest Xcode beta (currently, 7.1 beta 2), I noticed this in its Release Notes (login required) :

(…)Developers running on Yosemite will not be able to pair the Apple TV Remote with the tvOS Simulator Runtime. (…)

That implies that it’s somehow possible to pair the Apple TV Remote with a Mac running OS X 10.11 El Capitan – but how? There’s no explanation anywhere to be found.

Un-pair the Apple TV Remote

If you’ve already paired your Apple TV Remote with your Apple TV Developer Kit, you won’t be able to pair it with your Mac – you’ll have to un-pair it first.

Remote and interaction remote 2xOriginal Image Credit: Apple Inc., markup mine.

A response to this forum threadon the Apple Developer Forums lead me to how to do it: Hold down the Menu and Volume-Up keys on your Apple TV Remote for about 5 seconds, perhaps a little longer, and the Remote’s pairing will be reset.

Pair it with your Mac

Launch System Preferences and click on Bluetooth. In there, you’ll see something like this:

Screenshot of System Preferences 05 10 2015 17 37 23The Apple TV Remote in System Preferences / Bluetooth.

Click on Pair to pair the Apple TV Remote with your Mac. There should be a spinning wheel for a couple of seconds after which it should be paired.

Ready to Rock

Now the Xcode tvOS Simulator automatically recognizes the paired Apple TV Remote and should respond to anything you do on it. Enjoy :)

Update (October 6th, 2015) – Media Control?

Oliver Drobnik (@Cocoanetics on twitter) was interested in what kind of Bluetooth profiles the Apple Developer Kit Remote supports – for example, the Media Control profile to be able to adjust volume, play/pause, etc) – or if it can only be used in Xcode’s Simulator.
He suggested I use the free Bluetooth Scanner App LightBlue to see what profiles it supports.
According to the app and this Apple website that lists the service names, the profiles supported are Battery Service, Bond Management, Device Info and a custom Apple service – so no media control for now.
But thanks to Oliver, now we know.

What I… (September 2015)

This exciting installment of “What I…” features a mysterious Apple TV Developer Kit and an amazing city trip to Zurich.

… Did

Yoink IconUpdated Yoink to Version 3.1 (click)
Yoink 3.1 brings many new languages (like Simplified Chinese and Japanese, among others), performance improvements (like when waiting for so-called promised drags) and energy usage improvements (for example, Yoink doesn’t try to create QuickLook previews for folders and apps anymore as they are just icons anyway. It’s a minor thing, but it can add up), as well as several bug fixes.
I highly recommend updating if you haven’t already – it’s a free update for existing customers of the app.

Apple TV Developer Kit Box

Received an Apple TV Developer Kit (click)
Yes, I was one of the lucky ones to be chosen to receive an Apple TV Developer Kit. I’d like to know how the “choosing process” works inside Apple. Was there a contingent of Apple TV Developer Kits per country and the lucky ones were chosen per country as well or was it just a global “lottery”. I wasn’t lucky enough to be able to go to WWDC this year (because I didn’t win the lottery), but I’m very happy and glad it worked this time. I’m not supposed to show or talk about it, but I can say I do like it (<disclaimer>this is not a review</disclaimer>).
My cousin and I released an iOS game last year, Reach ZEN, and I’m currently looking into bringing it onto the Apple TV platform. It’s been pretty easy so far and I’ll be sure to post about it soon.

ScreenFloat IconWorked on ScreenFloat 2.0 (click)
I’ve been working on ScreenFloat 2.0, particularly currently the layout of the floating shots. I’m not very keen on the widgets that currently appear when you mouse over a floating shot, I’d like to change that up a bit and make it less gimicky-looking. I’ve also started doing some prototyping on possible annotations. Looking good so far, but at this point, I can not share any more. I hope I’ll be able to, soon.

Collecting Photos of my late Dad
In November 2013, my dad passed away at 63. He was a well-known and respected journalist here in Austria (working for KathPress, Die Furche, Das WirtschaftsBlatt and Kurierlogistik.kurier) and I thought it would be a good idea to make the round through their offices and see if they had any photos of him I wasn’t in possession of.
They were all very accommodating, however, I only found very few photos, I thought they had more.
I also drove to our old house in Burgenland (the country-side of Austria) which doesn’t belong to us anymore, but the new owner was kind enough to hold on to some personal objects of ours, which contained family trees, a couple of photographs and, most interestingly, letters from the battlefield (of WWII) of my granddad’s brother. I’m now in the process of digitizing them – I never knew they existed. We did have letters from my granddad, but not from his brother.

… Downloaded

Seabeard App IconSeabeard for iPad (click)
This is a pretty, fun and highly addictive game. I’ve already spent way too much time with it, and I’ve only had it for a week.
You’re a seafarer trying to rebuild your destroyed island. It has some Zelda elements to it, which by itself makes it a winner in my book.

… Read

Apple Pencil vs. Wacom Cintiq (click)
A blog post by Linda Dong (an Apple employee currently on a year off for personal projects) about the iPad Pro and Apple Pencil vs. the Wacom Cintiq. Very insightful coming from a person who uses this stuff every day for work and probably for personal stuff as well, but perhaps a little biased since she worked on it as well. Still, worth a read and definitely an opinion that can be trusted _because_ she uses it daily.

Pixar in a Box | Khan Academy (click)
An introduction to how things work behind the scenes at Pixar. Very interesting.

In Their Own Words: An Oral History Of Diablo II (click)
A very detailed read of how Diablo II came to be with the guys who made it. You’ll need some time to read this ;)

How Apple Built 3D Touch (click)
Just goes to show that nothing at Apple is done on a whim.

How Apple’s New Retina Flash – 5 Years In The Making – Brightens Up Selfies (click)
Another example.

Trials and Upgrades Won’t Save Independent Software Developers (click)
I’m not sure I agree with what Elia has to say, but he does offer some interesting alternatives.

We Own You: Confessions of a Free-To-Play Producer (click)
Shocking how much information about a person can be out there and fairly easily accessed.

… Watched

Supernatural Season 1 DVD CoverSupernatural (click)
My girlfriend turned me onto this show and it’s pretty great. We’ve blasted through season 1-3 already and just can not stop watching. If you’re into demon hunters and the like, give this show a try, you won’t regret it.

… Ate

Baked CroissantsBaked Croissants

… Went to See

My girlfriend Britta and I spontaneously decided to take a trip to Zurich over the weekend, and boy, was it awesome. A great city and very friendly people all around.
We hardly used any public transportation and walked through all of Zurich over the course of three days, including both Rigiblick and Uetliberg. 68.312 steps later, we were back in Vienna (by plane, not by foot :P).

View from GrossmuensterView of Zurich and Zurich Lake from Grossmünster.

View from UetlibergMy girlfriend and I, with a grand view of Zurich and Zurich Lake from Uetliberg.