MrSimonC / Toggle-Mac-Function-Keys

This AppleScript allows you to automate the toggling of using Function Keys vs Media Keys on MacOS
MIT License
81 stars 13 forks source link

Toggle Mac Function Keys

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:

Instructions for use

  1. On your mac, open Script Editor
  2. Copy / paste the contents of Toggle Function Keys.scpt
  3. File, Export (note: do not use Save As, which won't ask for permissions properly)
  4. Save the script as an application:
    1. Choose filename (e.g. "Toggle Function Keys")
    2. Save to Applications Folder
    3. File format: Application
    4. Code Sign: Sign to Run Locally
  5. Double click from the applications folder
  6. Enable the script to be run by going into System Preferences, Security & Privacy, [unlocking the dialogue box on older MacOS versions], then selecting (usually in Accessibility) 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)

Running

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.

At Startup

In a tip given from @SoftTransistor you could add the script to your login items to trigger switching keys each time you start up.

mac startup

Other ideas for running the app

Other ideas I've not had success with:

Background

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.

Credits