Alt, CMD, Ctrl or Shift key pressed

Hi all

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

Thank you

Hi again Ron,

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.

Correct DJ

My website examples must be copy/Paste and Run, at least I try that.

So difficult to make code in Excel VBA that is working in Win and Mac and do the same

Thanks again

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.

Yvan KOENIG (VALLAURIS, France) vendredi 15 août 2014 19:36:58

Like the Ron told, he doesn’t want to make use of 3rd party tools. So neither of them are a solution.

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.

Yvan KOENIG (VALLAURIS, France) vendredi 15 août 2014 21:07:52

Hi Yvan

I want to make Mac Excel version of this Add-in and I not want that the user must install any other software to have it working.

So if the solution from Stefank (I never see this solution) can be in a string that I can call with the VBA Macscript function then I love to try it.

A script library embedded in a script (.scptd or .app) is nether third-party nor does it require separate installation.

Hi Shane

Good morning

In VBA we must create a Script string and call it with the VBA Macscript function, for example this simple script:

You see that it is a part of the VBA code

Love to try what you are talking about but like to see a script example from your solution or Stefan’s
Can you post it here or link to a page with this info.

Thanks for your reply

Can VBA run a script from a file rather than text? If so, you’d save the script as a file in temporary items and call it from there.

Hi Shane

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.

Thanks and have a nice weekend

I understand. But the user would still need just one file – the VBA script would create the temporary extra script as part of its execution.

Hi Shane

Can you point me to the script CheckModifiersKeys from Stefan or yours please

I think Stefan’s is a separate app. My script is this:

use framework “AppKit” – for NSEvent

on checkModifier:keyName
	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
		return false
	end if
	set theFlag to current application's NSEvent's modifierFlags() as integer
	if ((theFlag div theMask) mod 2) = 0 then
		return false
		return true
	end if
end checkModifier:

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.

Thanks Shane

I check it out this weekend/next week

No, it’s a command line tool

Apologies. Separate binary would have been better.

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
I already ran a script using this scheme : Lion DiskMaker.

Yvan KOENIG (VALLAURIS, France) dimanche 17 août 2014 14:42:19

Hi all

Will publish a VBA solution based on DJ’s script on my site.
It is working so it is better than nothing.

But I love to see scripts that are working,maybe I can use it ?