I saw on another thread a way to initiate a shutdown, but I would like to do something a little trickier. How can I detect a shutdown is happening and abort it? Basically, I am trying to disable the sleep key on the keyboard. I've tried overridding the system key handler, but the sleep key still makes it through somehow.
System level keys are tricky. And to make it trickier the situation you are asking about is handled differently in Windows XP, Vista and 7. The first thing to try is just to catch your Form_Closing event. Check the event parameter for the CloseReason and set e.Cancel to true when you don't want to shutdown. That will prevent most non-critical shutdowns. If you want to get really serious, you need to do some "ugly" pinvoke'ing the windows API for Windows hooks. Do some research about "SetWindowsHookEx" with C#. This will allow you to intercept key stroke at a lower level than your project's internal message loop. This one will also work on newer operating systems like Windows Vista and 7. However, there is one key combination that cannot be overridden in the modern operating systems which is Ctrl+Alt+Delete. This is reserved for operating system security and cannot be intercepted or prevented except via Windows Security Policies. Best Regards, - Ben