Featured image of post Switching Keyboard Layouts with Caps Lock on macOS

Switching Keyboard Layouts with Caps Lock on macOS

Use Caps Lock to switch keyboard layouts and its LED as a layout indicator

Obviously, if English isn’t your first language, you end up switching keyboard layouts a lot. On just about every system except macOS, that usually means using a keyboard shortcut. I think Caps Lock is the easiest key to reach. On Linux, all you need is a setting like grp:caps_toggle. Macs have a dedicated Globe key (๐ŸŒ/Fn). Good idea, but I still have to stretch my pinky to reach it. It sometimes misses presses, too, because of delays in how special keys are handled. Caps Lock also has an LED that can show you which layout is active. That’s what Linux does by default when you set Caps Lock to switch layouts.

Of course, Apple has already decided for you, so there’s no easy fix. Time to dig out the duct tape. First up, let’s get Caps Lock to switch layouts.

macOS has a built-in setting to switch layouts with Caps Lock. There’s a catch, though. It handles special keys in a weird way, so if you start typing right after pressing Caps Lock, the layout just doesn’t switch. Maybe the system thinks it was an accidental tap or is waiting for a key combination. As usual, macOS thinks it knows better than you. I tried getting rid of the delay with hidutil property --set '{"CapsLockDelayOverride":0}'. No luck.

So I went the long way around and remapped Caps Lock to F18 (or another function key). I started out with Karabiner-Elements, but hidutil itself turned out to be a cleaner way to do it, with no extra software. macOS then sees a regular keypress, and the weird delay goes away.

Remap Caps Lock to F18

Here’s the command.

1
2
3
4
5
6
hidutil property --set '{
  "UserKeyMapping": [{
    "HIDKeyboardModifierMappingSrc": 0x700000039,
    "HIDKeyboardModifierMappingDst": 0x70000006D
  }]
}'

Unfortunately, the remap only lasts until you reboot, so let’s have launchd apply it.

Create the agent file at ~/Library/LaunchAgents/com.local.caps-to-f18.plist.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?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.local.caps-to-f18</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/hidutil</string>
        <string>property</string>
        <string>--set</string>
        <string>{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x70000006D}]}</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Now load the agent into the current session.

1
2
plutil -lint "$HOME/Library/LaunchAgents/com.local.caps-to-f18.plist"
launchctl bootstrap "gui/$(id -u)" "$HOME/Library/LaunchAgents/com.local.caps-to-f18.plist"

Set F18 to switch layouts

Now let’s tell macOS to use F18 to switch layouts.

  1. In System Settings โ†’ Keyboard โ†’ Text Input โ†’ Edit, turn off the built-in Caps Lock layout switch if it was enabled.
  2. In System Settings โ†’ Keyboard โ†’ Keyboard Shortcuts โ†’ Input Sources, enable Select next source in Input menu. Click the shortcut on the right and press Caps Lock. It should register as F18.

F18 assigned to switch keyboard layouts

Use the LED to show the active layout

Install and launch Hammerspoon with Lua support by following the setup instructions. Add this snippet to ~/.hammerspoon/init.lua and choose Reload Config from the Hammerspoon menu. The LED stays off for the U.S. layout and lights up for every other input source.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
local function updateCapsLED()
    local source = hs.keycodes.currentSourceID()
    if not source or source == "" then
        return
    end

    local ledOn = source ~= "com.apple.keylayout.US"

    local ok = hs.hid.led.set("caps", ledOn)

    print(string.format(
        "[caps-led] source=%s layout=%s led=%s ok=%s",
        source,
        hs.keycodes.currentLayout() or "?",
        ledOn and "ON" or "OFF",
        tostring(ok)
    ))
end

capsLEDWatcher = hs.distributednotifications.new(
    updateCapsLED,
    "com.apple.Carbon.TISNotifySelectedKeyboardInputSourceChanged"
)
capsLEDWatcher:start()

updateCapsLED()

Hammerspoon log showing the Caps Lock LED state

Licensed under CC BY-NC-SA 4.0
Last updated on Dec 05, 2026 17:27 MSK
Built with Hugo
Theme Stack designed by Jimmy