有没有办法为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);
} 

简短回答是。但这并不是最实用的。(这是我所知道的在插件中做类似事情的唯一方法(

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

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

不过,这有一个问题,即您的外接程序需要处于活动状态才能抓住按键,因为外接程序是一个侧面加载到word的网站。所以,若用户正在向Word写入内容,然后点击ctrl+l,那个么若用户并没有首先点击您的加载项,则不会发生任何事情。

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

然而,您可以在加载项中执行类似"拥有一个按钮"的操作:当按下ctrl=>执行smth,当正常单击时=>在单击shift down时执行其他操作=>进行一些超级格式化。。。等等。

最新更新