This AppleScript allows you to automate the toggling of using Function Keys vs Media Keys on MacOS. Useful for VS Code debugging other applications. Then, switching back to your media keys.
Essentially it does the job one would have to do manually: Open System Preferences, go to Keyboard, click "Use F1, F2 etc. keys as standard function keys".
Tested with:
Toggle Function Keys.scpt
Toggle Function Keys
. Running a second time will ask you to Allow access to control System Events - press Allow (which adds an extra permission in Settings > Privacy & Security > Automation. If this step does not appear & the script fails to run, then go back to step 3 and ensure you choose: File, Export - but this time save to the Desktop. Then follow step 5 & 6 then drag from the Desktop to Applications)For me, the easiest method I've found is to use cmd
+ space
to search for "Toggle Function Keys" and run it from there. Or drag the application to the dock and click to trigger it.
Other people have had success binding to a keyboard shortcut with a paid app. e.g. here.
In a tip given from @SoftTransistor you could add the script to your login items to trigger switching keys each time you start up.
Other ideas I've not had success with:
This script was adapted from other, non-working older scripts I found on the internet. Many don't seem to wait for the UI to appear, so I added that to the script. Also had the wrong UI text reference (to be fair, Apple change this between the M1 mac and old MacOS versions). Old scripts used to provide feedback via a pop up but I've removed this as this interrupts the user-flow when used a lot.
if osver > 13.0 then
section goes to mflboys for creating MacOS Ventura code sourcereveal anchor
.