Detect if the Shift, Ctrl, Alt or CMD 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.

Thanks to DJ Bazzie Wazzie from the script forum i was able to create this VBA example. Note: it is not very fast.You can run the macro named TestKey below to test it, do not forget to copy the function KeyPressedCheck inside the same module because it is called by the macro. In the function call in the macro you see that I use argument 1 for testing the Shift key now, see the comments in the macro for using argument 2, 3 or 4.

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.


***Not working anymore, will check this out soon****

11/11/2024
Web design by Will Woodgate