Capturing the keyboard key that was pressed 
Author Message
 Capturing the keyboard key that was pressed

Hi

Using Word 2002:

I'm trying to capture the key (or combination eg: Shift + J) value that the
user pressed and then run a macro, as I'm setting up some keybindings.

Does anyone know what the method is used to capture this keyboard value, if
it's possible?

TIA
Michelle



Fri, 24 Dec 2004 12:16:46 GMT  
 Capturing the keyboard key that was pressed

Quote:

> Hi

> Using Word 2002:

> I'm trying to capture the key (or combination eg: Shift + J) value that the
> user pressed and then run a macro, as I'm setting up some keybindings.

> Does anyone know what the method is used to capture this keyboard value, if
> it's possible?

> TIA
> Michelle

Hi Michelle,

Do you really want to run a macro each time the user presses "J"?

  CustomizationContext = ActiveDocument

  KeyBindings.Add _
    KeyCategory:=wdKeyCategoryMacro, _
    Command:="ShiftJ", _
    KeyCode:=BuildKeyCode(wdKeyShift, wdKeyJ)

But if you intercept common letters like this, some things in Word may not work properly any more (spell checker, AutoCorrect...).
Perhaps if you describe what you want to achieve, there may be a better solution?

You'd need a macro "Sub ShiftJ()", with
    Selection.InsertAfter "J"
to insert the "J", and your own code.

And if you want to get rid of the KeyBinding:

  CustomizationContext = ActiveDocument

  Application.KeyBindings.Add _
  KeyCode:=BuildKeyCode(wdKeyShift, wdKeyJ), _
  KeyCategory:=wdKeyCategoryDisable, _
  Command:="ShiftJ"

or

  Dim myKey As KeyBinding
  For Each myKey In _
    Application.KeysBoundTo(wdKeyCategoryMacro, _
    "ShiftJ")
        myKey.Clear
  Next myKey

Regards, Klaus
Word MVP



Fri, 24 Dec 2004 13:01:33 GMT  
 Capturing the keyboard key that was pressed
Hi Klaus

Thanks for your feedback and all your code, that was also helpful.

Yes, I really do want to capture when the J key is pressed, or any other key
for that matter.

I have a particular custom style which I will put some information into that
I don't want deleted, typed over, etc by the user.

I have a small generic procedure that will determine the keystroke/s
pressed.  If the location of the cursor is not sitting in the area that has
the custom-style that I have created, I will let the key carry out what it's
normal method is.  eg: the user presses the letter J, f10, etc.. any key.
Otherwise, the key will not carry out its normal function.

Therefore, I don't wish to hardcode any key value, but rather ascertain the
Key that was pressed, return its hex code.  If the code matches one that has
been filled into a local array, I will assign the generic macro. The reason
I need to check the value, as there are some specific keys which should be
left out and let to perform their normal function.

I hope that this information better explains what I'm trying to do.

Look forward to your advice.
Thanks again
Michelle



Fri, 24 Dec 2004 14:00:50 GMT  
 Capturing the keyboard key that was pressed
Hi Michelle,

Interesting idea! The ability to protect text (in some style) has often been requested, and the possibility of having a key do separate things depending on the current style opens some interesting possibilities, too.

I have never tried to intercept so many keys. Hope you won't run into problems (other than the ones I mentioned).

Can't give you much advice. My guess is that you really will have to create a separate KeyBinding and write a separate macro for each key. At least I can't imagine at the moment how a "generic procedure" for all keys might work (except possibly with API calls, but that is something I don't know anything about).

Regards, Klaus

Quote:

> Hi Klaus

> Thanks for your feedback and all your code, that was also helpful.

> Yes, I really do want to capture when the J key is pressed, or any
> other key for that matter.

> I have a particular custom style which I will put some information
> into that I don't want deleted, typed over, etc by the user.

> I have a small generic procedure that will determine the keystroke/s
> pressed.  If the location of the cursor is not sitting in the area that has
> the custom-style that I have created, I will let the key carry out what it's
> normal method is.  eg: the user presses the letter J, f10, etc.. any key.
> Otherwise, the key will not carry out its normal function.

> Therefore, I don't wish to hardcode any key value, but rather ascertain
> the Key that was pressed, return its hex code.  If the code matches one
> that has been filled into a local array, I will assign the generic macro.
> The reason I need to check the value, as there are some specific keys
> which should be left out and let to perform their normal function.

> I hope that this information better explains what I'm trying to do.

> Look forward to your advice.
> Thanks again
> Michelle



Fri, 24 Dec 2004 16:26:15 GMT  
 Capturing the keyboard key that was pressed
Thanks again Klaus for your information, I always appreciate input from an
MVP.

I will investigate API calls.  Not something I've done before, therefore
it's worth investigating.

thanks again
Michelle



Sat, 25 Dec 2004 05:49:48 GMT  
 Capturing the keyboard key that was pressed
Hi Michelle,

I've done something very similar to what you are trying in the development
of a legislative drafting system for the State of Nevada without using any
API calls. I capture every keystroke and run a sub based on each key.
Drop me a line and I'll be happy to help.

BTW it has been working just fine for 3 years starting with Word 97 (we're
now using Word 2K).

Steve
--
~~~~~~~~~~~~~~~~~~~~~~
Stephen Lang
401 S. Carson Street
Carson City, NV 89703
775.684.6830
GMT-8
slangatlcb state nv us
~~~~~~~~~~~~~~~~~~~~~~
To respond via e-mail:


Quote:
> Thanks again Klaus for your information, I always appreciate input from an
> MVP.

> I will investigate API calls.  Not something I've done before, therefore
> it's worth investigating.

> thanks again
> Michelle



Tue, 28 Dec 2004 06:55:50 GMT  
 Capturing the keyboard key that was pressed

Quote:

> Hi Michelle,

> I've done something very similar to what you are trying in the
> development of a legislative drafting system for the State of Nevada
> without using any API calls. I capture every keystroke and run a
> sub based on each key.
> Drop me a line and I'll be happy to help.

> BTW it has been working just fine for 3 years starting with Word 97
> (we're now using Word 2K).

Hi Steve,

That's what I originally suggested. It's good to hear that this method
works reliably even with a lot of KeyBindings.

Greetings, Klaus



Tue, 28 Dec 2004 08:57:25 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Keyboard Key pressed?

2. Scalextric car model - capturing arrow key presses

3. QBasic High resolution Time capture of pressed key

4. Capture arrow key press when an option group has the focus

5. capture the key pressed

6. Capturing Key press in a control.

7. capture tab key press

8. Capturing the Tab key-press in a c++ ocx embedded in a vb form

9. Capture keys pressed in WebBrowser Control

10. how do we capture the Tab Key Press event in a text box

11. Keyboard: How do you capture Win keys?

12. Without Pressing Keyboard I need to Press Enter In VB6 Code

 

 
Powered by phpBB® Forum Software