Link to home
Start Free TrialLog in
Avatar of maitovasanthinet
maitovasanthinet

asked on

Disable keys in C# program (Alt+Tab,Alt+F4,ctr+Alt+Delete )

Dear Friends,
                         How do disable the Alt+Tab,Alt+F4,ctr+Alt+Delete butttons from C# program?
Can anybody give me a clue or sample code?

Thanks in advance.

Thanks,
maitovasanthinet
ASKER CERTIFIED SOLUTION
Avatar of martinv
martinv
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually there is a pretty easy way to catch all keys. You should use the ProcessCmdKey protected method of the Control class for that. You have to override this function in the form (or control) that you want and check the key yourself like this:

protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
      {
            if (keyData == (Keys.Alt | Keys.F4)) {
                  MessageBox.Show ("Alt + F4 catched!");
                  return true;  // The key is manually processed
            } else
                  return base.ProcessCmdKey (ref msg, keyData);
      }

You can catch the other key combinations as well. For more info see the ProcessCmdKey function in MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclassprocesscmdkeytopic.asp
Sorry, the solution I gave you catches only some of the key combinations you want, it does not catch Ctrl+Alt+Del for example....
To TheAvenger:
Your code works nice for Alt+f4, as Alt+F4 actualy only send close message to actual windows, so you can catch it and ignore it.

But ctrl+alt+delete, alt+esc or alt+tab  don't do it. They are processed by the system (at least in WinXP and WinNT) and actual process don't receive any notification about it.
To martinv:

I got it :-( and I wrote that in the second comment so that the guy knows what it actually can do...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to TheAvenger:
sorry, I've written the comment before your second coment. I was too quick :-)

as for meliorasoft - thank you for pointing to the site. It looks wery interesting. Their documentation says that they are installing kernel level keyboard filter. It's smart and it should work. But drawback is, that  you have to run under administrator account to install such a hook. It's mentioned in their doc.

More over under Windows XP restart is needed. It's suitable for some scenarios such as information kiosk but not for normal application.