Keyboard shortcuts usually speed up your productivity - until you press the wrong one. For me it's CMD+Q. Almost everyday I close applications1 by accident which I want to stay open, either because I use them often enough through the day or they are essential for other workflows.
To prevent this from happening there's actually an app for it but I'm not a fan of running several helper apps cluttering up my Mac, especially if you can accomplish the same functionality with the tools you already have. In this case it's Keyboard Maestro.
The following macro is triggered by CMD+Q (obviously). If you press the SHIFT key within 3 seconds the current front most application at the time of triggering will be closed. During that period a large message display tells you what's going on2.
The core of the macro is a loop which checks every 0.2 seconds whether the SHIFT key is pressed. The small time setting ensures that even a short press is recognised. If this happens an AppleScript will quit the application. I decided not to use a build in action for this because it can only close the front most application, which might have changed since the macro was triggered. The current application is saved to a variable at the beginning of the macro and then used in the AppleScript.
Unfortunately with Keyboard Maestro it is not possible to remove the message display to indicate the end of the loop. Therefore a short sound will notify you about it.
If you want the macro only to apply to certain applications you have to create a macro group and put it in there. Other than that I guess the sequence is simple enough to alter it to your own specific needs or preferences. Maybe you want to change the message display. Or you could prevent CMD+Q from doing anything by just having no action at all in the macro. The only way to close an application would be to use its menu then.
You can download the macro at my Keyboard Maestro Repository on Github.
In most of the cases it's finger trouble where I miss CMD+A or CMD+W. ↩
I published a similar macro on my blog in July 2012. It required to keep holding CMD+Q for another two seconds to close the front application. In general it worked, but if you pressed CMD+Q too long, the next application coming to the front was closed too. ↩