Can you toggle function keys on / off with a keyboard shortcut on OSX?

Solution 1:

To make the following work, you need to Enable access for assistive devices in System Preferences » Universal Access.


Open Automator, select Service and choose that the service receives no input (near the top).

Double-click Run AppleScript in the Utilities category of the library. Replace the default code snippet of the newly created action with the following:

tell application "System Preferences"
    set current pane to pane id "com.apple.preference.keyboard"
    tell application "System Events"
        tell process "System Preferences"
            click checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
        end tell
    end tell
    quit
end tell

System Preferences will launch, but it will not be displayed and will quit immediately after toggling the setting.

Press Command-S to save, give it the name e.g. Toggle Fn. Result:

enter image description here


Go to System Preferences » Keyboard » Keyboard Shortcuts » Services to assign a keyboard shortcut for this Service.

Solution 2:

I know this post is old but on couldn't get the above to run on Mountinan Lion. I found a similar snippet but removed some unnecessary parts.

tell application "System Preferences"
    set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
    -- If we don't have UI Elements enabled, then nothing is really going to work.
    if UI elements enabled then
        tell application process "System Preferences"
            get properties

            click radio button "Keyboard" of tab group 1 of window "Keyboard"
            click checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
        end tell
        tell application "System Preferences" to quit
    else
        -- GUI scripting not enabled.  Display an alert
        tell application "System Preferences"
            activate
            set current pane to pane "com.apple.preference.universalaccess"
            display dialog "UI element scripting is not enabled. Please activate \"Enable access for assistive devices\""
        end tell
    end if
end tell

Hope this helps