Question: How do I make an application that I can make the ESC key close a window? Or really any key (I can change if I want to).

Please help. Thanks

Here is what I have and I can't get it to work.

```
-- close window with "ESC"

tell application "System Events"
    tell application "Finder" to activate
    key code 53

end tell

beep 3

```

Thanks! B-

Answer: 
It's considered better to attach it via JavaScript. This falls under the category of "[Unobtrusive JavaScript](http://en.wikipedia.org/wiki/Unobtrusive_JavaScript)". (more specifically here the separation of behavior from the layout)

```
document.getElementById('YourID').onclick = nameOfFunctionToBeCalled;

```
It's a better practice to do this purely from within Javascript, and keep your markup clean. If you're using a separate .js file, this also saves you from having to worry about issues with inline JS code inside HTML.

Using raw DOM it's pretty simple:

```
document.getElementById( "MyButton" ).onclick = DoSomething;

```