Some time ago, I explained how I setup “Global hotkeys for macOS with skhd”. I mentioned running it automatically upon login via the macOS Launch Daemon but did not detail the steps. So here they are...
shkd setup
First, compile skhd
as in my previous post. TL;DR:
- download
- extract
make
Then, create a ~/.skhdrc
configuration file and define your hotkeys. I complicated mine in the previous post by re-mapping keys via hidutil
... but here is a more basic setup for my old A1314 bluetooth keyboard:
- The first few are simple: A keystroke that runs a command, in this case
open
,osascript
andsay
. - The next few are more interesting: If the active window matches, e.g.
"finder"
or"safari"
then do replace that keystroke with another viaskhd -k
, otherwise*
just let the keystroke through~
as-is. - There two interesting ones takes this a step further: If the active window is
"firefox"
, then trigger in-built screenshot feature, otherwise*
, run the macOSscreencapture
command line.
cmd+alt+ctrl-v : open /Applications/VSCodium.app
cmd+alt+ctrl-f : open /Applications/Firefox.app
cmd+alt+ctrl-t : osascript -e 'tell application "Terminal" to activate'
cmd+alt+ctrl-n : say -vSamantha $(date "+It is %-I:%M")
cmd+alt+ctrl-d : ~/Programs/skhd -k "cmd+alt-d"
cmd-left [
"finder" : ~/Programs/skhd -k "cmd-0x21"
* ~
]
cmd-right [
"finder" : ~/Programs/skhd -k "cmd-0x1E"
* ~
]
f5 [
"finder" : ~/Programs/skhd -k "cmd+shift-0x2F"
"safari" : ~/Programs/skhd -k "cmd-r"
* ~
]
f6 [
"firefox" : ~/Programs/skhd -k "cmd+shift-s"
* : screencapture -coW
]
cmd-f5 : osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to not dark mode'
cmd-f6 [
"firefox" : ~/Programs/skhd -k "cmd+shift-s"
* : screencapture -oWx ~/Desktop/scr."$(date '+%F.%H-%M-%S')".png
]
Launch agent configuration
Now, configure a the macOS Launch Daemon (launchd
) to auto start skhd
upon login. You can read more on Apple’s ”Creating Launch Daemons and Agents” documentation.
Download and save the sample launchd job properly list file as ~/Library/LaunchAgents/com.koekeishiya.skhd.plist
. At minimum it needs something like this (of course you have to change your path):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.koekeishiya.skhd</string>
<key>ProgramArguments</key>
<array>
<string>/Users/Programs/skhd</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Register launch agent
Finally, run launchctl
to load skhd
as a service:
launchctl load ~/Library/LaunchAgents/com.koekeishiya.skhd.plist
If you get an error Load failed: 5: Input/output error
then unload
and then load
again... perhaps a few times until all security items fixed...
launchctl unload ~/Library/LaunchAgents/com.koekeishiya.skhd.plist
A note regarding system-level permissions:
- The Launch Agent needs the “Allow in the Background”.
- At minimum,
skhd
needs Accessibility permissions to “control your computer”. On first run, you will be promoted to allow, via Privacy & Security: - In addition, more access will need to be granted depending on each of your hotkey functions. For example,
- Screen Recording is required to take screenshots, and
- Automation is required to use System Events via Apple Script.
- Note that each new complied version will need to be
unloaded
and every Privacy & Security entry deleted, before re-load
-ing and re-approving each access request.