key down "3" types "a"

this is my current applescript, and it just makes my computer spam “a”, i have compiled and saved the code, restarted computer, resetarted applescript, copy pasted the code into new window etc. It does this no matter what i put into the quotation marks, and does and if i use (key code 20) it spams 3a3

tell application "System Events"
	repeat
		delay (random number from 1 to 4)
		key down "3"
		delay 0.2
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
		key up "3"
	end repeat
end tell

I never saw commands named “key down” or “key up”.
The only two which I know are keystroke and key code which may be used in :


tell application "System Events"
	tell process "TextEdit"
		set frontmost to true
		keystroke "p"
		keystroke "Azertyuiop" # with a bug in 10.12.3, it issue some unwanted uppercased chars
		keystroke "c" using {command down}
		key code 36 # issue return
		key code 36 # issue return
		keystroke "next line"
	end tell
end tell

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 7 mars 2017 11:53:19

key down and key up haven’t appeared in my System Events dictionaries for years, but still appear to work. I think they were intended for use with modifier keys rather than with character keys. I wouldn’t recommend their use anyway as it’s difficult to get back control of the affected keys unless each down in the script gets cancelled with an up.

I don’t know where the “a”'s coming from in this case, but it can appear when the character specified with a keystroke command isn’t in the character set of the logical keyboard currently in use.

Scripted key commands can also interact with any physical keys currently being held. So if you run a script by keying Command-“r” in Script Editor, the fact that you’re holding down the Command key can influence any keystrokes issued by the script ” unless you let go of it in time, of course.

Hello Nigel

You get the “a” on QWERTY keyboards. On my AZERTY one I get “q” so I guess that there is a link with key code , not with keystroke.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 7 mars 2017 12:10:01

Hi Yvan.

Yes. Sorry. You’re right. I think unrecognised characters come out the same as key code 0.

Hi Nigel

Thanks for the feedback.

More important : in the late beta of 10.12.4 (v4), the bug reported in my first message is gone.

We may now get correct behavior when we use

keystroke "Azertyuiop"
keystroke "Qsdfghjklm"

Only the A and the Q are uppercased characters.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 7 mars 2017 12:32:20

I had no idea that key up and key down worked still. I haven’t seen that in applescript in sooo many years.
OP, here’s a reference list of all the key codes: http://macbiblioblog.blogspot.com/2014/12/key-codes-for-function-and-special-keys.html , so you can change your script.

CAUTION. key code is dependent of the keyboard layout in use.

On a French keyboard, key code 0 issue “q” although the referenced list state that it issue “A”
On a French keyboard, key code 12 issue “a” although the referenced list state that it issue “Q”

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) Tuesday, 7 March 2017 20:52:42

An important warning, Yvan!

I expanded the note that was already at the bottom of the reference list making clear that some of the key codes are keyboard layout dependent… it’s now more obvious.

When I published that list years ago, it was mostly for use with function keys, etc, which are not keyboard layout dependent. It’s much more standard to use keystroke for alphanumeric keys, as you know. The reference list is obviously helpful, since it gets so many hits weekly.

Just to clarify the language a little: “key codes” are generated by keys according to their physical positions in the keyboard. They’re the same for all Macs, regardless of what’s printed on the keys (although different keyboards can have different subsets of keys). The first letter key on my keyboard generates key code 12 when pressed and so does the first on Yvan’s. But mine has a “Q” on it and his has an “A”. What governs the character produced by the key is the software keyboard layout (or “Input Source”) set in System Preferences. This maps key codes and key code combinations to the appropriate characters.

So to clarify what I’m clarifying: key codes themselves aren’t influenced by the keyboard layout in use, but characters associated with them are.

That’s a helpful clarification to how one talks about the differing results. Better to explicitly state what we know in our mind.
I updated the “cautionary note” at the bottom of that reference list to incorporate these clarifications: