none
How can I add accelerator keys to Word? RRS feed

  • Question

  • Hi all;

    I know that I can add hot keys to the ribbon. But if I press Ctrl-Z in Word, regardless of the ribbon tab showing and state, that calls undo. I'd like to add (optionally) some accelerator keys to Word that when pressed, perform an AddIn function.

    Is there a way to intercept all keystrokes Word is reading?

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Wednesday, September 26, 2018 11:59 AM

Answers

  • Hi David,

    >> I know that I can add hot keys to the ribbon. But if I press Ctrl-Z in Word, regardless of the ribbon tab showing and state, that calls undo.

    According to your description, you would like to intercept pressing a key combination that Microsoft Word is reading such as "Ctrl-Z".

    >> Is there a way to intercept all keystrokes Word is reading?

    To intercept such keyboard shortcuts, you can use the KeyBoard Shortcut component (ADXKeyBoardShortcut).

    Then add it onto the add-in module, set the ShortcutText property to the shortcut you need to intercept and handle the OnAction event.

    So, the code below creates ADXKeyBoardShortcut components intercepting the FileSave command:

    List<ADXKeyboardShortcut> fileSaveShortcuts = null;
    private void AddinModule_AddinInitialize(object sender, EventArgs e)
    {
        object param = Type.Missing;
        Word.KeysBoundTo wordKeys = WordApp.get_KeysBoundTo(
            Word.WdKeyCategory.wdKeyCategoryCommand, "FileSave", ref param);
        List<ADXKeyboardShortcut> shortcutProcessors = 
            new List<ADXKeyboardShortcut>(wordKeys.Count + 1);
        for (int i = 1; i <= wordKeys.Count; i++)
        {
            ADXKeyboardShortcut shortcut = new ADXKeyboardShortcut(this.components);
            shortcut.ShortcutText = wordKeys[i].KeyString;
            shortcut.Action += new ADXAction_EventHandler(FileSaveShortcut_Action);
            shortcutProcessors.Add(shortcut);
        }
        Marshal.ReleaseComObject(wordKeys);
    }
     
    void FileSaveShortcut_Action(object sender)
    {
        MessageBox.Show("You've pressed " + (sender as ADXKeyboardShortcut).ShortcutText);
    }

    For more information, please review the following link:

    How to intercept clicking an Office Ribbon control or command bar button

    Hopefully it helps you. Please feel free to ask any questions.

    And I noticed that another question that you asked on MSDN:

    Where is ConfigurationManager pulling GetSection() values from?

    Could you please mark the replies(Include your solution) as answers if they helped and help us close the thread?

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    • Marked as answer by DavidThi808 Thursday, September 27, 2018 11:04 AM
    Thursday, September 27, 2018 6:55 AM
    Moderator

All replies

  • Hi David,

    >> I know that I can add hot keys to the ribbon. But if I press Ctrl-Z in Word, regardless of the ribbon tab showing and state, that calls undo.

    According to your description, you would like to intercept pressing a key combination that Microsoft Word is reading such as "Ctrl-Z".

    >> Is there a way to intercept all keystrokes Word is reading?

    To intercept such keyboard shortcuts, you can use the KeyBoard Shortcut component (ADXKeyBoardShortcut).

    Then add it onto the add-in module, set the ShortcutText property to the shortcut you need to intercept and handle the OnAction event.

    So, the code below creates ADXKeyBoardShortcut components intercepting the FileSave command:

    List<ADXKeyboardShortcut> fileSaveShortcuts = null;
    private void AddinModule_AddinInitialize(object sender, EventArgs e)
    {
        object param = Type.Missing;
        Word.KeysBoundTo wordKeys = WordApp.get_KeysBoundTo(
            Word.WdKeyCategory.wdKeyCategoryCommand, "FileSave", ref param);
        List<ADXKeyboardShortcut> shortcutProcessors = 
            new List<ADXKeyboardShortcut>(wordKeys.Count + 1);
        for (int i = 1; i <= wordKeys.Count; i++)
        {
            ADXKeyboardShortcut shortcut = new ADXKeyboardShortcut(this.components);
            shortcut.ShortcutText = wordKeys[i].KeyString;
            shortcut.Action += new ADXAction_EventHandler(FileSaveShortcut_Action);
            shortcutProcessors.Add(shortcut);
        }
        Marshal.ReleaseComObject(wordKeys);
    }
     
    void FileSaveShortcut_Action(object sender)
    {
        MessageBox.Show("You've pressed " + (sender as ADXKeyboardShortcut).ShortcutText);
    }

    For more information, please review the following link:

    How to intercept clicking an Office Ribbon control or command bar button

    Hopefully it helps you. Please feel free to ask any questions.

    And I noticed that another question that you asked on MSDN:

    Where is ConfigurationManager pulling GetSection() values from?

    Could you please mark the replies(Include your solution) as answers if they helped and help us close the thread?

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    • Marked as answer by DavidThi808 Thursday, September 27, 2018 11:04 AM
    Thursday, September 27, 2018 6:55 AM
    Moderator
  • Thursday, September 27, 2018 11:04 AM