ASr*_*r.. 21

我有完整的代码禁用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 */
Run Code Online (Sandbox Code Playgroud)

然后在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);  
   }
Run Code Online (Sandbox Code Playgroud)

  • 工作得很好,除了Alt + Esc仍然导致问题(试过CarlosBlanco的解决方案并且它没有解决它).然而,添加条件`objKeyInfo.key == Keys.Escape && HasAltModifier(objKeyInfo.flags). (2认同)

Mik*_*sen 7

您可以使用OnKeyDown事件捕获按下的键并禁止您不想允许的键.

Scott Hanselman的BabySmash应用程序确实禁用了大多数关键笔划,例如alt-tab alt-esc等.大多数源代码和开发都可以在他的博客上找到.源代码在GitHub上.在源代码中,您将看到InterceptKeys类使用许多win32调用来获取按下的键的低级别挂钩.然后,他在App.xaml.cs文件中的HookCallback中处理这些.希望这可以帮助.

类似问题

另一个类似

  • Alt-Tab可以捕获,但不建议使用.Ctrl-Alt-Del无法在用户模式下被捕获.完全停止. (2认同)