2011-02-08 76 views
1

在MS Office中如何可能我去編程覆蓋熱鍵?重寫微軟Office熱鍵

我有一個全球熱鍵(CTRL + SHIFT + 1)爲我的應用程序在其他應用程序,但似乎迷路,當我在任何MS Office應用程序中嘗試它。當我關閉我的應用程序時,熱鍵在MS Office中再次運行,因爲它們應該是這樣。

+2

爲什麼`Python`標籤? – user225312 2011-02-08 15:50:02

回答

0

的情況下,有人在這裏把這個要查看的其他回答。這也描述了一個鉤子。

這是可能做到這一點使用鍵盤鉤子。這方面的一個良好的鉤類可以在此CodeProject Article

發現使用以下代碼將防止WIN + LEFTWIN + RIGHT的發生。你可以用它來覆蓋你想要的任何鍵。

這甚至會覆蓋您通過RegisterHotKey贏API加入熱鍵。

一旦你在你的項目中這些類,你可以像下面添加處理程序的靜態HookManager類。

//It's worth noting here that if you subscribe to the Key_Press event then it will break the international accent keys. 
HookManager.KeyPress += HookManager_KeyPress; 
HookManager.KeyDown += HookManager_KeyDown; 
HookManager.KeyUp += HookManager_KeyUp; 

您還可以添加鼠標事件,但爲了簡單起見,我只是顯示鍵盤掛鉤。

我還創建了一個通用的列表,以便知道哪些鍵是當前下來,我從上KeyUp事件列表中刪除這些密鑰。

public static List<Keys> keysDown = new List<Keys>(); 
private static void HookManager_KeyDown(object sender, KeyEventArgs e) 
     { 
      //Used for overriding the Windows default hotkeys 
      if(keysDown.Contains(e.KeyCode) == false) 
      { 
       keysDown.Add(e.KeyCode); 
      } 

      if (e.KeyCode == Keys.Right && WIN()) 
      { 
       e.Handled = true; 
       //Do what you want when this key combination is pressed 
      } 
      else if (e.KeyCode == Keys.Left && WIN()) 
      { 
       e.Handled = true; 
       //Do what you want when this key combination is pressed 
      } 

     } 

     private static void HookManager_KeyUp(object sender, KeyEventArgs e) 
     { 
      //Used for overriding the Windows default hotkeys 
      while(keysDown.Contains(e.KeyCode)) 
      { 
       keysDown.Remove(e.KeyCode); 
      } 
     } 

     private static void HookManager_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      //Used for overriding the Windows default hotkeys 

     } 

     public static bool CTRL() 
     { 
      //return keysDown.Contains(Keys.LShiftKey) 
      if (keysDown.Contains(Keys.LControlKey) || 
       keysDown.Contains(Keys.RControlKey) || 
       keysDown.Contains(Keys.Control) || 
       keysDown.Contains(Keys.ControlKey)) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

     public static bool SHIFT() 
     { 
      //return keysDown.Contains(Keys.LShiftKey) 
      if (keysDown.Contains(Keys.LShiftKey) || 
       keysDown.Contains(Keys.RShiftKey) || 
       keysDown.Contains(Keys.Shift) || 
       keysDown.Contains(Keys.ShiftKey)) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

     public static bool WIN() 
     { 
      //return keysDown.Contains(Keys.LShiftKey) 
      if (keysDown.Contains(Keys.LWin) || 
       keysDown.Contains(Keys.RWin)) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

     public static bool ALT() 
     { 
      //return keysDown.Contains(Keys.LShiftKey) 
      if (keysDown.Contains(Keys.Alt)) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     }