2009-07-27 114 views
1

我正在使用下面的代碼來禁用熱鍵,如Alt + f4,ctrl + c,這些熱鍵都很完美。但我無法使用下面的代碼註冊win + L。在c中註冊Win + L熱鍵時出現問題#

namespace KioskMode 
{ 
    public partial class Test : Form 
    { 
     #region Dynamic Link Library Imports 

     [DllImport("user32.dll")] 
     private static extern int FindWindow(string cls, string wndwText); 

     [DllImport("user32.dll")] 
     private static extern int ShowWindow(int hwnd, int cmd); 

     [DllImport("user32.dll")] 
     private static extern long SHAppBarMessage(long dword, int cmd); 

     [DllImport("user32.dll")] 
     private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk); 

     [DllImport("user32.dll")] 
     private static extern int UnregisterHotKey(IntPtr hwnd, int id); 

     #endregion 


     #region Modifier Constants and Variables 

     // Constants for modifier keys 
     private const int USE_ALT = 1; 
     private const int USE_CTRL = 2; 
     private const int USE_SHIFT = 4; 
     private const int USE_WIN = 8; 

     // Hot key ID tracker 
     short mHotKeyId = 0; 

     #endregion 

     public Test() 
     { 
      InitializeComponent(); 
      RegisterGlobalHotKey(Keys.F4, USE_ALT); 
      RegisterGlobalHotKey(Keys.L, USE_WIN); 
     } 

     private void RegisterGlobalHotKey(Keys hotkey, int modifiers) 
     { 
      try 
      { 
       mHotKeyId++; 

       if (mHotKeyId > 0) 
       { 
        // register the hot key combination 
        if (RegisterHotKey(this.Handle, mHotKeyId, modifiers, Convert.ToInt16(hotkey)) == 0) 
        { 
          MessageBox.Show("Error: " + mHotKeyId.ToString() + " - " + 
          Marshal.GetLastWin32Error().ToString(), 
          "Hot Key Registration"); 
        } 
       } 
      } 
      catch 
      { 
       UnregisterGlobalHotKey(); 
      } 
     } 


     private void UnregisterGlobalHotKey() 
     { 
      for (int i = 0; i < mHotKeyId; i++) 
      { 
       UnregisterHotKey(this.Handle, i); 
      } 
     } 

     protected override void WndProc(ref Message m) 
     { 
      base.WndProc(ref m); 
      const int WM_HOTKEY = 0x312; 
      if (m.Msg == WM_HOTKEY) 
      { 
       // Ignore the request or each 
       // disabled hotkey combination 
      } 
     } 
    } 
} 
+1

Windows鍵是「爲操作系統保留的」,所以任何對它的使用都將變得不可靠,並且與用戶對抗:嘗試使用Win-L,您將應用程序的願望放在用戶的需求之前。 – Richard 2009-07-27 09:48:46

+1

@ Richard-我認爲(基於同一個班級的名字)karthik正在創建一個Kiosk應用程序,而不是一個普通的桌面應用程序。 – RichardOD 2009-07-27 10:50:35

回答

5

由於Windows已將其用作熱鍵,因此無法註冊。如果你真的想這樣做,你必須註冊一個低級鍵盤鉤子。

註冊Alt + F4,Ctrl + C ...的原因是這些鍵不是熱鍵(它們只是在wndproc中處理)。

3

爲什麼不通過組策略或編輯註冊表來進行調查呢?