Hi I'm a beginner. Can anyone please tell me how to disable the task switch keys using c#
views:
69answers:
2
A:
You can use the OnKeyDown event to capture the keys pressed and suppress the ones you don't want to allow.
Scott Hanselman's BabySmash application does disable most key strokes like alt-tab alt-esc, etc. Most of the source and development can be found on his blog. The source is on CodePlex. In the source, you will see he InterceptKeys class that uses many win32 calls to get low level hooks to the keys pressed. He then handles these in HookCallback in the App.xaml.cs file. Hope this helps.
Mike Ohlsen
2010-07-09 17:00:21
I am not a C# developer, but: "are you sure?" We are not talking about simple keystrokes like "V", or "Ctrl+V", or even "Shift+Alt+Ctrl+V". We are talking about things like Alt+Tab, Ctrl+Alt+Del, etc.
Andreas Rejbrand
2010-07-09 17:15:47
added another similar SO question. You cant catch ctrl-alt-del, and probably not alt-tab, but most others. Should be able to do win key.
Mike Ohlsen
2010-07-09 18:29:18
the babysmash example does what you are looking for
Mike Ohlsen
2010-07-09 18:44:59
Alt-Tab is catchable, but not recommended.Ctrl-Alt-Del cannot be trapped in user mode. Full stop.
ChrisV
2010-07-09 18:52:03
A:
I've got the Complete code to Disable Windows Key, Alt+Tab and so on..
And Now I'm providing that for others reference..
/* Code to Disable WinKey, Alt+Tab, Ctrl+Esc Starts Here */
// Structure contain information about low-level keyboard input event
[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
public Keys key;
public int scanCode;
public int flags;
public int time;
public IntPtr extra;
}
//System level functions to be used for hook and unhook keyboard input
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hook);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern short GetAsyncKeyState(Keys key);
//Declaring Global objects
private IntPtr ptrHook;
private LowLevelKeyboardProc objKeyboardProcess;
private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
{
if (nCode >= 0)
{
KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));
// Disabling Windows keys
if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (ModifierKeys & Keys.Control) == Keys.Control)
{
return (IntPtr)1; // if 0 is returned then All the above keys will be enabled
}
}
return CallNextHookEx(ptrHook, nCode, wp, lp);
}
bool HasAltModifier(int flags)
{
return (flags & 0x20) == 0x20;
}
/* Code to Disable WinKey, Alt+Tab, Ctrl+Esc Ends Here */
Then Inside the Form_Load();
private void Form_Load(object sender, EventArgs e)
{
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
}
ASr..
2010-07-12 10:14:10