2011-11-14 69 views
14

如何從C#中開發的Word 2010 Addin中「捕獲」KeyPress事件?如何從Word 2010插件(用C#開發)獲取「KeyPress」事件?

注意:我不是在尋找「複雜」的解決方案,比如連接東西,但對於漂亮整齊的.NET甚至是對象模型。

應用對象我有「在我手中」的是:

Microsoft.Office.Interop.Word.Application

問候

+0

autohotkey: http://stackoverflow.com/questions/31470984/capturing-keydown-event-of-ms-word – wideweide

回答

17

不幸的是,沒有內置的Word API中或VSTO可以拿起擊鍵,關於這方面的更多信息可以發現here

我一直在尋找一個可行的解決方案一段時間,但最好的我可以拿出來是通過使用鉤子處理它通過Windows API ,很可能你會得出同樣的結論所以這裏有一個例子:

你需要一個using指令添加到以下組件:

using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Threading; 
using System.Windows.Forms; 

而這裏的鉤:

public partial class ThisAddIn 
    { 
     private const int WH_KEYBOARD_LL = 13; 
     private const int WM_KEYDOWN = 0x0100; 

     private static IntPtr hookId = IntPtr.Zero; 
     private delegate IntPtr HookProcedure(int nCode, IntPtr wParam, IntPtr lParam); 
     private static HookProcedure procedure = HookCallback; 

     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     private static extern IntPtr GetModuleHandle(string lpModuleName); 

     [DllImport("user32.dll", SetLastError = true)] 
     private static extern bool UnhookWindowsHookEx(IntPtr hhk); 

     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     private static extern IntPtr SetWindowsHookEx(int idHook, HookProcedure lpfn, IntPtr hMod, uint dwThreadId); 

     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); 

     private void ThisAddIn_Startup(object sender, System.EventArgs e) 
     { 
      hookId = SetHook(procedure); 
     } 

     private void ThisAddIn_Shutdown(object sender, System.EventArgs e) 
     { 
      UnhookWindowsHookEx(hookId); 
     } 

     private static IntPtr SetHook(HookProcedure procedure) 
     { 
      using (Process process = Process.GetCurrentProcess()) 
      using (ProcessModule module = process.MainModule) 
       return SetWindowsHookEx(WH_KEYBOARD_LL, procedure, GetModuleHandle(module.ModuleName), 0); 
     } 

     private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) 
     { 
      if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN) 
      { 
       int pointerCode = Marshal.ReadInt32(lParam); 
       string pressedKey = ((Keys)pointerCode).ToString(); 

       //Do some sort of processing on key press 
       var thread = new Thread(() => { MessageBox.Show(pressedKey); }); 
       thread.Start(); 
      } 
      return CallNextHookEx(hookId, nCode, wParam, lParam); 
     } 

     private void InternalStartup() 
     { 
      this.Startup += new System.EventHandler(ThisAddIn_Startup); 
      this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); 
     } 
    } 
+0

爲什麼線程內的處理代碼?在我自己的測試中,如果處理不是線程的,CallNextHookEx似乎失敗,但我在_why_上找不到任何文檔。 –

+1

上面的代碼不會在MS Word中陷印字符類型,它只是在字外 –

0

您可能會嘗試使用Excel WebBrowser Control而不是System.Windows.Forms WebBrowser;對於從

new System.Windows.Forms.WebBrowser(); 

的web瀏覽器構造器更改爲

new Microsoft.Office.Tools.Excel.Controls.WebBrowser(); 

您需要將引用添加到它處理的特殊鍵轉發爲TAB,DEL,CTRL + V等

您項目:項目/添加引用/擴展選擇Microsoft.Tools.Outlook & Microsoft.Tools.Outlook.v4.0.Utilities

相關問題