Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trapping Keystrokes In A Document (Word2000)

    I post the attached because I

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    I guess my first question would be: "why exactly do you want to?"
    It clearly is possible to log keystrokes (witness the increasing number of keylogging spyware) but I guess you have to do this at a global level. I don't know exactly how this works so I can't say whether it is possible to hook that into specific windows only - but I guess you would need to set a low level keyboard hook in Windows and re-route to callback functions of your own design. I suppose what you could do is implement a keyboard hook in the application_documentopen event and unhook it in the documentclose events?
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    > I guess my first question would be: "why exactly do you want to?"
    ... and my first answer is "because someone suggested a Most-Recently-Used type of facility for keystrokes. Their request from to populate a drop-down list with stale or current (ascending/decsending sequence) keystrokes, and use that list to improve familiarity with (to them) useful strokes. As a simple example, they think of a keystroke which they use only once a fortnight, but use it 20 times within a half-hour interval.
    My second answer is that I can dream up interesting things to do if I trap shortcut key combinations. I wrote an expanded document MRUse utility (clobbers MSoft's stupid limit-of-9) and then found I could use its data to re-populate the MyPlaces in the File Open dialogue so that I have not only my DOC&XLS MRUList, but as well have a list of MRUsed folders.
    My third answer is "It bugs me that one can, I gather, do this in an Excel Workbook, but not in a Word document"

    >set a low level keyboard hook in Windows
    If I had kept up with MASM and possibly C, let alone C++, I might be able to do this.

    More than anything else, I worry that I've overlooked some available feature in events and/or classes that someone else has spotted.

    It's not a big deal; I posted the attachment in case there was a glaring solution, and/or so that anyone else had a basis for tooling aorund with what I'd garnered over night.

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    You can do this from VBA using the WinAPI as I mentioned (SetWindowsHookEx being the main function) but it seems an awful lot of effort for not much gain. Particularly with your first example - if they've just used a key combo 20 times in half an hour, either they should remember it, or they should write it down! <img src=/S/grin.gif border=0 alt=grin width=15 height=15> Otherwise, how will they remember that there is a function that remembers keystrokes??
    With the second example, you can get a list of standard keyboard shortcuts and program those specifically.
    I guess the biggest problems I see are:
    1. Any decent virus checker is probably going to query this behaviour, which may freak your user out and/or not be compatible with corporate policy.
    2. You will be checking every key stroke. Not terribly efficient.
    3. Your lovely new code will be rendered entirely obsolete by Office 12 with it's remarkably prescient ribbons (or whatever they are called in the final version). No, really, it will. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    I don't know if this will work for what you're trying to do, but one approach that can effectively result in a document-specific keystroke response is to assign a key combination (say, Ctrl+R) to a global macro (say, "CtrlR"). CtrlR will consist of a single substantive line:
    <pre> Application.Run "DocCtrlR"</pre>

    Then, if you want to have a particular document effectively "trap" Ctrl+R and respond, put the desired response in a "DocCtrlR" macro that is saved in that particular document.

    You'd probably also want to include error-trapping in the global CtrlR macro so that, if the user hit Ctrl+R at a time when the current document didn't include a DocCtrlR macro, the non-result was graceful.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    Rory, thanks for the feedback. I'll delve/dive into SetWindowsHookEx this weekend.

    > how will they remember that there is a function that remembers keystrokes??
    This is probably the main reason for the request. It's akin to having a single document called "passwords.doc", in which we hide our passwords. A single combination for a pop up of those-keys-I-really-should-master is easy to remember. Providing that the list is customizable by the user ("It's OK, I think I've got Ctrl-C, Ctrl-V now"), it seems to make sense. At least to the person asking me to do it, so who am I to argue.

    As an aside, I learned of the F12 last week. Where has that been all my life? made me wish that I'd printed out a list, or better yet, had a list on which I could vote to re-assign weights as i mastered or discarded combinations.

    I agree with your comments about timing. I don't like the idea of inspecting every keystroke, on the off-chance that one of them might be useful. That's why I took the approach of obtaining a list of all assigned combinations and trap only them.

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    > assign a key combination (say, Ctrl+R)
    Steve, thanks for the suggestion.

    Below is pasted a snippet from my attached example.
    I think I did what you are suggesting, in spades.
    At startup (Word) I obtain a list of assigned keys and re-assign each one to my own Macroiii, thereby trapping every assigned keystroke. This works.
    As a penalty I must pre-fabricate enough public Macroiiis to accommodate the maximum permissible (under my system!) number of trapped combinations.
    Having fifty or so extra macros floating around isn't going to impress an end-user.
    <pre>(snip!)Public Sub Mac008()
    MsgBox strMac008
    Application.Run (strMac008)
    End Sub
    Public Sub Mac009()
    MsgBox strMac009
    Application.Run (strMac009)
    End Sub

    Public Function AssignMyKeys(Assigns() As KeyAss)
    Dim lng As Long
    For lng = LBound(Assigns) To UBound(Assigns)
    With Assigns(lng)
    Select Case lng
    Case 0
    strMac000 = .strCommand
    KeyBindings.Add KeyCategory:=.lngCategory, Command:="Mac000", KeyCode:=.lngCode
    Case 1
    strMac001 = .strCommand
    KeyBindings.Add KeyCategory:=.lngCategory, Command:="Mac001", KeyCode:=.lngCode
    Case 2(snip!)</pre>


  8. #8
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    It occurs to me you might be able to combine this with your existing method to trap 90%+ of cases. If you set a keybinding for the Ctrl and Function keys which then enables a hook, logs the next key and disables the hook, you might not have too much overhead? (Not sure how practical that is or how easy to implement!)
    Regards,
    Rory

    Microsoft MVP - Excel

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    > If you set a keybinding for the Ctrl and Function keys which then enables a hook

    GOOD ONE!
    Thanks.
    I must first learn the hook. I know I've got the key-substitution working, my demo proves that. I'll marry the hook to the demo (grossly inefficient, but works), and then set about filtering what's going on.
    Filtering is good, since the user want to use this as a learning device. Optionally they could say "This week I'm going to explore the Alt-Function key combinations and work out what subest might be useful to me; next week Shift-Function". That scenario would make sense, and lends itself to filtering.

  10. #10
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    One problem with keyloggers like this that I haven't found a good solution for:

    OK, you've managed to trap the keys... what next?

    As with intercepting anything, I'd like to do my stuff, and then have Word do whatever it usually does.

    The obvious solution would be SendKeys... Say,
    SendKeys "a"
    if you have intercepted the "a" key.

    But that won't work, because Word will end up in an endless loop and run out of stack space.

    Sure you could try to write some VBA code (Selection.InsertAfter or whatever), but you can bet that you won't be able to mimick whatever Word does perfectly in all situations.
    Say, the Spacebar, Enter, and punctuation trigger AutoCorrect (which I wouldn't know how to do in VBA code), or Tab/Backspace will do weird things with the indent if the proper option is set, and on and on...

    <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15> Klaus

  11. #11
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    My admittedly brief reading on the subject seems to indicate that if you use the CallNextHookEx function, it will then simply pass the keystroke on down the line so it will be processed normally after you have registered it. If I get a bit of time soon I will try and come up with an example but as it's Easter and my wedding anniversary this weekend, it won't be in the next few days! <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Regards,
    Rory

    Microsoft MVP - Excel

  12. #12
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    That would be great! All I can do with API calls is recognize them, and then be overcome by awe and fear.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Happy Easter! Klaus

  13. #13
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    Happy Easter to you too!
    I've just had a quick look (as my wife is out for the evening <img src=/S/grin.gif border=0 alt=grin width=15 height=15>) and have found some suitable code but am having great difficulty getting the actual hook in place. I will keep trying and post back if I get anywhere.
    Regards,
    Rory

    Microsoft MVP - Excel

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    Rory, thanks. I took my first look at it this morning.
    On my second look I remembered to save the enhanced template before starting the test to trap keystrokes.
    On my third attempt I used the hold-down-for-five-seconds button on the laptop.
    It sure traps keystrokes!
    I'll dig into it a little deeper over the next few days. It is almost certainly overkill for what ought to be straightforward in Word and is, apparently so, in Excel.

  15. #15
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Trapping Keystrokes In A Document (Word2000)

    Sounds like you had fun! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    When I run it in Word2002, I can type away merrily - all it does is print to the immediate window if I use a Ctrl, Alt or Shift key. I take it that was not your experience?
    Regards,
    Rory

    Microsoft MVP - Excel

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •