escape keystroke?

is there a line of code to make the keystroke escape key in an application?


The escape key is ASCII character 27, so you invoke it from an AppleScript as:

tell application System Events to key code 27

It’s in the Processes Suite of the System Events Dictionary for AppleScript

Hi guys.

I don’t think key codes are quite the same as ascii numbers, so it might be worth trying <key code 53> or <keystroke (ASCII character 27)>. It’s also a good idea to make sure the target application process is frontmost, which you can achieve like this:

tell application "System Events" to tell process "Target" -- modify as required
	set frontmost to true
	key code 53
end tell

Edit: FWIW, some preliminary tests here suggest that key code is about twice as fast as keystroke (ASCII character [n]) - presumably because of the extra call to Standard Additions for the ASCII conversion. (IIRC, though, keystroke predates key code - and should therefore be a bit more portable…) :slight_smile:

Thanks! It worked!


Where did you get a table of key codes? Is there a simple transformation?

Go to and search OSX applications for “Key Codes”. There is a free utility you can download. Make sure you are looking at the decimal number in the bottom-right corner (not the Hex code) when you use it.

Got it (in fact I already had it and forgot :rolleyes:) - Thanks.

Full Key Codes

Full Key Codes, Adam - as since mentioned. Every now and then, I clean out my files and dump a bunch of third party utilities that I no longer use. This is one I hang on to! :slight_smile: