Keyhook event handling at Word Document level

Hi,


We are on VSTO Customizations for Word 2003. We binded the XML Schema to the word document. At some of the nodes we need to capture the keys entered by the user and based on that we need to do some action.

For that we have written KeyHook events.

The following link is the source of our reference

http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/

For that we added filters and allowing the keyhook event only being fired based on the matching filters (or keys for which we enable firing).

The keyhook event is getting fired when user presses a key (which matches filter conditions set) at the document window. At the same time we have our own Action panes. Suppose if user enter something there in Action pane controls the keyhook event is getting fired actually it should not be. Is there any easy way to solve this issue

Regards,

Jeevanantham.S



Answer this question

Keyhook event handling at Word Document level

  • ourti

    Thanks for the reply.

    I tried to use GetFocus within hook procedure. Now i am able to differenciate the Word Window and other forms or user controls in action pane.

    Issue is that when i insert word "Comments" in the document and if i type keys in comments window the keyevent is getting fired. But it should not be. Is there anyway to identfy the windows like "comment" and etc... (the word features allow user to type except main document)... and skip them getting fired.

    It would be great if there is any work around.

    Waiting for your reply.

    Regards,

    Jeevanantham.S


  • Qumber Abbas

    Hi Jeevanantham,

    I'd suggest calling GetFocus from within your hook proc. This will enable you to determine what window the keystrokes are being sent to so you can decide whether your hook proc should take action.

    Sincerely,

    Geoff Darst

    Microsoft VSTO Team


  • saderogba

    Hi,

    Our VSTO Word Project is basically an XML Editor kind of thing. In that we have attribute pane wherein which we are showing our user controls to show elements and attribute values and we are opening some Windows forms externally for the user to edit something.

    functionallity is

    1. with in main document in some tags if user enters Shift + Enter keys( basically word line break) we need to insert <Br> (line break ) tags

    2. with in main document in some type of tags we need to restrict continous space and tab keys.

    for that we used keyhook event which is getting fired when the above said keys fired using filters.

    Functionality is working, when user works with in main document window.

    Issue is that if user types the same characters in other windows like in external forms, Attribute window, and in word Comments, Font Name combo in tool bar and etc...

    As you mentioned using api to get current window and comparing with main document window i some what restricted the keyhandler firing for External forms, Attributes Panes... but not for the Word in built windows.

    Example:

    if user adds a Comment ( Insert->Comment ) and types these characters ( shift + enter , space , or tab) in the comment window , the keyhandler is getting fired and putting some <Br> tags inside the document window somewhere.

    I believe this thread is more specific with my issue.

    Regards,

    Jeevanantham


  • Gilles Champagne

    Hi Jeevanatham,

    Comment isn't a window, which is why this doesn't work. There are probably several other features like Comment and you don't want to get into the business of trying to figure out what they are and then work around them (assuming you can--I don't know whether it is even possible to figure out when a comment is being edited). If what you want to do is going to work, you are going to need to be able to tell whether you are clicking on text within your XML tags. You can certainly tell when the keystrokes are going to the document (i.e Keyboard hook & GetFocus, or subclass the document window(s)), but beyond that, things get complicated.

    I believe you can deduce whether a keystroke is going to your XML by grabbing the Range from the Selection and then checking to see whether that Range is equal to or within any of the Ranges of your XML nodes. You'll want to check this yourself--that is an area of the Word OM I'm not particularly familiar with.

    Anyway, the real question is whether you can get away with doing this in your keyboard hook. I would be extremely skeptical that it would; my guess is that *any* Word OM calls in your hook would cripple your keyboard performance. You can certainly try it, but if you do, be sure to stress test your code with a very large number of nodes (or limit your design to only deal with a very small number of nodes) and take your weakest machine into account.

    A better approach might be to do this work as part of a SelectionChange handler. Whenever selection changes, find out if the current selection Range is equal to or with an XML node. Depending on the result of this test, transition some state (if appropriate). Then when your keyboard hook (or subclass) gets called and you confirm that keystrokes are going to the document, you can check to see what state you are in and then take the appropriate action.

    Hope that gives you some ideas.

    Geoff Darst

    Microsoft VSTO Team


  • SEK05

    Hi

    I would suggest the KeyHook is application wide and as such You need to distinguish which area of the application you are currently within which is a little harder, I am keen to know what specifically you are trying to stop/start in this scenario.

    Regards



  • Keyhook event handling at Word Document level