Benches macOS

3

Terminal Turn Hardware Keys On And Off

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I'm trying to script turning on and off the hardware keys on my MacBook that control the volume and brightness. I have a game which I like to assign the function keys to certain controls and going into System Preferences every time I want to play is a hassle.

If someone could tell me how to do this using the +defaults write+ command that would be most appreciated.

The preference is located in: +System Preferences > Keyboard & Mouse > Keyboard > Use all F1, F2, etc. keys as standard function keys+ and it is just a simple check box.

3 replies

Sign in or join Beachball Help to reply.

  1. 0

    Michael Conniff

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Matt

    You can do this with

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

    Undoing it should be obvious 😉

  2. 0

    Matt Skorina

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks Michael, that got me in the right direction but it didn't work.

    Before I wrote to
    com.apple.keyboard.fnState I read from it to see what was there.

    defaults read -g com.apple.keyboard.fnState

    When the checkbox was on in System Preferences (acted like function keys) the above resulted in
    1. When the checkbox was off (hardware keys) it resulted in
    0.

    When I ran +defaults write -g com.apple.keyboard.fnState true+ it didn't make the keys only work as function keys, in fact it didn't change anything. If I wrote a
    true or a
    false the keys didn't change from whatever they were set to before I ran that command.

    When I read back
    com.apple.keyboard.fnState whatever I had written to before was there. Writing
    false to that preference resulted in
    false being returned and the key type never changing.

    If I wrote a
    1 or a
    0 it didn't change how the keys behaved yet checking or unchecking the System Preferences checkbox (resulting in a
    1 or
    0 being read from the preference) would change the behavior of the keys.

    I even restarted (effectively killing all processes). That resulted in the sum total of nothing. I'm really stumped on this one. Googling "fnstate" resulted in a handful of of cases just like this that I hadn't discovered in my previous Googlings. None of them with a solution that worked for me.

    Is there some silly mistake that I'm making here or what?

  3. 0

    Michael Conniff

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Matt

    I did not have this property in my plist initially, so I backed up the file and changed it in the GUI. I then discovered this entry in "~/Library/Preferences/.GlobalPreferences.plist":

    <key>com.apple.keyboard.fnState</key>
    <false/>

    But
    defaults read -g com.apple.keyboard.fnState will return this as '0', or '1' if the value is <true/>.

    However, the command I posted omitted an important flag, to prevent whatever is input being treated as a string 😟 So try:

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

    Now the read command will display '1', but in the file the value will be '<true/>'.

    Sorry I missed that out before!