Assing single keys to macros // intercept keypresses 
Author Message
 Assing single keys to macros // intercept keypresses

Can anyone point me out a solution on how to assign single keypresses, like the standard alphabet keys to macros?

I'm working on an app dealing with complex scripts and it needs special handling of user inputs, other than the methods Word provides for EA langs. I'd need to run various macros or a macro with various args whenever the user presses any common key just as you do when you are typing.

If it can't be carried out with macros (that I'm afraid to, due to security restrictions), do you know a way to hook the whole typing process and intercept all keypresses made in a document (like an onKeyPressed event for the doc itself)?

All hints appreciated,

e-Musty



Thu, 21 Apr 2005 09:35:38 GMT  
 Assing single keys to macros // intercept keypresses
You can't--not in Word. Shortcut keys must contain Ctrl, Alt, or a function
key.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
Word MVP FAQ site: http://www.mvps.org/word


Can anyone point me out a solution on how to assign single keypresses, like
the standard alphabet keys to macros?

I'm working on an app dealing with complex scripts and it needs special
handling of user inputs, other than the methods Word provides for EA langs.
I'd need to run various macros or a macro with various args whenever the
user presses any common key just as you do when you are typing.

If it can't be carried out with macros (that I'm afraid to, due to security
restrictions), do you know a way to hook the whole typing process and
intercept all keypresses made in a document (like an onKeyPressed event for
the doc itself)?

All hints appreciated,

e-Musty



Thu, 21 Apr 2005 09:54:42 GMT  
 Assing single keys to macros // intercept keypresses

Quote:

>> Can anyone point me out a solution on how to assign single
>> keypresses, like the standard alphabet keys to macros?


Quote:
> You can't--not in Word. Shortcut keys must contain Ctrl, Alt, or
> a function key.

Only if you try to define them from the user interface (probably to protect
the innocent).

With a macro (look at .KeyBindings) you can assign any key (and create
mayhem).
If you assign macros to standard alphabet or punctuation keys, you should
be aware that some things won't work properly any more (such as
AutoCorrect).
I've also heard that some ran into trouble when they tried to assign lots
of shortcut keys.
When you do something few other people do with Word, you should always be
prepared for trouble.

Sub AddKeyBinding_a()
 ' This needs to run only once, to add the KeyBinding to the document:
 CustomizationContext = ActiveDocument
 KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
   Command:="myMacro_a", _
   KeyCode:=wdKeyA
End Sub

Sub myMacro_a()
  ' This macro runs each time you type an "a"
  MsgBox "a"
End Sub

Sub RemoveKeyBinding_myMacro_a()
  '  run to remove all KeyBindings to myMacro_a
  Dim myKey As KeyBinding
  For Each myKey In _
    Application.KeysBoundTo(wdKeyCategoryCommand, _
    "myMacro_a")
    myKey.Clear
  Next myKey
End Sub

Regards,
Klaus



Thu, 21 Apr 2005 10:23:54 GMT  
 Assing single keys to macros // intercept keypresses
Well, I don't do VBA, and I think running macros from ordinary alphabetic
keys would be rather weird, anyway.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
Word MVP FAQ site: http://www.mvps.org/word


Quote:

> >> Can anyone point me out a solution on how to assign single
> >> keypresses, like the standard alphabet keys to macros?


> > You can't--not in Word. Shortcut keys must contain Ctrl, Alt, or
> > a function key.

> Only if you try to define them from the user interface (probably to
protect
> the innocent).

> With a macro (look at .KeyBindings) you can assign any key (and create
> mayhem).
> If you assign macros to standard alphabet or punctuation keys, you should
> be aware that some things won't work properly any more (such as
> AutoCorrect).
> I've also heard that some ran into trouble when they tried to assign lots
> of shortcut keys.
> When you do something few other people do with Word, you should always be
> prepared for trouble.

> Sub AddKeyBinding_a()
>  ' This needs to run only once, to add the KeyBinding to the document:
>  CustomizationContext = ActiveDocument
>  KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
>    Command:="myMacro_a", _
>    KeyCode:=wdKeyA
> End Sub

> Sub myMacro_a()
>   ' This macro runs each time you type an "a"
>   MsgBox "a"
> End Sub

> Sub RemoveKeyBinding_myMacro_a()
>   '  run to remove all KeyBindings to myMacro_a
>   Dim myKey As KeyBinding
>   For Each myKey In _
>     Application.KeysBoundTo(wdKeyCategoryCommand, _
>     "myMacro_a")
>     myKey.Clear
>   Next myKey
> End Sub

> Regards,
> Klaus



Thu, 21 Apr 2005 11:27:30 GMT  
 Assing single keys to macros // intercept keypresses

Quote:
> Well, I don't do VBA, and I think running macros from ordinary
> alphabetic keys would be rather weird, anyway.

Hi Suzanne,

That's probably what the developers thought when they didn't allow users to
customize these keys.
But if you want to enter thousands of different characters from some
complex script with a Western keyboard, you'll have to do weird things ;-)

Hi e-Musty,

If you are an experienced programmer, there may be some support to build
your own IME:
Windows DDK, "Win32 Multilingual IME Overview for IME Development"
http://msdn.microsoft.com/library/en-us/appendix/hh/appendix/imeimes_...
sp

You might also ask in microsoft.public.word.international.features, where
the guys and gals that know about complex scripts and IMEs hang out. You
probably would need to describe in greater detail what exactly you are
trying to do.

Greetings,
Klaus



Thu, 21 Apr 2005 12:12:35 GMT  
 Assing single keys to macros // intercept keypresses
First I have tried the way similar to the one you supposed, but for some or
other reason, I've got a "Word cannot change the function of the specified
key" error, however I've even tried the method with non-single keys, like
Ctrl+Alt+D or so. I have also not forget to construct the keycode with
BuildKeyCode.

But anyway, I'll give it one more chance, investigating your source code in
details.

Thanks Klaus


Quote:

> >> Can anyone point me out a solution on how to assign single
> >> keypresses, like the standard alphabet keys to macros?


> > You can't--not in Word. Shortcut keys must contain Ctrl, Alt, or
> > a function key.

> Only if you try to define them from the user interface (probably to
protect
> the innocent).

> With a macro (look at .KeyBindings) you can assign any key (and create
> mayhem).
> If you assign macros to standard alphabet or punctuation keys, you should
> be aware that some things won't work properly any more (such as
> AutoCorrect).
> I've also heard that some ran into trouble when they tried to assign lots
> of shortcut keys.
> When you do something few other people do with Word, you should always be
> prepared for trouble.

> Sub AddKeyBinding_a()
>  ' This needs to run only once, to add the KeyBinding to the document:
>  CustomizationContext = ActiveDocument
>  KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
>    Command:="myMacro_a", _
>    KeyCode:=wdKeyA
> End Sub

> Sub myMacro_a()
>   ' This macro runs each time you type an "a"
>   MsgBox "a"
> End Sub

> Sub RemoveKeyBinding_myMacro_a()
>   '  run to remove all KeyBindings to myMacro_a
>   Dim myKey As KeyBinding
>   For Each myKey In _
>     Application.KeysBoundTo(wdKeyCategoryCommand, _
>     "myMacro_a")
>     myKey.Clear
>   Next myKey
> End Sub

> Regards,
> Klaus



Thu, 21 Apr 2005 21:15:30 GMT  
 Assing single keys to macros // intercept keypresses
Ya, the whole process is very weird. My main problem (and the reason of the
need of macros) is that however Word has RTL reading feature, it does not
really do RTL writing. Installing East-Asian language support for this
purpose is not a way I can choose. I'm gonna to look into the details of the
IME editors you've suggested.


Quote:
> > Well, I don't do VBA, and I think running macros from ordinary
> > alphabetic keys would be rather weird, anyway.

> Hi Suzanne,

> That's probably what the developers thought when they didn't allow users
to
> customize these keys.
> But if you want to enter thousands of different characters from some
> complex script with a Western keyboard, you'll have to do weird things ;-)

> Hi e-Musty,

> If you are an experienced programmer, there may be some support to build
> your own IME:
> Windows DDK, "Win32 Multilingual IME Overview for IME Development"

http://msdn.microsoft.com/library/en-us/appendix/hh/appendix/imeimes_...

- Show quoted text -

Quote:
> sp

> You might also ask in microsoft.public.word.international.features, where
> the guys and gals that know about complex scripts and IMEs hang out. You
> probably would need to describe in greater detail what exactly you are
> trying to do.

> Greetings,
> Klaus



Thu, 21 Apr 2005 21:19:49 GMT  
 Assing single keys to macros // intercept keypresses
Well, I've got it, thanks for the stuff you've attached to your valuable
comments. I have tried to figure out the simple difference between your code
and my one and to get to know why mine wasn't running.

