Press esc to close the window

That is something I like: press esc will close the front window.

I am in a Windows testing process and found that some windows respond to an esc press by closing the window and some don’t.

At first, I added this to the Window Event Handler:

Function KeyDown(Key As String) As Boolean // Press the esc Key to close this window If Key = Chr(27) Then Self.Close End If End Function

And this works.

Then, I checked the code in all other windows (I wanted to use the same code in all windows)… I was unable to find any code but the window auto close on an esc key press without code (I do not found any code)…

Idea ?

Nota: I checked code, properties, no subclass involved, nothing that can respond to the esc key press. Same Frame --> Type --> Document, same MenuBar… (MainMenuBar)… a true mystery !

Do the windows where this works have buttons, like a “Cancel” button? Traditionally, esc will press the Cancel button and that may be what you’re seeing.

Otherwise, I’d create a window subclass that includes this code, then set the super of all windows that should use this behavior to that subclass. In fact, I’ve learned that it’s a good idea to subclass all the native controls for a project as it makes it easier to change default behavior across the entire app.

A possible workaround could be to hide a Cancel button in the window - make it 1 pixel wide and deep. Then put “Self.Close” for its action.

Thanks Kem, Peter.

This can be the case. I will check. (I totally forgot about that !)

Too small sized button to be seen ? This is an idea to explore !

Talking about “hidden” buttons…

I noticed a Phantom behind my Listbox (in a window) and wondered / asked me where does it comes from.

It happens to be moved (by me) outside of the Window (above) at design time (I do not wated to clear it initialy in case I had to revert it to its original location. Once I cleared it, it does not show itself.
Strange.