Enable/Disable Fn keys from the command line on the Mac

13,969

Solution 1

An AppleScript that should do the trick -- taken from http://scriptbuilders.net/files/fn1.1.html, with slight modifications

--Check if GUI Scripting is Enabled
tell application "System Events"
    if not UI elements enabled then
        set UI elements enabled to true
    end if
end tell

--Enable/Disable "Use all F1, F2, etc. keys as standard function keys" option in Keyboard & Mouse Preference pane and close System Preferences
tell application "System Events"
    tell application "System Preferences"
        reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
    end tell
    click checkbox 1 of tab group 1 of window 1 of application process "System Preferences"
end tell
if application "System Preferences" is running then
    tell application "System Preferences" to quit
end if

Tested on MacOS 10.6.4

Solution 2

The command is defaults write -g com.apple.keyboard.fnState, although I've had problems in the past changing it. I ended up just using an AppleScript. Give it a try.

defaults write -g com.apple.keyboard.fnState -boolean true

Edit
To elaborate, the problems I've had is that the actual value is changed, but it doesn't actively change the setting in System Preferences nor does the fnState toggle, because the file is only read at boot/login etc. Also, making changes to a config file that's opened by another task sounds like a good way to corrupt the file.

Solution 3

You can install the awsome Karabiner-Elements.

Under System Preferences-> Keyboard preferences, make sure "Use all F1, F2, etc. keys as standard function keys" is checked as a perquisites.

  • Open KeyRemap4MacBook preferences.
  • Navigate to "Pass Through Mode" option.
  • Check the 'Change Fn+Escape to toggle "Pass Through Mode"'
  • Open "Change F1..F19 Key" and check the "Macbook Pro" or "Macbook Air" option choosing your correct mac type.

Solution 4

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!

Share:
13,969
Randall
Author by

Randall

Updated on June 24, 2022

Comments

  • Randall
    Randall almost 2 years

    I hardly ever use the function keys on my macbook pro. I mostly just use them for volume, brightness, etc. Now that I've started playing Starcraft 2 a bunch, I want to use them without having to press the fn key down.

    I want to write a little shell script that will flip the "Use all F1, F2, etc keys as standard function keys" check box. I was thinking I could use the defaults command to change it but I wasn't sure what values to use. This way I don't have to change the the preferences every time I want to play. I can just run the script that'll switch the keys and even launch the game.

    Any ideas?

  • Daisy Sophia Hollman
    Daisy Sophia Hollman over 13 years
    This technically isn't a command line solution, since it uses AppleScript to access GUI elements, thus leaving the command line.
  • Ansel Halliburton
    Ansel Halliburton over 13 years
    if you really want to, you can run this on the command line using osascript << EOF [paste script here] EOF
  • Ethan Reesor
    Ethan Reesor about 12 years
    As far as I know, the defaults command isn't the same as simply changing the file. I get the impression that it is keyed into the operating system's defaults/preferences mechanism, which should notify any application that cares.
  • Lri
    Lri almost 11 years
    It's also possible to toggle settings with something like k=/Applications/KeyRemap4MacBook.app/Contents/Applications/K‌​eyRemap4MacBook_cli.‌​app/Contents/MacOS/K‌​eyRemap4MacBook_cli; s=remap.functional2fn_2008; $k changed | grep -q $s && $k disable $s || $k enable $s. See Tests/lib/string/data/checkbox.xml for the identifiers of the predefined settings.
  • Tango
    Tango over 10 years
    Just as ad added note, I just tested this on Mavericks with no functional result.
  • Tango
    Tango over 10 years
    Just as ad added note, I just tested this on Mavericks and received error messages. It will not work on Mavericks.
  • Ethan Z
    Ethan Z over 4 years
    The application link is dead. Do you have a mirror?
  • Gal Bracha
    Gal Bracha over 4 years
    @EthanZ Yes. Updated it to pqrs.org/osx/karabiner - they changed the name
  • Freewalker
    Freewalker about 2 years
    Wow, thanks. My macro (activate/deactivate F key use when I switch to/from VS Code) broke on one of the OS upgrade, but works again now thanks to you. For my purposes I need to only activate (not toggle), so I want no action if it's already on. So I added code set fnCheckbox to checkbox "Use F1... then tell fnCheckbox if (its value as boolean) then