Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the "KeyPress" event from a Word 2010 Addin (developed in C#)?

Tags:

c#

ms-word

add-in

How can I "catch" the KeyPress event from a Word 2010 Addin developed in C#?

Note: I'm not looking for "complex" solutions like hooking stuff, but for the nice and tidy .NET even from the object model.

The application object I have "in my hands" is:

Microsoft.Office.Interop.Word.Application

Best Regards

like image 402
user1045372 Avatar asked Nov 14 '11 10:11

user1045372


1 Answers

Unfortunately there is nothing built-in in the Word API or VSTO which can pick up key strokes, more info on this can be found here

I've been searching for a feasible solution for some time but the best I could come up with was handling it through Windows API using hooks, it's likely you will reach the same conclusion so here's an example:

You'll need to add a using directive to the following assemblies:

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

And here's the hook:

   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);
        }
    }
like image 54
Denys Wessels Avatar answered Oct 02 '22 23:10

Denys Wessels