2010-07-09 70 views
9

誰能告訴我如何使用C#來禁用任務切換鍵,禁止任務切換鍵(WINKEY,ALT標籤,ALT-ESC,CTRL-ESC)如何使用低級別的鍵盤鉤子在C#

+1

爲什麼要這麼做? – Fosco 2010-07-09 14:34:01

+4

任何類型的自助服務終端應用...有效的問題不知道爲什麼DV,這可能是重複的。 – 2010-07-09 14:37:25

+0

大多數電腦遊戲都這樣做。獨家全屏在許多系統上獲得更好的性能,誰想要在您嘗試播放自己喜歡的遊戲時彈出Skype聊天? – yoyo 2016-02-24 17:45:39

回答

15

我已經得到了完整的代碼禁用Windows鍵Alt鍵 + 標籤等..

現在我提供以下代碼作爲其他人的參考:

/* 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 */ 

然後Inside 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); 
    } 
+0

我得到這個錯誤錯誤\t'System.Windows.Input.ModifierKeys'是一個'類型',但像'變量'使用 – Omar 2013-03-13 15:20:42

+1

我最終使用這種檢查'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)' – Omar 2013-03-13 15:28:59

+0

我使用這個代碼,但我得到錯誤'試圖讀取或寫入受保護的內存。這通常表示其他內存已損壞 – Niloo 2014-01-15 19:58:49

5

您可以使用OnKeyDown事件來捕獲按下的按鍵,並抑制不想允許的按鍵。

Scott Hanselman的BabySmash application確實禁用了alt-tab alt-esc等大多數按鍵筆畫。大多數source and development都可以在他的博客中找到。來源是GitHub。在源代碼中,您會看到他使用許多win32調用的InterceptKeys類來獲取按下按鍵的低級掛鉤。然後他在App.xaml.cs文件中的HookCallback中處理這些內容。希望這可以幫助。

Similar Question

Another Similar

+0

增加了另一個類似的SO問題。你不能抓住CTRL-ALT-DEL,也可能不是ALT-TAB,但大多數人。應該能夠贏得關鍵。 – 2010-07-09 18:29:18

+0

babysmash示例做你正在尋找的 – 2010-07-09 18:44:59

+2

Alt-Tab是可捕獲的,但不推薦。 Ctrl-Alt-Del不能被置於用戶模式。句號。 – ChrisV 2010-07-09 18:52:03