Issue
"What is the best method to assign a shortcut key to a command in inventor programmatically via the api? I can see on the command definition there are default shortcut key and override shortcut key properties but which one should I set?"
Solution
The following VBA sample demonstrates how to assign a shortcut key to a customized button added to assembly Panel Bar.
Please note the following several points:
If the shortcut key has been consumed by another command, you can’t use it again for your own command, otherwise you will get a runtime error. For example Ctrl+A is used by the “Select All” command, so you can’t use “Ctrl+A” again. You can get a list of all commands and the shortcut keys consumed by Inventor through the “Copy to clipboard” command which is on the keyboard page of the Customize dialog (Tools>Customize¡).
The DefaultShortcut property can be used to set the command alias, but can’t be used for setting the accelerator, you should use OverrideShortcut instead. The user interface won’t delete the controls you added, so you need to remove them manually from the user interface.
If the shortcut key is used by you, you can’t use it again even if you delete the corresponding control from UI, unless you reset all keys from the keyboard page on the customize dialog.
Shortcuts of kAcceleratorShortcut type are created by defining the OverrideShortcut to an allowed non-alphanumeric character, for example “Alt+U”, but those are defined as key combinations by Inventor, like “Ctrl+C” or “Alt+H”, you can’t use.
Private WithEvents oBtnDef As ButtonDefinition
Sub AddBtnDefWithShortcut()
Dim oControlDefs As ControlDefinitions
Set oControlDefs = ThisApplication.CommandManager.ControlDefinitions
'Create our own button command
Set oBtnDef = oControlDefs.AddButtonDefinition("MyControldefinition1", "Internal name for my controldef 1", kShapeEditCmdType)
'Assign shortcut key to the button command
oBtnDef.OverrideShortcut = "Alt+A"
' Find the main assembly command bar.
Dim oAsmCmdBar As CommandBar
Set oAsmCmdBar = ThisApplication.UserInterfaceManager.CommandBars.Item("AMxAssemblyPanelCmdBar")
' Add a control to the command bar.
Call oAsmCmdBar.Controls.AddButton(oBtnDef)
End Sub
You can delete the button command created by above code, like this:
Sub DeleteControlDef()
Dim oControlDefs As ControlDefinitions
Set oControlDefs = ThisApplication.CommandManager.ControlDefinitions
Dim oDef As ControlDefinition
Set oDef = oControlDefs.Item("Internal name for my controldef 1")
oDef.Delete
End Sub