如何使用键盘快捷键打开弹出窗口? [英] How to open popover with keyboard shortcut?

查看:501
本文介绍了如何使用键盘快捷键打开弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用jQuery的键盘快捷键

我想使用以下方法显示弹出窗口快捷键,而不是单击工具栏上的图标。
你有什么好主意吗?
谢谢您的帮助。

I want to display a popover window using a shortcut key instead of clicking the icon on the toolbar. Do you have any good idea? Thank you for your help.

推荐答案

Abody97的答案告诉您如何确定是否已按下某个组合键。如果您不确定如何获取该组合键以显示弹出窗口,则需要此。不幸的是,Safari使得这一过程变得不必要了。

Abody97's answer tells you how to determine if a certain key combo has been pressed. If you're not sure how to get that key combo to show the popover, this is what you need. Unfortunately, Safari makes this needlessly complicated.

在全局脚本中,您需要一个类似以下的函数来显示弹出窗口,并给出ID和ID。应该显示它的工具栏项目:

In the global script, you'll need a function like the following to show a popover, given its ID and the ID of the toolbar item that should show it:

function showPopover(toolbarItemId, popoverId) {
    var toolbarItem = safari.extension.toolbarItems.filter(function (button) {
        return button.identifier == toolbarItemId && button.browserWindow == safari.application.activeBrowserWindow;
    })[0];
    var popover = safari.extension.popovers.filter(function (popover) {
        return popover.identifier == popoverId;
    })[0];
    toolbarItem.popover = popover;
    toolbarItem.showPopover();  
}

您还需要在全局脚本的消息侦听器中调用此函数的代码,如下所示(此示例不假定您已经有消息侦听器):

You'll also need code to call this function in your global script's message listener, like the following (this sample does not assume you already have a message listener in place):

safari.application.addEventListener('message', function (e) {
    if (e.name == 'Show Popover') {
        showPopover(e.message.toolbarItemId, e.message.popoverId);
    }
}, false);

最后,在注入的脚本中,侦听键组合的函数需要调用 dispatchMessage ,如下所示:

Finally, in your injected script, the function that listens for the key combo needs to call dispatchMessage, as below:

safari.self.tab.dispatchMessage('Show Popover', {
    toolbarItemId : 'my_pretty_toolbar_item',
    popoverId     : 'my_pretty_popover'
});

(将其替换为 showPopUp()在Abody97的代码示例中。)

(Stick that in place of showPopUp() in Abody97's code sample.)

注意:如果您只有一个工具栏项和一个弹出窗口(从不打算添加更多),那么它将变得更加简单。假设您已经将弹出窗口分配给Extension Builder中的工具栏项目,则只需使用

Note: If you only have one toolbar item and one popover (and never plan to add more), then it becomes much simpler. Assuming you've already assigned the popover to the toolbar item in Extension Builder, you can just use

safari.extension.toolbarItems[0].showPopover();

代替对 showPopover 的调用全局消息侦听器,并在注入脚本中对 dispatchMessage 的调用中忽略消息值。

in place of the call to showPopover in the global message listener, and omit the message value in the call to dispatchMessage in the injected script.

这篇关于如何使用键盘快捷键打开弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