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

S

Stefan

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
 
I

Ignacio Machin

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=CallingConvention.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,
 
D

Daniel Pratt

Hi Stefan,

Stefan said:
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
 
S

Stefan Tabaranu

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.GetHINSTANCE(System.Reflection.As
sembly.GetExecutingAssembly().GetModules()[0]).ToInt32(),0);
}


Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top