How can you tell if a Visual Studio command was triggered by keyboard or menu?

138 views Asked by At

Is it possible to know if a Visual Studio command was triggered from the keyboard or a menu? I'd like to write a plugin that monitored VS commands and suggested keyboard shortcuts for any commands chosen from a menu.

1

There are 1 answers

0
Sven Amann On BEST ANSWER

It is not possible to determine from the Command itself, how it was triggered. Neither can you get this information from the CommandEvent. This is by design, because Commands can be bound to an arbitrary number of triggers of any kind (including explicit invocation in some code).

What you can do, however, is to register yourself as a listener on all CommandBarButton.OnClick and CommandBarComboBox.OnChange in the tree DTE.CommandBars.Controls. Whenever one of the above events occurs, capture the very next CommandEvent. This is the command immediately triggered by the respective CommandBarControl.

I guess this is sufficient for the scenario you describe. Note, however, that you cannot conclude from this that all other commands are triggered by some shortcut, since commands can be triggered implicitly (through other commands). You can approximate this by checking whether or not a key is pressed, when a command occurs, but this is a rather fragile approach... If someone knows a better approach here, adds are welcome!