I got some great help from DJ Bazzie Wazzie to get some script to know when a key is pressed but as always i want more
return (do shell script “/usr/bin/python -c ‘import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSControlKeyMask > 1’”) as boolean
Other keys are
Case 1: KeyConstant = “NSShiftKeyMask” 'Shift key
Case 2: KeyConstant = “NSControlKeyMask” 'Ctrl key
Case 3: KeyConstant = “NSAlternateKeyMask” 'Alt or Option key
Case 4: KeyConstant = “NSCommandKeyMask” 'Command or CMD key
I call this script now with VBA and it is working OK to call a different macro when I click on a button in a date picker user form with for example the Alt key pressed the only problem is that it is not very fast
Is there script that can give this information faster ?
If not I am already happy with this but in Windows a API is much faster
Great that my solution worked out for you. The performance issue is that it runs python code from a do shell script with comes with some latency, depending on you machine’s performance of course. The only solution, someone correct me if I’m wrong, is that you use/create a scripting addition that can tell you which modifier key is pressed or using AppleScriptObjC in a library. If I remember correctly there is a scripting addition named jon’s commands or something but it requires that the machine running the code needs this scripting addition is installed. I thought, based on your website which can run code on a mac out of the box, you’re not looking for any kind of that solutions which requires additional installations of 3rd party software.
However I have never tested if VBA can use the use statement (loading an AppleScriptObjC library) inside a MacScript function. If that is possible in VBA you should definitely try using an AppleScriptObjC script library. But again, based on your tutorial website, I think it is a too complex solution. I have given you a python solution to make use of the PyObjC bridge which can be used in plain AppleScript using a do shell script which ships with every version of Mac OS X.
But if anyone has a better plain AppleScript solution to lookup which modifier key is pressed, I’m very curious as well.
CheckModifiersKeys by StefanK does exactly what is required.
The ASObjC Runner application’s property modifier keys does the same.
My memory fails, maybe Shane Stanley, the author of ASObjC Runner delivered a Mavericks library offering the feature too.
I apologize but I don’t understand where would be the problem.
As far as I know, the three proposed options may be embedded in the applescript file as far as this one is a package which is a standard format.
I’m reluctant to do that when I post a script in a forum to help somebody but when the script is supposed to be delivered as a tool ready for use, what is stored in the package is not a problem.
I never try that but it is not really what I want. Will be great if it is possible and can be used then but we are not used at add-ins that have more then one file needed. I see if I can get it working to call a script file and get the answer back in the VBA from this script so my code can use the answer.
I think Stefan’s is a separate app. My script is this:
use framework “AppKit” – for NSEvent
if keyName = "option" then
set theMask to current application's NSAlternateKeyMask as integer
else if keyName = "control" then
set theMask to current application's NSControlKeyMask as integer
else if keyName = "command" then
set theMask to current application's NSCommandKeyMask as integer
else if keyName = "shift" then
set theMask to current application's NSShiftKeyMask as integer
set theFlag to current application's NSEvent's modifierFlags() as integer
if ((theFlag div theMask) mod 2) = 0 then
But you’d have to save the main script as a .scptd, and then save this in a subfolder. So it is a bit complicated to set up.
On reflection, the idea I suggested isn’t going to work. Saving as a temporary .scpt and inserting the library file sounds fine, but there’s a not-so-subtle problem: you can’t compile a script that uses a library without the library existing in one of the library paths at compile time.
You could try saving the library to the user’s Script Libraries folder, but that’s probably in breach of at least the spirit of your self-imposed restrictions.
It seems that the command line file may be embedded in the script’s package. Am I wrong ?
If this scheme is wrong there is always the ability to embed ASObjC Runner.app.
I already ran a script using this scheme : Lion DiskMaker.