如何从Word 2010 Addin(用C#开发)获取“KeyPress”事件?

时间:2011-11-14 10:26:06

标签: c# ms-word add-in

如何从C#开发的Word 2010 Addin中“捕获”KeyPress事件?

注意:我不是在寻找像钩子这样的“复杂”解决方案,而是为了整洁而整洁 .NET甚至来自对象模型。

我“在我手中”的应用程序对象是:

Microsoft.Office.Interop.Word.Application

最好的问候

2 个答案:

答案 0 :(得分: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);
        }
    }

答案 1 :(得分:0)

您可能尝试使用Excel WebBrowser Control而不是System.Windows.Forms WebBrowser;它处理特殊的密钥转发,如TAB,DEL,CTRL + V等

更改WebBrowser构造函数
new System.Windows.Forms.WebBrowser();

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

您需要添加对项目的引用:Project / Add Reference / Extensions选择Microsoft.Tools.Outlook& Microsoft.Tools.Outlook.v4.0.Utilities