Can you toggle function keys on / off with a keyboard shortcut on OSX?
Solution 1
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:
Go to System Preferences » Keyboard » Keyboard Shortcuts » Services to assign a keyboard shortcut for this Service.
Solution 2
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
Solution 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.
Related videos on Youtube
Ben Waine
Updated on September 17, 2022Comments
-
Ben Waine almost 2 years
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.
-
HikeMike over 13 yearsIf System Preferences is already running when you perform this command, it will quit. The code to prevent this would have bloated this answer.
-
Philip Durbin over 13 yearsThe 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/…
-
HikeMike over 13 years@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. -
Philip Durbin about 13 yearsthis 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/…
-
HikeMike about 13 years@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. -
Ben Waine over 11 yearsTotally forgotten I'd written this question. Regoogled and found your solution. Perfect thanks!
-
Kyle almost 11 yearsI use a combination of shell script and AppleScript and toggle the state via Keyboard Maestro. This way I can also have per app based rules, c.f. in the Adobe Suite. See: rocketink.net/2013/06/toggle-function-keys.html
-
Madivad almost 8 yearsI use Controllermate and used this script in combination with the EJECT button (I have the DVD drive removed) and it has worked perfectly in ElCap! Great stuff, thanks!
-
Johannes Maria Frank about 3 yearsIt 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.