By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,384 Members | 845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,384 IT Pros & Developers. It's quick & easy.

Disable ALT+F4; CTRL+ALT+DEL; CTRL+ESC;ALT+TAB

P: n/a
Hy,

i have an app and i must disable this combination: ALT+F4;
CTRL+ALT+DEL; CTRL+ESC;ALT+TAB like this:

i find something on Internet and i can block ALT+F4

protected override System.Boolean
ProcessCmdKey(ref System.Windows.Forms.Message
msg,System.Windows.Forms.Keys keyData)
{
if ((msg.Msg == 0x104) && (((int)
msg.LParam) == 0x203e0001))
return true;
return false;
}

pls help, how can i block the rest of keys.
Stefanelus
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Stefan,

You need to P/Invoke SetWindowsHookEx:

I got this code from somewhere but I did not test it at the end.

public class Win32Hook
{

[DllImport("kernel32")]
public static extern int GetCurrentThreadId();

[DllImport( "user32",
CharSet=CharSet.Auto,CallingConvention=CallingConv ention.StdCall)]
public static extern int SetWindowsHookEx(
HookType idHook,
HOOKPROC lpfn,
int hmod,
int dwThreadId);

public enum HookType
{
WH_KEYBOARD = 2
}

public delegate int HOOKPROC(int nCode, int wParam, int lParam);

public void SetHook()
{
// set the keyboard hook
SetWindowsHookEx(HookType.WH_KEYBOARD,
new HOOKPROC(this.MyKeyboardProc),
0,
GetCurrentThreadId());
}

public int MyKeyboardProc(int nCode, int wParam, int lParam)
{
//Perform your process
return 0;
}
}
And then you can install the hook procedure by the following code:

Win32Hook hook = new Win32Hook();
hook.SetHook();

In the MyKeyboardProc function, you can determine which key is pressed by
the wParam parameter. You also can identify the key is pressed or released
by the lParam parameter. Then you can set a flag when the Ctrl is pressed
and set another flag when the Alt is pressed. When both flag are set, stop
the program.
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Stefan" <st****@inwww.ltd.uk> wrote in message
news:5e****************************@phx.gbl...
Hy,

i have an app and i must disable this combination: ALT+F4;
CTRL+ALT+DEL; CTRL+ESC;ALT+TAB like this:

i find something on Internet and i can block ALT+F4

protected override System.Boolean
ProcessCmdKey(ref System.Windows.Forms.Message
msg,System.Windows.Forms.Keys keyData)
{
if ((msg.Msg == 0x104) && (((int)
msg.LParam) == 0x203e0001))
return true;
return false;
}

pls help, how can i block the rest of keys.
Stefanelus

Nov 15 '05 #2

P: n/a
Hi Stefan,

"Stefan" <st****@inwww.ltd.uk> wrote in message
news:5e****************************@phx.gbl...
Hy,

i have an app and i must disable this combination: ALT+F4;
CTRL+ALT+DEL; CTRL+ESC;ALT+TAB like this:

i find something on Internet and i can block ALT+F4


I don't know how far you'll get blocking the rest of them, but blocking
the CTRL+ALT+DEL sequence (esp. on NT, 2K, XP) is practically impossible.
Okay, there's this thing called a GINA dll, but trust me, you don't want to
go there.

The reason for this is security. Just about any application can put up a
set of dialogs that look like the standard secure windows login dialog(s).
Pressing CTRL+ALT+DEL is the guaranteed way to get to the "real" login
screen without worrying that you're about to give your user name/password to
a java applet, etc.

Regards,
Dan
Nov 15 '05 #3

P: n/a


Hy Ignacio,

thx for your help, i find something on internet:

private delegate int LowLevelKeyboardProcDelegate(int nCode, int
wParam, ref KBDLLHOOKSTRUCT lParam);
[ DllImport( "user32.dll", EntryPoint="SetWindowsHookExA",
CharSet=CharSet.Ansi )]
private static extern int SetWindowsHookEx(int idHook ,
LowLevelKeyboardProcDelegate lpfn, int hMod , int dwThreadId);
[ DllImport( "user32.dll", EntryPoint="UnHookWindowsHookEx",
CharSet=CharSet.Ansi )]
private static extern int UnHookWindowsEx(int hHook);
[ DllImport( "user32.dll", EntryPoint="CallNextHookEx",
CharSet=CharSet.Ansi )]
private static extern int CallNextHookEx(int hHook,int nCode, int
wParam, ref KBDLLHOOKSTRUCT lParam);
const int WH_KEYBOARD_LL = 13;
public struct KBDLLHOOKSTRUCT
{
public int vkCode;
int scanCode;
public int flags;
int time;
int dwExtraInfo;
}
private int intLLKey;
private KBDLLHOOKSTRUCT lParam;

private int LowLevelKeyboardProc(int nCode,int wParam,ref
KBDLLHOOKSTRUCT lParam)
{
bool blnEat = false;
switch (wParam)
{
case 256:
case 257:
case 260:
case 261:
//Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
if(((lParam.vkCode == 9) && (lParam.flags == 32)) ||
((lParam.vkCode == 27) && (lParam.flags == 32)) || ((lParam.vkCode ==
27) && (lParam.flags == 0)) || ((lParam.vkCode == 91) && (lParam.flags
== 1)) || ((lParam.vkCode == 92) && (lParam.flags == 1)) || ((true) &&
(lParam.flags == 32)))
{
blnEat = true;
}
break;
}

if (blnEat)
return 1;
else return CallNextHookEx(0, nCode, wParam, ref lParam);

}
public void KeyboardHook(object sender, EventArgs e)
{
intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, new
LowLevelKeyboardProcDelegate(LowLevelKeyboardProc) ,
System.Runtime.InteropServices.Marshal.GetHINSTANC E(System.Reflection.As
sembly.GetExecutingAssembly().GetModules()[0]).ToInt32(),0);
}
Stefan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.