2

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?
2

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.

0

Actually, if you use Karabiner Elements anyway, there's an easier way to achieve this:

Though it's quite a mixture of "worlds" (Karabiner/shell/GUI) its charm is that you do not need any other app(s) to get it "shortcut" ;-)

{ "parameters": {
          "basic.to_delayed_action_delay_milliseconds": 1000},
"from": {
          "key_code": "return_or_enter",
          "modifiers": {
                "mandatory": ["control", "shift" ] }
        },
 "to": [{"shell_command": "open -g /System/Library/¬ 
                           PreferencePanes/Keyboard.prefPane/"
                              }],
 "to_delayed_action": {
       "to_if_invoked": [                     ___
            {"key_code": "tab"},                 |   These key-codes
            {"key_code": "tab"},                 |   can be replaced
            {"key_code": "tab"},                 |   by a single osa-
            {"key_code": "tab"},                 |   script, more ele-
            {"key_code": "tab"},                 |   gantly, listed
            {"key_code": "spacebar"},            |   below !         *)
            {"key_code": "q",                    |   
             "modifiers": ["command"]},       ___| < (Easier to handle)
            {"key_code": "escape"}] },
 "type": "basic"
}
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

(* 'tell application \"System Events\" to tell process ¬
    \"System Preferences\"\n click checkbox 1 of tab group 1 ¬
    of window 1\n end tell\n quit application \"System Preferences\"'

As you can see, System Preferences Keyboard is opened by a shell-command, which demands "waiting time" for following key-codes (milliseconds: 1000).

I didn't find a keys-shortcut to get at the checkbox, so it navigates by 5 "tabs", one "spacebar" (= click), closes the prefpane and sets a final "escape" (without it my system reacts with a delay).
(I appended an extra, actually part of 1st answer's AppleScript, but adapted:\nfor line breaks, \"for special keys.)

I installed a "Ctrl-Shift-Enter" shortcut, you may use s.th. else …
("caps_lock" at EventViewer sends either key_down or key_up – press twice to change states!)

This code can be inserted into your karabiner.json manually, or imported as a "complex modification" needs a "title: > rules: > description: > manipulators:" -framework (recycle existing one).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .