Make Sure All Modifiers Are Released Before Continuing On

Is there a way to have applescript make sure all modifiers are release before moving on in a script? I am trying to run modifiers by triggering applescripts from QuicKeys or Quicksilver and it gets messed up of course by the modifiers I am holding down to trigger the script in the first place.

It seems like this should work if you add to the start of the script but it doesn’t.

tell application "System Events"
	key up {command, option, control, shift}
end tell

http://en.wikibooks.org/wiki/AppleScript_Programming/System_Events

Any suggestions?

I’ve found a few other people ask this on the web but haven’t found a solution if there is one other than the delay which I was already using.

http://stackoverflow.com/questions/6584357/why-applescript-always-send-keystrokes-with-command-down

http://apple.stackexchange.com/questions/36504/applescript-ignore-modifier-keys/89814#89814

Which is either makes it too slow or if too short messes things up again.

Browser: Safari 536.29.13
Operating System: Mac OS X (10.8)

Hello.

I just suggest right away, that this is close to unsolvable without having a delay long enough to let you manage to get your fingers off the keyboard, after you have triggered the script and , to let the Applescript figure out that the keys aren’t pressed, because I think any keyboard modifiers that are physically down will override the key up command.

That would make sense. And sorry for not bringing a positive answer. :frowning:

Actually, you could make an applet out of what itches you, and execute the applet by Spotlight, to not interfere with modifier keys on the keyboard. :slight_smile:

Another solution would be to execute the scripts by mouse from the script menu. Or assigning the script to a Function Key without modifier.

ASObjCRunner can check modifier key states.

http://www.macosxautomation.com/applescript/apps/runner.html

Hello.

Yes, you could loop/delay until the modifier key state is false!

And … getModKey by StefanK provides the same functionality, but it should be used in a shell script really, so you’d need only one do shell script. (That shell script is reusable from then onwards). It uses the Carbon Frameworks, so I am a bit unsure whether it works still on Mountain Lion.

This seems to be the case from all my tests. Even if I do key down then key up nothing seems to override the fact that you are still physically holding down the modifier from executing the script.

This might be a possibility. It was my hopes to pass this on to others and allow them to be able to assign whatever key they wanted. I pretty much have it working for me but if I can make it as failsafe as possible that would be nice.

This seems to be a great idea I’ll have to experiment with this and ASObjCRunner and see what seems to be the best solution. I am trying to not do extra installs since this will go on others computers but if that is more elegant and works better I’ll give that a go. Thank you both for the great ideas and help!

Hello.

That is greeat, it so happens that I can make a getModKeys for you, that uses Corefoundation and works from Leopard onwards. I could have it ready in 4 days as I am in the middle of something. That you could put into the Contents:Resources folder of a script bundle, easy to distribute.

The deal is, that you see to that it is tested on a computer with two keyboards connected to it.

But I hope you do try Stefan’s first! I see no need to do this, if his works.

I updated checkModifierKeys

Instead of Carbon it uses Quartz Event Services

It returns 0 if no modifier key is pressed.
You can check single keys by passing the key name(s) as argument(s) (shift, option, command, control, capslock)

Hello.

:slight_smile: That is great, then I don’t have to finish my IOServices version. ( I really have it working, I just haven’t assembled it into a user-executable.)

Thanks for the generous offer.

Thanks StephanK for the update link. I have googled “checkModifierKeys” and searched this forum but don’t see a place where you discuss how to use it. I see other references to it though, but where did this originally post?

The following script doesn’t seem to work, I am thinking I am needing an “if, than” statement but it seemed like from the dictionary and script example http://www.macosxautomation.com/applescript/apps/runner_vanilla.html that this would force modifier keys to be release and not check on their state.


tell application "ASObjC Runner"
	shift key down of modifier keys
	false
	control key down of modifier keys
	false
	option key down of modifier keys
	false
	command key down of modifier keys
	false
	
end tell

tell application "Finder"
	activate
	--Open a new finder window after all physical modifier keys have been made to release.
	tell application "System Events" to keystroke "n" using {command down}
end tell

It seems ideally if I could

I should go down that road since it requires no other installs other than the script. How do I do this or maybe it isn’t possible?

checkModifierKeys returns a non-zero value, if any modifier key is down, otherwise 0
Put the executable somewhere and adjust the path


set modifierkeysPressed to true
repeat while modifierkeysPressed
	set modifierkeysPressed to (do shell script "/path/to/checkModifierKeys") is not "0"
	delay 0.2
end repeat

I opened the web page which you pointed.

It contains :

