首页 > 解决方案 > 有没有办法为 Microsoft Word 加载项创建新的键绑定?

问题描述

我有一个基本的 Microsoft Word 插件,但该应用程序的目标用户是喜欢使用键盘而不是鼠标进行各种操作的高级用户。

有没有办法将加载项中的操作/按钮绑定到键或组合键?可以通过键绑定来触发操作,而不是单击按钮吗?

我想附上类似的东西:

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);
    } 

标签: ms-wordoffice-jsoffice-addins

解决方案


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

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

以下是一些示例: 如何在没有 jQuery 或任何其他库的情况下捕获 CTRL-S?

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

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

但是,您可以执行以下操作:在加载项中有一个按钮:当使用 ctrl 向下单击时 => 执行 smth,当正常单击时 => 执行其他操作,当单击 shift 时 => 执行一些超级格式化...等等...


推荐阅读