【问题标题】:How to Suppress task switch keys (winkey, alt-tab, alt-esc, ctrl-esc) using low-level keyboard hook in c#如何在 c# 中使用低级键盘挂钩抑制任务切换键(winkey、alt-tab、alt-esc、ctrl-esc)
【发布时间】:2011-03-13 22:03:18
【问题描述】:

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

【问题讨论】:

  • 你为什么要这样做?
  • 任何类型的信息亭应用程序...有效的问题不知道为什么是 DV,但这可能是重复的。
  • 大多数 PC 游戏都是这样做的。独家全屏在许多系统上获得更好的性能,谁希望在您尝试玩自己喜欢的游戏时弹出 Skype 聊天?

标签: c# winforms winapi


【解决方案1】:

我有完整的代码来禁用 Windows KeyAlt + 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
  • 运行良好,但 Alt+Esc 仍然导致问题(尝试了 CarlosBlanco 的解决方案,但没有解决)。但是,添加条件 objKeyInfo.key == Keys.Escape && HasAltModifier(objKeyInfo.flags) 有效。
  • 很棒的解决方案,效果很好,除了我退出 alt + tab 后出于某种原因仍然被禁用。我尝试使用UnhookWindowsHookEx,但它似乎没有用
【解决方案2】:

您可以使用OnKeyDown 事件来捕获按下的键并抑制您不想允许的键。

Scott Hanselman 的 BabySmash application 确实禁用了大多数按键,如 alt-tab alt-esc 等。大部分 source and development 可以在他的博客上找到。来源在GitHub。在源代码中,您将看到他的 InterceptKeys 类,它使用许多 win32 调用来获取按下的键的低级别挂钩。然后,他在 App.xaml.cs 文件中的 HookCallback 中处理这些。希望这会有所帮助。

Similar Question

Another Similar

【讨论】:

  • 添加了另一个类似的 SO 问题。你不能抓住 ctrl-alt-del,可能不是 alt-tab,但大多数其他的。应该可以做win key。
  • babysmash 示例满足您的需求
  • Alt-Tab 是可捕捉的,但不推荐。 Ctrl-Alt-Del 不能被困在用户模式。句号。
猜你喜欢
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 2010-11-23
  • 2013-06-30
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多