Using the close menu option (via the control menu hanging off the window icon, or Alt-F4, or the "X" button) sends a request to unload.

If your program interferes with this then it won't unload.

How do you "click on" the escape key???