Keyboard Hooks is not working in Ms Word 2013

I installed a low level keyboard Hooks in Ms Word-Addin project and captured "Enter" , "Tab" , "Shift+Tab" keys


which works fine in Word 2007 and 2010 (32-Bit/64-Bit),But in Ms Word-2013 does not trigger any keyboard hooks.
I tried several solutions which provided on different blogs but problem still remains.


Kindly let me know if you have proper solution for this.


Who created the add-in? Those are the people you should contact.
Stefan Blom
Microsoft 365 Word MVP since 2005
Volunteer Moderator (Office)
MS 365, Win 11 Pro
~~~~
Please note that I do not work for Microsoft
MVP program info: https://mvp.microsoft.com/
~~~~

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Actually I developed not installed an add-in for Ms Word, in that add-in i am using keyboard hook event (lowlevel API) , keyboard hook event is working fine for Ms Word 2007/2010,
but in Ms Word 2013 that keyboard hook event is not working (does not trigger in Ms Word 2013).

The main keyboard hooks routine look like this:


private void SetKeyboardHook()
        {
            // Create an instance of the HookProc delegate.
            keyboardHookProcedure = new HookProc(KeyboardHookProc);
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHookProcedure, GetModuleHandle(curModule.ModuleName), 0);
            }
        }
#region DLLImports

// Hook procedure callback type.
// SetWindowsHookEx is used to install a thread-specific hook.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

// UnhookWindowsHookEx is used to uninstall the hook.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool UnhookWindowsHookEx(int idHook);

// CallNextHookEx is used to pass the hook information to the next hook procedure in the chain.
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);

// GetModuleHandle is used when calling SetWindowsHookEx for a keyboard hook.
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string lpModuleName);

#region Nested type: HookProc

private delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

#endregion



 private int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam) 
        {
            
// Per docs, if nCode < 0, the hook procedure must pass the 
            // message to CallNextHookEx function without further processing
            // and should return the value returned by CallNextHookEx. 
            if (nCode < 0)
            {
                return CallNextHookEx(hookHandle, nCode, wParam, lParam);
            }
            if ((wParam == (IntPtr) WM.KEYDOWN) || (wParam == (IntPtr) WM.SYSKEYDOWN))
            {
                // The lparam that Windows passes us is a pointer to a KbDllHookStruct.
                var kbDllHookStruct = (KbDllHookStruct) Marshal.PtrToStructure(lParam, typeof (KbDllHookStruct));

                // -- Contrl + C Press Key Hook
                if ((Control.ModifierKeys & Keys.Control) == Keys.Control && kbDllHookStruct.vkCode == (int) Keys.C)
                {
                    //do something when Ctrl + C key Press
                }
                // -- Shift + Enter Press Key Hook
                else if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift && kbDllHookStruct.vkCode == (int)Keys.Return)
                {

                }
                // -- Control + Enter Press Key Hook
                else if ((Control.ModifierKeys & Keys.Control) == Keys.Control && kbDllHookStruct.vkCode == (int)Keys.Return)
                {

                }
                // -- Alt + Enter Press Key Hook
                else if ((Control.ModifierKeys & Keys.Alt) == Keys.Alt && kbDllHookStruct.vkCode == (int)Keys.Return)
                {

                }
                else if ((Control.ModifierKeys & Keys.Alt) == Keys.Alt && kbDllHookStruct.vkCode == (int)Keys.Tab)
                {

                }
                // -- Enter Press Key Hook
                else if (kbDllHookStruct.vkCode == (int) Keys.Return)
                {
                    try
                    {
                        //do something when EnterKey Press
                    }
                    catch
                    {                        
                    }
                }
                // -- Shift + Tab Press Key Hook
                else if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift && kbDllHookStruct.vkCode == (int) Keys.Tab)
                {
                    try
                    {
//do something when Shift + Tab key press
                    }
                    catch 
                    {                        
                    }
                }
                // -- Tab Press Key Hook
                else if (kbDllHookStruct.vkCode == (int) Keys.Tab)
                {
                    try
                    {
//do something when tabkey press
                    }
                    catch
                    {
                    }
                }
                else if (wParam == (IntPtr) WM.SYSKEYDOWN && kbDllHookStruct.vkCode == (int) Keys.F11)
                {
                    return 1;
                }
            }
            var callNextHookEx = 0;
            try
            {
                // Ensure that we don't break the hook chain.
                callNextHookEx = CallNextHookEx(hookHandle, nCode, wParam, lParam);
            }
            catch{}            
            
            return callNextHookEx;
        }


The Above code is properly working in Word 2007/2010, but this is not working in Word 2013.


Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Sorry, I can't assist you with your code. Maybe MSDN would be the best place to ask. See http://social.msdn.microsoft.com/Forums/en-US/home.
Stefan Blom
Microsoft 365 Word MVP since 2005
Volunteer Moderator (Office)
MS 365, Win 11 Pro
~~~~
Please note that I do not work for Microsoft
MVP program info: https://mvp.microsoft.com/
~~~~

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated April 15, 2024 Views 21 Applies to: