Share via


Key Method

Returns a KeyBinding object that represents the specified custom key combination. If the key combination doesn't exist, this method returns Nothing.

expression**.Key(KeyCode**, KeyCode2)

expression Required. An expression that returns a KeyBindings or KeysBoundTo object.

KeyCode    Required Long. A key you specify by using one of the WdKey constants.

KeyCode2    Optional Variant. A second key you specify by using one of the WdKey constants.

Remarks

You can use the BuildKeyCode method to create the KeyCode or KeyCode2 argument.

Example

This example assigns the ALT+F4 key combination to the Arial font and then displays the number of items in the KeyBindings collection. The example then clears the key combinations (returns it to its default setting) and redisplays the number of items in the KeyBindings collection.

CustomizationContext = NormalTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyF4), _
    KeyCategory:=wdKeyCategoryFont, Command:="Arial"
MsgBox KeyBindings.Count & " keys in KeyBindings collection"
KeyBindings.Key(KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyF4)).Clear
MsgBox KeyBindings.Count & " keys in KeyBindings collection"

This example assigns the CTRL+SHIFT+U key combination to the macro named "Macro1" in the active document. The example uses the Key property to return a KeyBinding object so that Word can retrieve and display the command name.

CustomizationContext = ActiveDocument
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
    wdKeyShift, wdKeyU), KeyCategory:=wdKeyCategoryMacro, _
    Command:="Macro1"
MsgBox KeyBindings.Key(BuildKeyCode(wdKeyControl, _
    wdKeyShift, wdKeyU)).Command

This example determines whether the CTRL+SHIFT+A key combination is part of the KeyBindings collection.

Dim kbTemp As KeyBinding

CustomizationContext = NormalTemplate
Set kbTemp = KeyBindings.Key(BuildKeyCode(wdKeyControl, _
    wdKeyShift,wdKeyA))
If (kbTemp Is Nothing) Then MsgBox _
    "Key is not in the KeyBindings collection"

Applies to | KeyBindings Collection Object | KeysBoundTo Collection Object

See Also | BuildKeyCode Method | Command Property