Hotkeys update

I was trying to run my WhatKeys app at work, because I actually had a use for it, and discovered it doesn't compile on Leopard. I'll fix that when I get a chance.

On a related note, the developer of the ShiftIt app I mentioned has a blog called "Cocoa Tips and Tricks." One of his posts is about implementing hotkeys, and one of the comments to that post mentions that Snow Leopard has an NSEvent method that makes it real easy. I see from the doc that:

Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted).

and:

Note that your handler will not be called for events that are sent to your own application.

I'll have to check whether the DDHotKey library I used has these limitations. Regardless, this is nice to know.

My hotkeys code

I finally cleaned up and posted the notes and code from my presentation at the last CocoaHeads. See here.

Hotkeys for running AppleScript was a requested topic. Since I didn't know anything about global hotkeys, the existence of DDHotKey and ShortcutRecorder was convenient to say the least.

Since some of the target audience was new to Cocoa programming, I sprinkled helpful comments and links throughout the code, but not nearly as many as I could have — it would have been like trying to write a whole book in comments.

I was surprised how many conceptual topics even a simple Cocoa app can get into that I take for granted. It just goes to show, even the best example code is not enough to get a new Cocoa programmer started, not in a serious way. You really have to commit a bunch of time and work through Apple's conceptual documentation or something like Hillegass's book. Or go to a good training camp like Hillegass's Big Nerd Ranch.

Intro to Global Hotkeys

[Presented at the joint meeting of CocoaHeads-NYC and the New York FileMaker Developers Group, July 8, 2010. I've heavily edited this since giving the presentation. The original text was much more sparse.]

Mixed audience for this meeting

Therefore:

  • Partly an intro to global hotkeys
  • Partly a glimpse into Cocoa development, for newcomers
  • Will show two apps, one trivial and one more advanced

App One: "Hello World" of hot keys

App is called "Hotness".

  • A minimal complete app
  • Bare-bones UI with five buttons (actually a matrix of five button cells)
  • Just one Objective-C class, four methods
  • Hotkey actions are defined in five AppleScript files
  • Uses a third-party library called DDHotKey which makes registering hotkeys very simple

Cocoa patterns and techniques:

  • target-action (the button matrix has a target)
  • delegation (the application object has a delegate)
  • calling AppleScript from Cocoa
  • bringing your application to the front (see the hotkey mapping for Control-0)

App Two: more realistic

App is called "WhatKeys".

  • User can create, modify, and remove hotkey assignments
  • Hotkeys can be mapped to either an AppleScript file or AppleScript code entered directly
  • Like Hotness, uses DDHotKey
  • Uses a third-party library called ShortcutRecorder for entering and displaying keyboard shortcuts

Cocoa patterns and techniques are same as in Hotness, plus:

  • MVC ("Model-View-Controller")
    • a model class (WKHotKeyAssignment)
    • view controller and window controller ("coordinating controllers")
    • array controller ("mediating controller")
  • bindings
  • properties
  • responder chain
  • user defaults and property lists (for saving the user's hotkey assignments)
  • handling NSError

Where to get

Here are links for the source:

[UPDATE 2011-01-12: I've put the code in GitHub and changed the above links accordingly.]

To compile the example code you'll need Apple's Developer Tools, which you can get for free here (requires registration). After installing the Dev Tools, double-click an xcodeproj file to open the project in Xcode.

[UPDATE 2001-02-09: This post used to be a WordPress "page", but I decided it should really be a regular blog post, so here it is.]