Detect if the Shift, Command, Control or the Option key has been pressed

In Excel for Windows you can use the GetKeyState API to check this but on a Mac it is not so easy. Code like this can be very useful to run the code you want depending of the key status of the Shift, Command, Control or Option.

Thanks to DJ Bazzie Wazzie from the MacScripter forum i was able to create a VBA example in 2014, but this one is not working anymore because it use Python and this is not installed anymore by default in the last MacOs versions. I publish another way now on this page that use a separate script file that you run with the new AppleScriptTask function. More work to setup but this you only have to do one time so this is not a big problem. Thanks again to my friends from the MacScripter forum that always share their knowledge.

Note: It is not very fast but this is not a big problem when you use it on a button on the worksheet or UserForm is my opinion.

Note: When you call the function from a userform button or button on a worksheet the Ctrl key test is not working, when you run the Ctrl test from the macro dialog it is working. Reason is that Ctrl + click is right click on a Mac so the Ctrl test is only working from the macro dialog.


Copy the Script file in the correct location (You must do this one time)


I create this install script for you to copy the script file in the correct location, you can download the Install.scpt script together with the KeyPressed.scpt file below. The folder must be in the Users Download folder, you see that if you download the compressed folder from my website it will automatically uncompressed in your Downloads folder and you can double-click the Install.scpt file in it to Open and Run it to copy the KeyPressed.scpt in the correct location so the AppleScriptTask function can find it.

You can download the Install.scpt and KeyPressed.scpt files below (File dates 23-Nov-24)
Download Files


The VBA macro that you can use now in your projects


You can use this VBA macro now in your projects, Add a button on your worksheet and attach this macro to it and you can test it, Suggestions are most welcome to make the script/code on this page better.
26/01/2025
Web design by Will Woodgate