Close a window with escape key

What is the best way to close a window with the escape key? What if the window itself doesn’t have focus?

I’ve tried detecting the key in window.keydown and it doesn’t work. Do I need to check in every control or is there a better way?

Thanks!

-Mike

Use this

if Asc(key) = 27 then
me.Close
end if

As far as I know you have to do catch the key stroke in every single control. Of course you could subclass the controls you use, so for each different control you’d only have to implement this once instead for each instance.

Thanks guys! I thought I must have been missing something obvious but I guess not!

For my purposes it’s probably enough to just send the keys to a method that checks the ascii code. It’s only for a few forms.

Use a PushButton with it’s ‘Cancel’ property checked.