【发布时间】:2011-03-13 22:03:18
【问题描述】:
谁能告诉我如何使用c#禁用任务切换键
【问题讨论】:
-
你为什么要这样做?
-
任何类型的信息亭应用程序...有效的问题不知道为什么是 DV,但这可能是重复的。
-
大多数 PC 游戏都是这样做的。独家全屏在许多系统上获得更好的性能,谁希望在您尝试玩自己喜欢的游戏时弹出 Skype 聊天?
谁能告诉我如何使用c#禁用任务切换键
【问题讨论】:
我有完整的代码来禁用 Windows Key , Alt + Tab 等等..
现在我提供以下代码供其他人参考:
/* 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 */
然后在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);
}
【讨论】:
System.Windows.Input.ModifierKeys' is a 'type' but is used like a 'variable
if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (Keyboard.Modifiers & ModifierKeys.Control) != 0)
Attempted to read or write protected memory. This is often an indication that other memory is corrupt
objKeyInfo.key == Keys.Escape && HasAltModifier(objKeyInfo.flags) 有效。
UnhookWindowsHookEx,但它似乎没有用
您可以使用OnKeyDown 事件来捕获按下的键并抑制您不想允许的键。
Scott Hanselman 的 BabySmash application 确实禁用了大多数按键,如 alt-tab alt-esc 等。大部分 source and development 可以在他的博客上找到。来源在GitHub。在源代码中,您将看到他的 InterceptKeys 类,它使用许多 win32 调用来获取按下的键的低级别挂钩。然后,他在 App.xaml.cs 文件中的 HookCallback 中处理这些。希望这会有所帮助。
【讨论】: