Search code examples
outlookvstooutlook-addin

VSTO CommandBarButton click modifier


I create a toolbar menu hierarchy in Outlook using VSTO, CommandBarPopup and CommandBarButton. I set a Click handler on the CommandBarButton's and everything works fine, but I would like to be able to do different things in the click handler depending on whether the user right-clicked on the menu, or shift-left-clicked or what not (for example, to include or not include the original message when automatically composing a template reply).

How do I detect which mouse button the user clicked with, or whether shift, alt, or ctrl keys were pressed when the user clicked?


Solution

  • In the event handler you can use the Keyboard.GetKeyStates method which gets the set of key states for the specified key.

    // Uses the Keyboard.GetKeyStates to determine if a key is down.
    // A bitwise AND operation is used in the comparison. 
    if ((Keyboard.GetKeyStates(Key.LeftShift) & KeyStates.Down) > 0)
    {
        // the left shift is pressed now
    }
    

    CommandBars were deprecated with Office 2010. You need to use the Fluent UI for creating a custom UI in the add-in. There are two main ways in VSTO to create a custom UI:

    A context menu is customized using the Fluent UI as well. See Extending the User Interface in Outlook 2010 for more information.

    The Fluent UI is described in depth in the following series of articles: