有没有办法为 Microsoft Word 加载项创建新的键绑定?

Is there a way to create new key bindings for Microsoft Word add ins?

我有一个基本的 Microsoft Word 插件,但该应用程序的目标用户是高级用户,他们喜欢使用键盘而不是鼠标来做各种事情。

有没有办法将加载项中的 action/button 绑定到一个键或组合键?除了单击按钮,是否可以通过键绑定触发操作?

我想附上如下内容:

ctrl + l

像这样:


    function hightlightLongestWord() {
        Word.run(function (context) {
            // Queue a command to get the current selection and then
            // create a proxy range object with the results.
            var range = context.document.getSelection();

            
            // This variable will keep the search results for the longest word.
            var searchResults;
            
            // Queue a command to load the range selection result.
            context.load(range, 'text');
            // Synchronize the document state by executing the queued commands
            // and return a promise to indicate task completion.
            return context.sync()
                .then(function () {
                    $("#template-description").text(range.text);
                    // Get the longest word from the selection.
                    var words = range.text.split(/\s+/);
                    var longestWord = words.reduce(function (word1, word2) { return word1.length > word2.length ? word1 : word2; });

                    // Queue a search command.
                    searchResults = range.search(longestWord, { matchCase: true, matchWholeWord: true });

                    // Queue a commmand to load the font property of the results.
                    context.load(searchResults, 'font');
                })
                .then(context.sync)
                .then(function () {
                    // Queue a command to highlight the search results.
                    searchResults.items[0].font.highlightColor = '#FF0000'; // Green
                    searchResults.items[0].font.bold = true;
                })
                .then(context.sync);
        })
        .catch(errorHandler);
    } 

简短回答是。但这不是最实用的。 (这是我知道的在加载项中执行类似操作的唯一方法)

您可以创建一个 javascript 事件处理程序来捕获按键组合。

这里有一些例子: How do I capture a CTRL-S without jQuery or any other library?

这有一个问题,尽管您的加载项需要处于活动状态才能捕捉到按键,因为该加载项是一个旁加载到 word 的网站。因此,如果用户正在写入 Word,然后按 ctrl+l,如果用户没有先单击您的加载项,则不会发生任何事情。

据我所知,覆盖 Words 自己的组合键的唯一方法是通过 VBA 和宏(注意 office online 不支持宏):https://docs.microsoft.com/en-us/archive/blogs/vsod/using-shortcut-keys-to-call-a-function-in-an-office-add-in

但是你可以做一些事情,比如在加载项中有一个按钮:当按下 ctrl 时点击 => 做 smth,当正常点击时 => 做其他事情,当点击下移时 => 做一些超级格式化.. .等...