Karabiner-Elements (10.12 and later)
Karabiner stopped working in 10.12 due to changes in the kernel architecture, and Karabiner was rewritten as Karabiner-Elements which uses a new JSON format for the configuration files.
You can now add a rule like this to karabiner.json
:
{
"from": {
"key_code": "t",
"modifiers": {
"mandatory": ["right_option"]
}
},
"to": [
{
"shell_command": "open -a terminal"
}
],
"type": "basic"
}
This makes a short press of the right command key open Emacs:
{
"from": {
"key_code": "right_command",
"modifiers": {
"optional": ["any"]
}
},
"to": [
{
"key_code": "right_command",
"lazy": true
}
],
"to_if_alone": [
{
"shell_command": "open -a emacs"
}
],
"type": "basic"
}
Karabiner (10.11 and earlier)
Save a file like this as private.xml
:
<?xml version="1.0"?>
<root>
<vkopenurldef>
<name>KeyCode::VK_OPEN_URL_Safari</name>
<url>file:///Applications/Safari.app</url>
</vkopenurldef>
<vkopenurldef>
<name>KeyCode::VK_OPEN_URL_iTunes</name>
<url>file:///Applications/iTunes.app</url>
</vkopenurldef>
<vkopenurldef>
<name>KeyCode::VK_OPEN_URL_EMACS</name>
<url>file:///Applications/Emacs.app</url>
</vkopenurldef>
<item>
<name>custom</name>
<identifier>custom</identifier>
<autogen>__KeyToKey__ KeyCode::F11, KeyCode::VK_OPEN_URL_Safari</autogen>
<!-- change the right option key into an extra modifier key -->
<autogen>__KeyToKey__ KeyCode::OPTION_R, KeyCode::VK_MODIFIER_EXTRA1</autogen>
<autogen>__KeyToKey__ KeyCode::I, ModifierFlag::EXTRA1, KeyCode::VK_OPEN_URL_iTunes</autogen>
<!-- make a short press of the right command open Emacs -->
<autogen>__KeyOverlaidModifier__ KeyCode::COMMAND_R, KeyCode::COMMAND_R, KeyCode::VK_OPEN_URL_EMACS</autogen>
</item>
</root>
Alfred 2
If you have bought the Powerpack for Alfred 2, you can create a workflow like this:
Hotkeys have a short delay by default, but you can reduce the delay by changing the "Trigger behaviour" option from "Simulate modifier keys released" to "Pass through modifier keys (Fastest)":
Automator services
Open Automator, select "Service" as the type of the document, change "Service receives" from "text" to "no input", add a "Launch Application" action, and save the service.
Then assign a keyboard shortcut for the service from System Preferences > Keyboard > Shortcuts > Services.
I don't recommend this option, because at least on my system, there is at best a delay of around half a second before an application is focused by a keyboard shortcut created using this method, and the delay is often even longer. There has also been a bug since 10.7 where the keyboard shortcuts for Automator services don't always work until you hover over the services menu from the menu bar. (Edit: it's now 8 years after I wrote this answer and the bug still hasn't been fixed.) Also, the keyboard shortcuts for services are not available in some applications like Emacs.