22

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?

Randall
  • 14,691
  • 7
  • 40
  • 60
  • This question may be very old, but it's still pretty high in the search rankings. For anyone finding it now, there's an application called Palua (http://www.molowa.com/mac-os-x/palua/palua-3-0/) which flips the state for you, including intelligently on application switch. There's also FunctionFlip (http://kevingessner.com/software/functionflip/) which permanently flips just a subset of the keys. – spikeheap Feb 27 '14 at 08:49

5 Answers5

8

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

user123444555621
  • 148,182
  • 27
  • 114
  • 126
5

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.

Robert
  • 21,110
  • 9
  • 55
  • 65
  • 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. – Ethan Reesor May 19 '12 at 19:40
  • 2
    Just as ad added note, I just tested this on Mavericks with no functional result. – Tango Jan 10 '14 at 05:19
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.
Gal Bracha
  • 19,004
  • 11
  • 72
  • 86
  • It's also possible to toggle settings with something like `k=/Applications/KeyRemap4MacBook.app/Contents/Applications/KeyRemap4MacBook_cli.app/Contents/MacOS/KeyRemap4MacBook_cli; s=remap.functional2fn_2008; $k changed | grep -q $s && $k disable $s || $k enable $s`. See [Tests/lib/string/data/checkbox.xml](https://raw.github.com/tekezo/KeyRemap4MacBook/master/Tests/lib/string/data/checkbox.xml) for the identifiers of the predefined settings. – Lri Jul 03 '13 at 14:53
  • The application link is dead. Do you have a mirror? – Ethan Z Dec 02 '19 at 15:50
  • @EthanZ Yes. Updated it to https://pqrs.org/osx/karabiner/ - they changed the name – Gal Bracha Dec 29 '19 at 23:32
3

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 C
  • 419
  • 3
  • 12
  • 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` – Freewalker Apr 27 '22 at 23:49
  • For folks with MacOS Ventura, see the repo link in this answer - I've updated the code & tested it works on a MacBook Air M2. – Simon C Dec 23 '22 at 19:43
0

Thanks to @simonc's answer, easily using it with raycast.

If anybody wants to have the functionality to be run through terminal:

  1. save below script as an sh file, eg: toggle-fn.sh

    osascript <<EOD
    
    -- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
    -- Tested on MacOS Monterey (12.6.2) July 2023, MacOS Ventura (13.4.1) July 2023, MacOS Sonoma Preview (14.0) July 2023
    -- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys
    
    set osver to system version of (system info)
    
    if osver ≥ 13.0 then
        open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"
    
        tell application "System Events" to tell process "System Settings"
            # example window title: "Keyboard – 86%", so "begins with" is needed
            repeat until window begins with "Keyboard" exists
            end repeat
    
            # wait until Keyboard window is the main window of the application and is accessible
            repeat until exists of (1st window whose value of attribute "AXMain" is true)
            end repeat
    
            # wait until the group is displayed (needed else fails on Apple M2 Pro)
            repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
            end repeat
    
            # "Keyboard Shortcuts..." Button
            set keyboardButton to 1
            # in MacOS 14 Sonoma, button 1 & 2 are assigned to keyboard brightness
            if osver ≥ 14.0 then
                set keyboardButton to 3
            end if
            click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
    
            repeat until sheet 1 of window 1 exists
            end repeat
    
            # Click Function Keys
            keystroke "f"
    
            repeat until checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1 exists
            end repeat
    
            click checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
    
            # "Done" Button - Close the sheet so the application can quit
            click button 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
    
            # Attempting to check the sheet at a certain point while closing will throw an error
            # In that case, the outer repeat will try again
            repeat
                try
                    repeat while sheet 1 of window 1 exists
                    end repeat
                    exit repeat
                end try
            end repeat
        end tell
    
        tell application "System Settings" to quit
    else if osver < 13.0 then
        -- Below for MacOS Monterey and below
        tell application "System Settings"
            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"
                    try
                        click checkbox "Use F1, F2, etc. keys as standard function keys on external keyboards" of tab group 1 of window "Keyboard"
                    end try
                    try
                        click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
                    end try
                end tell
                tell application "System Settings" to quit
            else
                -- GUI scripting not enabled.  Display an alert
                tell application "System Settings"
                    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
    end if
    
    EOD
    
  2. run on terminal using sh toggle-fn.sh

Or if anyone is using raycast and want to have this functionality through its command functionality:

  1. save below script as an sh file, eg: toggle-fn.sh
    #!/bin/bash

    # Required parameters:
    # @raycast.schemaVersion 1
    # @raycast.title Toggle Fn
    # @raycast.mode silent

    # Optional parameters:
    # @raycast.icon 

    # Documentation:
    # @raycast.description Toggle Fn Keys
    # @raycast.author smthng
    # @raycast.authorURL https://github.com/smthng
    # @raycast.packageName System

    osascript <<EOD
  
    -- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
    -- Tested on MacOS Monterey (12.6.2) July 2023, MacOS Ventura (13.4.1) July 2023, MacOS Sonoma Preview (14.0) July 2023
    -- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys
    
    set osver to system version of (system info)
    
    if osver ≥ 13.0 then
        open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"
        
        tell application "System Events" to tell process "System Settings"
            # example window title: "Keyboard – 86%", so "begins with" is needed
            repeat until window begins with "Keyboard" exists
            end repeat
            
            # wait until Keyboard window is the main window of the application and is accessible
            repeat until exists of (1st window whose value of attribute "AXMain" is true)
            end repeat
            
            # wait until the group is displayed (needed else fails on Apple M2 Pro)
            repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
            end repeat
            
            # "Keyboard Shortcuts..." Button
            set keyboardButton to 1
            # in MacOS 14 Sonoma, button 1 & 2 are assigned to keyboard brightness
            if osver ≥ 14.0 then
                set keyboardButton to 3
            end if
            click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
            
            repeat until sheet 1 of window 1 exists
            end repeat
            
            # Click Function Keys
            keystroke "f"
            
            repeat until checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1 exists
            end repeat
            
            click checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
            
            # "Done" Button - Close the sheet so the application can quit
            click button 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
            
            # Attempting to check the sheet at a certain point while closing will throw an error
            # In that case, the outer repeat will try again
            repeat
                try
                    repeat while sheet 1 of window 1 exists
                    end repeat
                    exit repeat
                end try
            end repeat
        end tell
        
        tell application "System Settings" to quit
    else if osver < 13.0 then
        -- Below for MacOS Monterey and below
        tell application "System Settings"
            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"
                    try
                        click checkbox "Use F1, F2, etc. keys as standard function keys on external keyboards" of tab group 1 of window "Keyboard"
                    end try
                    try
                        click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
                    end try
                end tell
                tell application "System Settings" to quit
            else
                -- GUI scripting not enabled.  Display an alert
                tell application "System Settings"
                    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
    end if

    EOD
  1. import it into raycast as a script command
  2. run it using the name "toggle fn" on raycast

note: for raycast to be able to run the shell command that requires accessibility permissions, you have to first give access to raycast on accessibility through system settings > privacy and security > accessibility

Beytan Kurt
  • 2,203
  • 4
  • 34
  • 57