I once desperately needed to solve this in a Python/PyQt app. What I came up with is about 1000% more complicated than kopsis' solution but it worked for me. The thing is, when you press the Cancel button you actually do receive the keypress but only *after* the corresponding CloseEvent. So I created an event filter that, upon catching a CloseEvent, sets up a 50ms timer. If it catches a Cancel KeyEvent during those 50ms, the app goes on and the Cancel keypress is processed. If the timer times out and there is no Cancel key, the app closes (because you've tapped the X button on the title bar).
Of course, all that would mean that you can *never* close the app with the Cancel key so my filter can be turned off when you don't need to capture the Cancel key.
In retrospect, maybe I shouldn't've been so scared of the WStyle_Customize flag
z.