c# - 如何在 C# 中使用低级键盘钩子(Hook)来抑制任务切换键(winkey、alt-tab、alt-esc、ctrl-esc)

标签 c# winforms winapi

谁能告诉我如何使用 C# 禁用任务切换键

最佳答案

我有完整的代码来禁用 Windows 键 , 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);  
   }

关于c# - 如何在 C# 中使用低级键盘钩子(Hook)来抑制任务切换键(winkey、alt-tab、alt-esc、ctrl-esc),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3213606/

相关文章:

Delphi 7 : How to copy non-latin text to clipboard?(转换为 Unicode?)

c# - 内部接口(interface)实现的奇怪限制

c# - Task.ContinueWith 不适用于 OnlyOnCanceled

c# - 模型 View View Controller C#

c# - 如何仅刷新数据绑定(bind) DataGridView 的可见行?

windows - 只为一个进程初始化一次临界区

c++ - 从 IIS 7.5 Web 应用程序调用 Win32 CreateEvent() 失败

c# - 如何通过 VSTS REST API 创建构建定义

c# - 使窗体的背景透明

c# - WebBrowser 文档始终为空