Skip to content

AddShortcut Method

Description

Adds a key binding (accelerator key) for a command. Allows keys in String format.

Syntax

Public Sub AddShortcut( _
    ByVal [Command](#) As Long, _
    ByVal [Shortcut](#) As String _
)

Parameters

  • Command: The command ID which will be invoked when the accelerator is used.
  • Shortcut: String containing shortcut key(s) for the specified command.

Remarks

AddShortcut provides an easier method to add shortcuts (key bindings) for commands by allowing them to be entered in String format rather than needing ASCII codes as with the Add method.

The format for the Shortcut string must not include any spaces if used in combination with a key like "Ctrl". If allowing DoubleKeyShortcuts, a comma (",") must be placed between shortcuts. Reference the code sample below for the correct formatting.

Here is a list of all the "String" names for various key mappings (use the name between quotes and see the samples below):

  • VK_LBUTTON = "Left Button"
  • VK_RBUTTON = "Right Button"
  • VK_CANCEL = "Ctrl+Break"
  • VK_MBUTTON = "Middle Button"
  • VK_BACK = "Backspace"
  • VK_TAB = "Tab"
  • VK_CLEAR = "Clear"
  • VK_RETURN = "Enter"
  • VK_SHIFT = "Shift"
  • VK_CONTROL = "Ctrl"
  • VK_MENU = "Alt"
  • VK_PAUSE = "Pause"
  • VK_CAPITAL = "Caps Lock"
  • VK_ESCAPE = "Esc"
  • VK_SPACE = "Space"
  • VK_PRIOR = "Page Up"
  • VK_NEXT = "Page Down"
  • VK_END = "End"
  • VK_HOME = "Home"
  • VK_LEFT = "Left Arrow"
  • VK_UP = "Up Arrow"
  • VK_RIGHT = "Right Arrow"
  • VK_DOWN = "Down Arrow"
  • VK_SELECT = "Select"
  • VK_PRINT = "Print"
  • VK_EXECUTE = "Execute"
  • VK_SNAPSHOT = "Snapshot"
  • VK_INSERT = "Ins"
  • VK_DELETE = "Del"
  • VK_HELP = "Help"
  • VK_LWIN = "Win"
  • VK_RWIN = "Win"
  • VK_APPS = "Application"
  • VK_MULTIPLY = "Num *"
  • VK_ADD = "Num +"
  • VK_SEPARATOR = "Separator"
  • VK_SUBTRACT = "Num -"
  • VK_DECIMAL = "Num ."
  • VK_DIVIDE = "Num /"
  • VK_F1 = "F1"
  • VK_F2 = "F2"
  • VK_F3 = "F3"
  • VK_F4 = "F4"
  • VK_F5 = "F5"
  • VK_F6 = "F6"
  • VK_F7 = "F7"
  • VK_F8 = "F8"
  • VK_F9 = "F9"
  • VK_F10 = "F10"
  • VK_F11 = "F11"
  • VK_F12 = "F12"
  • VK_NUMPAD0 = "Num 0"
  • VK_NUMPAD1 = "Num 1"
  • VK_NUMPAD2 = "Num 2"
  • VK_NUMPAD3 = "Num 3"
  • VK_NUMPAD4 = "Num 4"
  • VK_NUMPAD5 = "Num 5"
  • VK_NUMPAD6 = "Num 6"
  • VK_NUMPAD7 = "Num 7"
  • VK_NUMPAD8 = "Num 8"
  • VK_NUMPAD9 = "Num 9"
  • VK_NUMLOCK = "Num Lock"
  • VK_SCROLL = "Scrl Lock"
  • VK_ATTN = "Attn"
  • VK_CRSEL = "Crsel"
  • VK_EXSEL = "Exsel"
  • VK_EREOF = "Ereof"
  • VK_PLAY = "Play"
  • VK_ZOOM = "Zoom"
  • VK_NONAME = "No Name"
  • VK_PA1 = "Pa1"
  • VK_OEM_CLEAR = "Oem Clear"

Example

Set KeyBindings = CommandBars.KeyBindings  

KeyBindings.AddShortcut ID_FILE_NEW, "Ctrl+N"  
KeyBindings.AddShortcut ID_FILE_OPEN, "Ctrl+O"  
KeyBindings.AddShortcut ID_FILE_SAVE, "Ctrl+S"  
KeyBindings.AddShortcut ID_EDIT_CUT, "Ctrl+X"  
KeyBindings.AddShortcut ID_EDIT_COPY, "Ctrl+C"  

KeyBindings.AllowDoubleKeyShortcuts = True  
' Example of a double shortcut key  
KeyBindings.AddShortcut ID_EDIT_PASTE, "Ctrl+V, Ctrl+P"  
KeyBindings.AddShortcut ID_FILE_OPEN, "Ctrl+K, Ctrl+Page Up"

See Also


Copyright (c) 1998-2024 Codejock Technologies. All rights reserved.