[i]There is also a property called modifier keys, which you can use to find out the state of the various modifier keys. You can get its properties in a single call, or query one of the properties representing a particular key. For example:

tell application “ASObjC Runner”
caps lock key down of modifier keys
→ true
properties of modifier keys
→ {caps lock key down:true, shift key down:false, command key down:true, control key down:false, option key down:false, class:mod keys}
end tell[/i]

It seems clear that it reads the status of the modifiers but doesn’t change it.

More, in the application dictionary, we may read :

mod keys‚n : The modifier keys.
properties
caps lock key down (boolean, r/o) : Is the caps lock key down?
command key down (boolean, r/o) : Is the command key down?
control key down (boolean, r/o) : Is the control key down?
option key down (boolean, r/o) : Is the option key down?
shift key down (boolean, r/o) : Is the shift key down?
properties (record, r/o) : All the object’s properties returned as a record.

It seems that you didn’t understood that “r/o” means : “read only

KOENIG Yvan (VALLAURIS, France) mercredi 1 mai 2013 20:24:12

Hello.

Here is the previous manpage for checkModifierKeys I assume it still works that way, I’ll leave it for skillet to find out for now.

[code]checkModifierKeys(1) BSD General Commands Manual checkModifierKeys(1)

NAME
checkModifierKeys, – Modifier Key listener for the Mac Os X

SYNOPSIS
checkModifierKeys [shift | cmd | control | option | capslock]

DESCRIPTION
checkModifierKeys, Returns “1” to standard output if one of or more of
the modifierkeys in the argumentlist were held down under execution or
“0” if not.

AUTHOR
Stefan Klieme

Darwin May 1, 2013 Darwin[/code]

I changed the code slightly aside from the modern API.

without any argument it returns 0 if no key is down otherwise the (decimal) representation of the flag bit positions

capslock 0x00010000
shift 0x00020000
control 0x00040000
option 0x00080000
command 0x00100000

with an argument is masks just the particular bit(s)

This is great thanks! Working fabulously, it had me stumped for a little bit because it was saying my path did not exist.
/Users/boshoshua/Dropbox/Reference/Applications/Script\ Reference/checkModifierKeys

When I ran the executable from the folder it gave me this path so apparently Dropbox is doing some strange path modification I haven’t seen before.

/Users/boshoshua/Dropbox/Reference\ Files/Applications/Script\ Reference/checkModifierKeys

Thank you all for your help!

Yvan Koenig I did see the read only, I just misread that the example was showing it might return true and was thinking that the example was setting it to true. Thanks for clearing that up for me.

Hello Stephan

I tried to test checkModifierKeys on my 10.8.3 system (using French).

It behaves well when I test shift or capslock but for the three other modifiers it returns “0” even when the modifiers keys are depressed.

Am I making something wrong ?

KOENIG Yvan (VALLAURIS, France) jeudi 2 mai 2013 09:51:06

I tested it directly in Terminal.app, there it works except for the command key
which is probably a Terminal restriction

Running in AppleScript Editor/Script Debugger it might not work in a few cases when the modifier keys are caught for a function in the editor

Thanks.

So I was using it wrongly.

When I save the script as an application the modifiers are correctly reported.

KOENIG Yvan (VALLAURIS, France) jeudi 2 mai 2013 10:24:54

FWIW, I’ve adding an “await release of” command to ASObjC Runner. If anyone wants to test it, email me.

await release of‚v : Pause as long as these modifier keys are pressed.
await release of list of shift/Œoption/Œcommand/Œcontrol/Œcaps lock : A list of the modifier keys to wait for.
[flashing for real] : Maximum number of seconds for which the menu bar icon will flash while waiting. Default is 0.

The flashing is a great idea! :slight_smile:

By the way, I am wondering about something, would I do any thing harm, or make havoc, if I put your app into the contents of an applet? -I haven’t done it, but I wonder how that would go, if it was legal to do so, and if something would become malfunctioning if the user had registered and older version.

The second scenario.

And how would you feel about it if I just “brought AsObjC runner with me”, in the applet’s contents, and then copied it out into the applications folder, if it already wasn’t there? Maybe showing a dialog to the user, disclosing what is about to happen.

I’m wondering, as some approach like this, would make it easier for me to incorporate it into scripts/applets, should I need it.

Thank you.

It should be fine on all counts. If you look at the Web page, under “Embedding ASObjC Runner in applets”, you’ll see instructions for the best way to do it.

I can’t see any problem. It’s basically yours to do what you like with, other than pass off as your own or sell.