Comment obtenir l’événement «KeyPress» à partir d’un complément Word 2010 (développé en C #)?

Comment “attraper” l’événement KeyPress à partir d’un complément Word 2010 développé en C #?

Remarque: je ne cherche pas des solutions “complexes” comme le trucage du crochet, mais du net. NET même à partir du modèle d’object.

L’object d’application que j’ai “entre mes mains” est:

Microsoft.Office.Interop.Word.Application

Meilleures salutations

Malheureusement, il n’y a rien d’intégré dans l’API Word ou VSTO qui puisse capter les touches du clavier. Vous trouverez plus d’informations à ce sujet ici.

Je cherchais une solution réalisable depuis un certain temps, mais le mieux que je pouvais imaginer était de la gérer via l’API Windows à l’aide de points d’ancrage. Il est probable que vous aboutissez à la même conclusion. Voici un exemple:

Vous devrez append une directive using aux assemblys suivants:

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

Et voici le crochet:

  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(ssortingng 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); ssortingng pressedKey = ((Keys)pointerCode).ToSsortingng(); //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); } } 

Vous pouvez essayer d’utiliser le contrôle Excel WebBrowser au lieu de System.Windows.Forms WebBrowser; il gère les transferts de clé spéciaux tels que TAB, DEL, CTRL + V, etc.

Pour cela changer le constructeur WebBrowser de

 new System.Windows.Forms.WebBrowser(); 

à

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

Vous devez append des références à votre projet: Projet / Ajouter une référence / Extensions, sélectionnez Microsoft.Tools.Outlook & Microsoft.Tools.Outlook.v4.0.Utilities