Assign Keyboard Shortcut to Macro using control definitions within same Macro.

Assign Keyboard Shortcut to Macro using control definitions within same Macro.

ankush.mhettarAVDWK
Contributor Contributor
275 Views
3 Replies
Message 1 of 4

Assign Keyboard Shortcut to Macro using control definitions within same Macro.

ankush.mhettarAVDWK
Contributor
Contributor

Hi,

Is this Possible to Assign Keyboard Shortcut to Macro using control definitions within same Macro Code.

And not by assign shortcut to macro using customize command.

 

for example please find code below,

Dim oCD As ControlDefinition
ThisApplication.CommandManager.ControlDefinitions.Item("INDEX")
oCD.DefaultShortcut = 0

 

When is try to run code, code fails at this line

ThisApplication.CommandManager.ControlDefinitions.Item("INDEX")

as I am not familiar with Macros or ilogic, I do not know what exactly to be written in this section ("INDEX").

 

Please help me out to learn this.

 

Thank you.

 

0 Likes
276 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @ankush.mhettarAVDWK.  I do not believe a VBA macro can assign its own keyboard shortcut to itself, within it own code.  At least not the first time around.  The macro must already exist, before you can assign a keyboard shortcut to be able to run it.  This is like the question about which came first, the chicken, or the egg.  However, if you edited the code of the macro after it was already created, to add some code in there which would attempt to set that keyboard shortcut, that might work, but would basically be pointless then.  I assume it would be possible to create the entire macro, then set its keyboard shortcut immediately after creating it, all from outside of the macro itself, using some other code type automation process, but that's probably getting a bit over board.

 

Edit:  Another thing to keep in mind is that, shortcuts are somewhat environment specific, because the same keyboard shortcut may be used to do different things while in different environments.  You may want to check if the shortcut you are about to assign to something is already being used by some other process/tool.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

ankush.mhettarAVDWK
Contributor
Contributor

Hi @WCrihfield,

I have a macro to run external ilogic in which I am trying to add the code mentioned above. I added keyboard shortcut to this macro using customize user command. But I want to know is there any by chance we could add the shortcut Key to macro in its own code.

For example please see below snap, using those if we could assign may be.

 

ankushmhettarAVDWK_0-1670567109569.png

 

Thank You.

0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor

Hi @ankush.mhettarAVDWK.  I just tried this with one of my existing VBA macros, but I could not get it to work for me.  I could get it to not throw an error sometimes when I set some values to the DefaultShortcut property, but then after I have ran the macro normally, which would have set the shortcut, when I check the Customization dialog, I still not not see that the shortcut assigned to that macro, so it is not being recognized, and not working.

Dim CDs As ControlDefinitions
Set CDs = ThisApplication.CommandManager.ControlDefinitions
Dim CD As ControlDefinition
Set CD = CDs.Item("macro:ModuleName.SubName")
CD.DefaultShortcut = "k"

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes