7

I know you can toggle this option on and off in the System Preferences screen for 'keyboard' but I'd like to know if I can do it via a shortcut as I do it all the time.

9

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.

8
  • 1
    If System Preferences is already running when you perform this command, it will quit. The code to prevent this would have bloated this answer. – Daniel Beck Feb 9 '11 at 18:39
  • The question is tagged osx-snow-leopard, which this answer seems to require. I don't think I can make a service in OS X 10.5 judging from what I can see in front of me compared to gigaom.com/apple/… – Philip Durbin Feb 14 '11 at 1:53
  • @Philip I don't recall how Services worked in 10.5, but an Automator-created service is just an Automator workflow saved to ~/Library/Services. Maybe you can replicate this manually? To configure the Services menu, use ServiceScrubber. – Daniel Beck Feb 14 '11 at 7:16
  • this works on 10.6 but for some reason my keyboard shortcut kept failing or firing when I didn't want it to. so i switched to calling it from the command line with osascript based on stackoverflow.com/questions/3446128/… – Philip Durbin May 11 '11 at 13:48
  • @Philip You can always enable the AppleScript menu and store the code as scpt file in ~/Library/Scripts and call from the menu bar. Using the 3rd party FastScripts, you can assign a keyboard shortcut. – Daniel Beck May 11 '11 at 21:06
4

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

2

For anyone else trying to make this work - I've finally gotten my solution to work. Tested with: MacOS Big Sur, 11.4, June 2021.

The code is based here: https://github.com/MrSimonC/Toggle-Mac-Function-Keys

but for brevity, here is the contents of the apple script file:

-- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
-- Tested on MacOS Big Sur (11.4) June 2021
-- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys

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

tell application "System Events"
    if UI elements enabled then
        tell application process "System Preferences"
            repeat until exists tab group 1 of window "Keyboard"
                delay 0.5
            end repeat
            click radio button "Keyboard" of tab group 1 of window "Keyboard"
            click checkbox "Use 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.security"
            display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
        end tell
    end if
end tell

Hope someone finds it useful! Simon.

New contributor
Simon Crouch is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • It seems the checkbox is dependent on the text (who on earth designed this?), so on my Mac M1 with Big Sur I had to add "on external keyboards", to get the script to run without errors. – Johannes Maria Frank 21 hours ago
0

You could also use a private.xml like this with KeyRemap4MacBook:

<?xml version="1.0"?>
<root>
  <item>
    <name>test</name>
    <identifier>test</identifier>
    <autogen>__KeyToKey__ KeyCode::F1, VK_COMMAND | ModifierFlag::NONE, KeyCode::VK_CONFIG_TOGGLE_fn</autogen>
    <autogen>__KeyToKey__ KeyCode::BRIGHTNESS_DOWN, VK_COMMAND | ModifierFlag::NONE, KeyCode::VK_CONFIG_TOGGLE_fn</autogen>
  </item>
  <item>
    <name>fn</name>
    <identifier vk_config="true">fn</identifier>
    <autogen>__KeyToKey__ KeyCode::BRIGHTNESS_DOWN, KeyCode::F1</autogen>
    <autogen>__KeyToKey__ KeyCode::BRIGHTNESS_UP, KeyCode::F2</autogen>
    <autogen>__KeyToKey__ KeyCode::EXPOSE_ALL, KeyCode::F3</autogen>
    <autogen>__KeyToKey__ KeyCode::LAUNCHPAD, KeyCode::F4</autogen>
    <autogen>__ConsumerToKey__ ConsumerKeyCode::KEYBOARDLIGHT_LOW, KeyCode::F5</autogen>
    <autogen>__ConsumerToKey__ ConsumerKeyCode::KEYBOARDLIGHT_HIGH, KeyCode::F6</autogen>
    <autogen>__ConsumerToKey__ ConsumerKeyCode::MUSIC_PREV, KeyCode::F7</autogen>
    <autogen>__ConsumerToKey__ ConsumerKeyCode::MUSIC_PLAY, KeyCode::F8</autogen>
    <autogen>__ConsumerToKey__ ConsumerKeyCode::MUSIC_NEXT, KeyCode::F9</autogen>
    <autogen>__ConsumerToKey__ ConsumerKeyCode::VOLUME_MUTE, KeyCode::F10</autogen>
    <autogen>__ConsumerToKey__ ConsumerKeyCode::VOLUME_DOWN, KeyCode::F11</autogen>
    <autogen>__ConsumerToKey__ ConsumerKeyCode::VOLUME_UP, KeyCode::F12</autogen>
    <autogen>__KeyToKey__ KeyCode::F1, KeyCode::BRIGHTNESS_DOWN</autogen>
    <autogen>__KeyToKey__ KeyCode::F2, KeyCode::BRIGHTNESS_UP</autogen>
    <autogen>__KeyToKey__ KeyCode::F3, KeyCode::EXPOSE_ALL</autogen>
    <autogen>__KeyToKey__ KeyCode::F4, KeyCode::LAUNCHPAD</autogen>
    <autogen>__ConsumerToKey__ KeyCode::F5, ConsumerKeyCode::KEYBOARDLIGHT_LOW</autogen>
    <autogen>__ConsumerToKey__ KeyCode::F6, ConsumerKeyCode::KEYBOARDLIGHT_HIGH</autogen>
    <autogen>__ConsumerToKey__ KeyCode::F7, ConsumerKeyCode::MUSIC_PREV</autogen>
    <autogen>__ConsumerToKey__ KeyCode::F8, ConsumerKeyCode::MUSIC_PLAY</autogen>
    <autogen>__ConsumerToKey__ KeyCode::F9, ConsumerKeyCode::MUSIC_NEXT</autogen>
    <autogen>__ConsumerToKey__ KeyCode::F10, ConsumerKeyCode::VOLUME_MUTE</autogen>
    <autogen>__ConsumerToKey__ KeyCode::F11, ConsumerKeyCode::VOLUME_DOWN</autogen>
    <autogen>__ConsumerToKey__ KeyCode::F12, ConsumerKeyCode::VOLUME_UP</autogen>
  </item>
</root>

See the source for the key code values and predefined settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.