Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 529
Randall Avatar asked Aug 10 '10 04:08

Randall


People also ask

How do I turn off fn key on Mac?

If you're using a Mac, click the Apple menu, select System Preferences, and then click Keyboard. Check the box next to "Use F1, F2, etc keys as standard function keys," and then close the window.

How do I enable the fn key on a Mac?

On your Mac, choose Apple menu > System Preferences, click Keyboard , then click Keyboard. Select “Use F1, F2, etc. keys as standard function keys” or “Use F1, F2, etc. keys as standard function keys on external keyboards” (depending on your Mac model).

How do I turn off Fn command?

To enable FN Lock on the All in One Media Keyboard, press the FN key, and the Caps Lock key at the same time. To disable FN Lock, press the FN key, and the Caps Lock key at the same time again.


2 Answers

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

like image 185
user123444555621 Avatar answered Oct 09 '22 06:10

user123444555621


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.

like image 38
Robert Avatar answered Oct 09 '22 06:10

Robert