By Barbara Han
Some times back, Gary posted a blog talking about “how to set the shortcut key to a customized command in Inventor”, however the code in that sample is quite out-of-date, so I want to give a update and also added one more point that you need to notice:
There are two types of shortcut key - Shortcut and Alias. Inventor API help document mentioned that the accelerator shortcut key can't be duplicate, but the alias shortcut key can. The alias shortcut key is interpreted as "Alt+<key>" (<key> is the letter showing in the Customize –> keyboard window), for instance, "I" alias shortcut key means that you can press Alt+I to bring out that command.
The following is a VBA code sample that assigns the alias shortcut key "I" to a custom command along with the code of deleting the command and custom ribbon. With VB.NET, the code is similar.
Below code is saved VBA Editor’s DocumentProject –>ThisDocument module:
Private WithEvents oBtnDef As ButtonDefinition
Sub AddBtnDefWithShortcut()
Dim oControlDefs As ControlDefinitions
Set oControlDefs = ThisApplication.CommandManager.ControlDefinitions
Dim ClientId As String
ClientId = "{3BB9D239-E672-4F2B-AA33-5B6A6EB74C54}"
'Create our own button command
Set oBtnDef = oControlDefs.AddButtonDefinition("MyControldefinition1", "Internal name for my controldef 1", kShapeEditCmdType, ClientId)
'Assign shortcut key to the button command
oBtnDef.OverrideShortcut = "I"
' Find the drawing ribbon.
Dim oRibbon As Ribbon
Set oRibbon = ThisApplication.UserInterfaceManager.Ribbons("Drawing")
Dim oRibbonTab As RibbonTab
Set oRibbonTab = oRibbon.RibbonTabs.Add("AdskRibbonTab", "AdskRibbonTab", ClientId)
' Add a control to the ribbon tab.
Dim oRibbonPanel As RibbonPanel
Set oRibbonPanel = oRibbonTab.RibbonPanels.Add("My Ribbon", "AdskRibbonPanel", ClientId)
Call oRibbonPanel.CommandControls.AddButton(oBtnDef)
End Sub
Private Sub oBtnDef_OnExecute(ByVal Context As NameValueMap)
MsgBox ("command is running")
End Sub
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
Sub DeleteRibbon()
' Find the drawing ribbon.
Dim oRibbon As Ribbon
Set oRibbon = ThisApplication.UserInterfaceManager.Ribbons("Drawing")
Dim oRibbonTab As RibbonTab
Set oRibbonTab = oRibbon.RibbonTabs.Item("AdskRibbonTab")
Dim oRibbonPanel As RibbonPanel
Set oRibbonPanel = oRibbonTab.RibbonPanels.Item("AdskRibbonPanel")
oRibbonPanel.Delete
oRibbonTab.Delete
End Sub