Very Stupid Newbie Q: how to tell if a key is pressed

This is really an idiotic question . . . but I’m an idiot . . .
(falling head-first down mountains will to that to some people)

I have finally been able to upgrade to OS X 10.4.8 from 10.3.9 and one “challenge” for me is that the very convenient scripting addition that I’ve used for years: Jon’s Commands 3.0d3 no longer works . . . and it is everywhere in my scripts, especially when I want to trap for keyboard activity. For example:


tell app "yourAppHere"
  if keys pressed = {"Caps Lock"} then
    say "Login script cancelled."
  else
    say "What? Behind the rabbit?" -- I may be suffering from severe brain damage, but I can still quote Monty Python!
  end if
end tell

I dutifully put the OSAX in the /Library?ScriptingAdditions/ folder, but it doesn’t run anymore. Please tell me how to trap for pressed keys! I have gone through all the dictionaries I have and can tell the script to press keys in many ways, but I can’t figure out how to tell if a key–and which one(s)–is currently pressed.

Thank you and sorry for such a silly question!

Does your machine have a PowerPC or Intel processor? The Jon’s Commands osax is only compiled for PowerPC.

"Bring us a shrubbery " :wink:

I don’t know man I put the OSAX in /library/scripting additons
then rebooted, I 'm having no luck with any of jon’s commands I would try putting it in the system/library/scripting addtions but I don’t have write access to that area but if you do may you can try that ?

mm

Bruce, I just saw your post as I was reviewing my message and I am running an “intelimac” that explains my problem

Ah, thank you, Bruce, that explains it.

I was gifted a new MBP with an Intel chip. It is an amazing machine . . . but I guess many of my scripts now need to be reworked.

(I would have preferred to have gotten the ability to take a shower, but the MBP is still appreciated!)

Is there any way–on an Intel chip–to do the same thing: to trap for pressed keys?

Thank you!

I think Jon’s Commands works fine if you invoke it from a PPC environment. For example, save the following as ppc-carbon app (choose save as “application” in the Script Editor):

«event JonstikC»

display dialog result

The double-click it in the Finder.

On Intel, use Extra Suites:


tell application "Extra Suites" to set kd to ES keys down

Thank you, all!

I am having trouble using Extra Suites to read a key press, as suggested in this forum. Based on your suggestions, I have tried to use the following script:

tell application “Extra Suites” to set kd to ES keys down
repeat 10 times
delay 1
if kd contains “command” then
exit repeat
else
say “nothing pressed”
end if
end repeat

It compiles and runs, but when I hold down the command key, it is not exiting the repeat text. Also does not work with option.

What am I doing wrong? Is the usage different than with Jon’s keys pressed?

Model: Intel Powerbook
Browser: Firefox 2.0.0.7
Operating System: Mac OS X (10.4)

In your script, kd is set to the result of ES keys down only once: before the loop is started. Then, every time though that loop only that initial value is checked to see if it contains “control”.

You need to put the call to ES keys down inside the loop to check the keyboard state for each loop iteration.

repeat 10 times
	tell application "Extra Suites" to set kd to ES keys down
	if kd contains "command" then
		exit repeat
	else
		say "nothing pressed"
	end if
	delay 1
end repeat

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Thanks so much. That did it. For some reason, “escape” doesn’t work on the Intel Mac, although it does on my older Powerbook. But the other keys work fine. If I used ES keys down as the command instead of kd, then would I not have to make a call to Extra Suites each time?

BTW, how do I paste in scripts like everyone else does? This is all I get.


I am not quite sure what you are asking here. The script listed in post #9 makes one call to Extra Suites per loop iteration. If you want the script to check the current state of the keyboard for each loop iteration, the script must make a call to Extra Suites for each iteration. The return value from ES keys down is just an AppleScript list of zero or more AppleScript strings. The items in that list will never change unless your script changes them. Extra Suites is no longer involved by the time the if statement is executed. To get a new value the script has to make another call to Extra Suites.

If you are bothered by saving a value in a variable and then use that variable in only one place, you can eliminate the kd variable from the script if you like:

repeat 10 times
	tell application "Extra Suites" to ES keys down
	if result contains "command" then
		exit repeat
	else
		say "nothing pressed"
	end if
	delay 1
end repeat

Also, it looks like Extra Suites has a way to test just for the command key, if that is all you are actually interested in:

repeat 10 times
	tell application "Extra Suites" to ES command down
	if result then
		exit repeat
	else
		say "command not pressed"
	end if
	delay 1
end repeat

If you are concerned with the tell command, you can play around with the scope of the tell block, however it is my opinion that, in general, the smaller the tell block the better. Things can get confusing when OSAXen and/or applications have similar dictionary entries. If you keep the scope of the tell blocks as small as possible, it limits potential problems.

Here the entire if statement is in the scope of the tell to Extra Suites:

repeat 10 times
	tell application "Extra Suites"
		if ES command down then
			exit repeat
		else
			say "command not pressed"
		end if
	end tell
	delay 1
end repeat

Here the whole loop in the in the scope of the tell to Extra Suites:

tell application "Extra Suites"
	repeat 10 times
		if ES command down then
			exit repeat
		else
			say "command not pressed"
		end if
		delay 1
	end repeat
end tell

These arrangement of the tell block/statement do not affect how many keyboard related calls are made to the Extra Suites application, mostly it affects how the text of the script is compiled (actually in this case, the say command is affected to, but in a mostly harmless way).

Yes, that is what the Applescript button produces when you click it without any text selected. You should paste the text of your script in between those tags (the point between the adjacent close bracket and opening bracket).

The easiest way to see what the post text is supposed to look like for linked scripts is to click on the Quote link at the bottom of a post that has such a working AppleScript link. You can click this link to see post #9 from this thread. When you are done looking at the quoted post text and how the tags work, just click Go back, or press the back button in your browser, or close the tab/window (as long as you do not click Submit you will not create a new post).

Basically, you just need to put

 before the start of your script text and 

after the end of your script text. One way is to paste your script text into the post, then select it and click the Applescript button above the smilies. Or you can click the Applescript button with nothing selected, move the cursor to the point between the two newly inserted tags and then paste your script text. Or you can just type the open bracket, a, p, p, l, e, s, c, r, i, p, t, close bracket, then paste or type the text of your script, and finally type open bracket, slash, a, p, p, l, e, s, c, r, i, p, t, close bracket.

Thanks so much for your very complete reply. This forum is SO helpful for newbies like myself.

Extra Suites looks like it has lots of useful commands. Is there a good place to go to read about it? The example scripts didn’t tell me much about the various options for ES keys down, for example (like the “command” option.

Thanks again for your help.

All the information I used when doing research for this thread came from the dictionary of the Extra Suites application. Dictionaries can be accessed by choosing the File > Open Dictionary. menu item in Script Editor.

The dictionary of an application (or OSAX) tells you the commands (verbs) and classes (nouns) that the application (or OSAX) supports in AppleScript. Ideally the dictionary entries will fully describe what each command does and what each class represents. However, like any other piece of documentation, dictionaries are vary in quality (some only list the AppleScript words, without much description).

Along with the AppleScript Language Guide, application and OSAX dictionaries are the primary guides to learn what pseudo-English phrases are valid in AppleScript programs.

This article at unScripted has a some information about dictionaries.