As far as I figured out, you can't use the name of a macru with arguments as
the Command parameter of the KeyBinding object. Nevertheless there is the
CommandParameter attribute for the appropriate Add method, Word doesn' seem
to accept such macros. As the Help discusses it (only in a few word,
unfortunately) there are some built-in function for which you must apply
such a command parameter, but it does not say anything about how to pass
arguments to your own macros this way.

To try to avoid the trouble you've mentioned, I planned to use only a few
macros for the various keys, always passing the keycode of the key pressed
to the macro and process with the value as needed.

By the way, briefly to outline, I'm working on a template/addin that makes
it able to work with an ancient complex script (ancient Hungarian) that
writes right-to-left. So, for example, you would write "Stuff" as "ffutS".
But the order of the words also follows the RTL rule, unlike supported in
Word. That is, suppose you want to write the phrase "cool stuff", you'd
write "ffuts looc" instead of the supported way "looc ffuts".

The application has already been developed for WordBasic by someone, now I
have to migrate it to VB. The guy has been developed a macro for all keys
and assigned it manually to the macros. Opposite to that, I wouldn' like to
bomb Word with 100-200 macros just for the sole purpose of entering a key.
All these macros can be divided into four groups, the bigger of which holds
the 90% of all the macros. These are the same but designed for other letters
than the other. My plan is to consolidate those dozens of macros into one
and instead assigning, for example, "KeyA" to key A, "Key B" to key B,
"KeySmallA" to a and so on, I'd prefer to use only one macro for these, like
"Key(Code as Long)" and assign the keys to this macro programmatically.

At this point, I'm ambiguous if I can do it or I'll have to have the dozens
of similar macros remain in the code (that makes loading of the template
very slow, of course).

If you know anything or any useful resource about passing arguments to
macros programmatically assigned to keys, I'd appreciate it as well.

Rock on,
e-Musty


Quote:

> >> Can anyone point me out a solution on how to assign single
> >> keypresses, like the standard alphabet keys to macros?


> > You can't--not in Word. Shortcut keys must contain Ctrl, Alt, or
> > a function key.

> Only if you try to define them from the user interface (probably to
protect
> the innocent).

> With a macro (look at .KeyBindings) you can assign any key (and create
> mayhem).
> If you assign macros to standard alphabet or punctuation keys, you should
> be aware that some things won't work properly any more (such as
> AutoCorrect).
> I've also heard that some ran into trouble when they tried to assign lots
> of shortcut keys.
> When you do something few other people do with Word, you should always be
> prepared for trouble.

> Sub AddKeyBinding_a()
>  ' This needs to run only once, to add the KeyBinding to the document:
>  CustomizationContext = ActiveDocument
>  KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
>    Command:="myMacro_a", _
>    KeyCode:=wdKeyA
> End Sub

> Sub myMacro_a()
>   ' This macro runs each time you type an "a"
>   MsgBox "a"
> End Sub

> Sub RemoveKeyBinding_myMacro_a()
>   '  run to remove all KeyBindings to myMacro_a
>   Dim myKey As KeyBinding
>   For Each myKey In _
>     Application.KeysBoundTo(wdKeyCategoryCommand, _
>     "myMacro_a")
>     myKey.Clear
>   Next myKey
> End Sub

> Regards,
> Klaus



Thu, 21 Apr 2005 22:09:30 GMT  
 Assing single keys to macros // intercept keypresses

Quote:
> By the way, briefly to outline, I'm working on a template/addin that
makes
> it able to work with an ancient complex script (ancient Hungarian) that
> writes right-to-left.

What operating sytem do you use? In Windows2000 and XP (probably also NT)
you should be able to "lie" to Word about the language, formatting it as
Hebrew or Arabic or another RTL language, and simply type right to left.

You should only need to activate the language support for Hebrew/Arabic (on
my machine in Programs > Microsoft Office > Microsoft Office Tools >
Microsoft Office Language Settings).

Regards,
Klaus



Sun, 24 Apr 2005 02:39:26 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. single keypress macro

2. Can VBA intercept keypress within Excel ?

3. Intercepting keypresses

4. Intercepting Keypress

5. intercept keypress?

6. Intercepting Close event through AutoClose macro ( also intercepting FilePrint and FileSave)

7. assing macro to a button

8. Assing macro with Arg to toolbar ?

9. Putting whole line of text in textfield on single keypress

10. Make Numeric keys "single-stroke" keys


 
Powered by phpBB® Forum Software © phpBB Group