Keypress event?

Is there a way using AppleScript or osascript to react to a generic keypress (assuming there is a system event that says when a key has been pressed but doesn’t specify which)?

In the end my goal is to have a small soundfile play whenever I am typing in a chat application (Adium, for instance). If you’ve ever used ICQ, the typewriter soundeffect is the sound I’m using and the effect is what I’m trying to recreate.

Using Play Sound and iKey I have been able to get as far as having a sound play whenever I hit a key I have assigned the script to (the script that plays the sound) but I’ve hit a wall in that I cannot get iKey to also type the character in the application, all it does is play the sound when I hit the key.

I think I could get around this if I could just have the sound played whenever a key is pressed if AppleScript can act on such an event.

Or if it’s not possible, can anyone think of another way I could do this?

Hi,

Firstly, Sytem Events does not get keystrokes but simulates them.

AppleScript is too slow for correct timing on keystrokes.

Using Jon’s Commands, one can get keystrokes. The best way to use this scripting addition is to just play a sound while the user is typing. Suppose you type “xyz” and play beeps while the user is typing. Your script may just beep once, twice or if the person is really slow, then it may beep three times. The reason why it won’t beep three times is that it is too slow and won’t catch every keystroke (i.e. it might miss the “y”). However, this will still inform users that they are typing. Here’s an example:

tell application “Stickies” to activate
repeat until (keys pressed) is {“q”}
set keys_pressed to keys pressed
if keys_pressed is not {} then
tell application “Play Sound”
play alias “Macintosh HD:Users:kel:Desktop:Ping.aiff”
end tell
end if
end repeat

There are different variations of using Jon’s Commands’ ‘keys pressed’, but you need more speed for this.

http://www.seanet.com/~jonpugh/

gl,

Thanks for the reply, it did work but I’m afraid it’s notwhat I was looking for. I’m hoping there is another alternative that maybe your or someone else could suggest.

Using the following code (essentially what you gave me) it plays the typing sound slowly and continues to repeat it for a significant amount of time after I have stopped typing (like it’s playing sounds that queued up).

tell application "Adium" to activate
repeat until (keys pressed) is {"q"}
	set keys_pressed to keys pressed
	if keys_pressed is not {} then
		tell application "Play Sound"
			play alias "Thermopylae:Users:Justin:Desktop:MsgType.wav"
		end tell
	end if
end repeat

So I tried the following, but it just seems to play the sound twice for each press (I know it’s not, but the effect is like it’s playing on key down and on key up) and suffers from a similar lag as the first.

play alias "Thermopylae:Users:Justin:Desktop:MsgType.wav" with concurrence

Is there anything else I could do? The sound effect worked perfectly with my original setup where when a specific key was pressed it played (but obviously if it doesn’t also send the character to the app it’s totally worthless). It definitely needs to be with concurrence.

Hi Justin,

I know you can get more beeps than keystrokes and in a more efficient manner. This is totally uncontrolled. If you get less beeps than keystrokes, then it is slightly controlled.

You might check versiontracker.com for a c program that may do a better job at typewriter sound simulation.

gl,