3
votes

I have tried using the hook method for shortcut keys in my Word addin as I found on here Using shortcut keys to call a function in an Office Add-in Although this works that it calls my method, it does however write the letter/number of the shortcut into the document. So if my shortcut is say ctrl+2, it will write "2" into my document after my method has run. The really stange thing is that if you were to press ctrl+2 in Word when the addin isn't loaded, it doesnt write "2" into the document, so see no reason why it should when running the addin. Does anyone know how to get round this issue or have any other methods of Word VSTO addin shortcuts? I am writting my addin in VS 2012 and using Word 2010.

1
You're calling return (int)CallNextHookEx(_hookID, nCode, wParam, lParam) everytime, so the event of the Keydown will get bubbled to other listeners (like the document). Try to return 1 if you don't want the event to bepublished further.Matthias Müller
Many thanks Matthias. That works. As an extra question, c# tell me I shouldn't be using AppDomain.GetCurrentThreadId() any more as its obsolete (although it appears to still work). I tried to replace this with System.Threading.Thread.CurrentThread.ManagedThreadId instead, but (which is what the MS site says is it's replacement, but this doesn't work. Any ideas what i should be doing here?crookie
I'll reply as answerMatthias Müller

1 Answers

2
votes

First question: You're calling return (int)CallNextHookEx(_hookID, nCode, wParam, lParam) everytime, so the event of the Keydown will get bubbled to other listeners (like the document). Try to return 1 if you don't want the event to be published further.

About ThreadId: What is the error exactly? I'm not sure how VSTO is implementing synchronizationcontext handling, see for example here: VSTO: invoking on main Excel thread, or How can SynchronizationContext.Current of the main thread become null in a Windows Forms application?