MacOS – Use caps-lock to lock fn key or other way to lock fn key

applescriptcaps-lockkeybindingskeyboardmacos

What I want to achieve is simple:

  • caps lock on = fn locked (use function keys).
  • caps lock off = fn unlock (use media keys).

I found a couple of topics around this problem, but their solutions didn't help me:

  • Checking / unchecking the option "Use fn keys as standard" in the keyboard preference panel is what I want to achieve when pressing /unpressing the caps lock key.
  • Others solutions, like the Functionflip program or remapping keyboard tools like Karabiner don't seem to be proposals to resolve my problem.

Do you know programs or solutions that can achieve this?

If not:

  • Can I assign a program to the caps lock key? How?
  • Can I toggle the option in the Keyboard preference pane with a script?

Best Answer

To address the last point in your post, "Can I toggle the option in the Keyboard preference pane with a script?", here is the AppleScript code that will toggle the state of the "Use all F1, F2, etc. keys as standard function keys" checkbox on the Keyboard tab of the Keyboard System Preferences.

tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.keyboard"
    reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"
end tell

tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of tab group 1 of window 1
end tell

quit application "System Preferences"

Note: This code was tested under OS X 10.8.5. It may need to be adjusted for later versions of OS X.

You could use this code in an AppleScript application that could be placed in the Dock or in an Automator Service Workflow that could be assigned a Keyboard Shortcut, etc. If you need help with that, just ask.