Has anyone come across any ways to determine the key up/down status of a particular key…either natively in AS or via an Obj-C ‘call method’ call?
I know that Jon’s Commands for X has the ‘keys down’ command (and it works), so I know that this functionality is possible. What I want to know, however, is how I can possibly do this without using Jon’s.
To explain (briefly) what I want to do: while in a loop, I want to check to see if the space bar is pressed…if it is pressed, the loop will exit, if it is not pressed, the loop will repeat.
Alternatively, is there similar functionality for testing the status of the mouse button?
AS Studio can capture the mouse button or a keystroke but only as part of an event. You can add a “mouse down” handler (which has an event attached to it) to a box or some other element in your script and have that set a flag that is checked at the beginning of the repeat loop. If the flag is true, continue, if false, exit the repeat. The downside of this is that the user must click on the box for this to work, it can’t be just any click. What I find to be a better alternative is to display a progress bar while in a repeat loop that that also has a button for canceling the repeat. You can see an example of this in a demo app I built:
That’s pretty kludgy but it will work. I’ve modified my app so that there is an offscreen button that has a keyboard equivalent of just “z” (having an equivalent of just space didn’t seem to work). So, while the repeat loop is doing it’s thing, holding down “z” will click the offscreen button and trip the stop_progress flag. The downside of this is that when the progress window is closed, the “z” keystroke has nowhere to go (i.e., it doesn’t click a button and there is no editable text field to receive it) so the app will beep. To remedy this, I added another offscreen button that also has a keyboard equivalent of just “z”. This button will then receive the keystroke but it won’t do anything. To see the offscreen buttons in IB, expand the width of the main window and the progress panel. You have to have these offscreen because they have to be both enabled and not hidden to receive the keystroke. The text that points to the buttons is hidden so you can see it in IB but not when the app is run.
Again, you can see this in action by seeing this project:
I can’t read the links provided by jon8 so I dunno if what I did is useless for you. I had a similar need, to test the status of a key but didn’t want an event, most AS event doesn’t includes the information anyway (at least with AppleScript Studio 1.2).
So I made a little Objective-C Library and few AS methods to encapsulate them. That works fine but I used the function GetCurrentKeyModifiers which is usefull only to test modifiers keys without to wait any event (it tests only the keys command, shift, option, control, caps lock).
You seems to need to test any key and I dunno if GetKeys will do the job and what problems it could add (my first tries was with it and I gave up when I found GetCurrentKeyModifiers).
If you want anyway the sample code Objective-c and AppleScript that use GetCurrentKeyModifiers, ask me here, I’ll post it here.
About Jon’s Commands for X, yes they work fine but only if you use a US keyboard. With non US keyboards as mine the key mapping is wrong.
I think it’s not an easy Objective-c job. You should first try an Objective-c forum then encapsulate yourself the resulting Objective-c library in AppleScript.
I have tried the suggestion for breaking out of a loop by creating a button that changes the status of a flag which is then checked in the repeat loop and have allocated the escape key. It works well but having broken out of the loop if you continue to press the esacpe key when you restart the loop by clicking the button that contains the loop script it takes two presses of the button before the loop restarts