2011-11-14 9 views
14

Come posso "catturare" l'evento KeyPress da un componente aggiuntivo di Word 2010 sviluppato in C#?Come ottenere l'evento "KeyPress" da un AddIn Word 2010 (sviluppato in C#)?

Nota: non sono alla ricerca di soluzioni "complesse" come il routing, ma per il bello e ordinato .NET anche dal modello a oggetti.

L'oggetto applicazione che ho "nelle mie mani" è:

Microsoft.Office.Interop.Word.Application

migliori saluti

+0

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

risposta

17

Purtroppo non c'è nulla di built-in nella Parola API o VSTO che può raccogliere i colpi di chiave, maggiori informazioni su questo possono essere trovati here

Sono stato alla ricerca di una soluzione fattibile per qualche tempo, ma il meglio che ho potuto venire è stato gestirlo tramite API di Windows utilizzando ganci , È probabile che si raggiunge la stessa conclusione ecco un esempio:

Avrai bisogno di aggiungere una direttiva using ai seguenti gruppi:

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

Ed ecco il gancio:

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

Perché il codice di elaborazione all'interno di una discussione? Nel mio test, CallNextHookEx sembra fallire se l'elaborazione NON è in corso, ma non riesco a trovare alcuna documentazione su _why_. –

+1

Sopra il codice non intrappola il tipo di carattere in MS Word, appena fuori dalla parola –

0

Si potrebbe provare a utilizzare Excel WebBrowser Control invece del WebBrowser System.Windows.Forms; gestisce l'inoltro tasto speciale come TAB, DEL, CTRL + V, ecc

Per che cambiano il contructor WebBrowser da

new System.Windows.Forms.WebBrowser(); 

a

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

si avrebbe bisogno di aggiungere i riferimenti al vostro progetto: Aggiungi/Aggiungi riferimento/Estensioni seleziona Microsoft.Tools.Outlook & Microsoft.Tools.Outlook.v4.0.Utilities

Problemi correlati