如何从 XUL 中的命令事件中按下鼠标按钮?

发布于 2024-07-08 07:21:32 字数 757 浏览 7 评论 0原文

看起来 XUL 命令和点击事件有些不同。

虽然我的函数在使用 command 事件时确实被调用,但事件对象不包含 button 属性。

我的问题是:如何在不使用 click 事件的情况下检测按下的鼠标按钮?

可以演示我的问题的最简单的代码:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<window id="yourwindow" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script language="JavaScript">
    var showMeTheButton=function(e)
    {
        alert(e.button);
    }
</script>
<button onclick="showMeTheButton(event)" label="Click me with onclick"></button>
<button oncommand="showMeTheButton(event)" label="Click me with oncommand"></button>
</window>

It seems that the XUL command and click events are somewhat different.

Although my function does get called when using the command event, the event object does not contain the button property.

My question is: how do I detect what mouse button that was pressed without using the click event?

The simplest code that can demonstrate my problem:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<window id="yourwindow" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script language="JavaScript">
    var showMeTheButton=function(e)
    {
        alert(e.button);
    }
</script>
<button onclick="showMeTheButton(event)" label="Click me with onclick"></button>
<button oncommand="showMeTheButton(event)" label="Click me with oncommand"></button>
</window>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衾梦 2024-07-15 07:21:32

要记住的主要事情是,任何导致按钮激活的操作都会触发 oncommand ,其中包括在按钮获得焦点时按空格键,使用附加的键盘快捷键按钮,或用鼠标单击按钮。 事件处理程序实际上并不应该关心它是如何调用的,而只是关心它是如何调用的。 (我想这是一个保持一致的用户界面的有意识的决定。)

导致按钮属性添加到事件对象的唯一操作是 onclickondblclickonmousedownonmouseup。 您会注意到,即使禁用 onclick 按钮,该事件仍然会触发,而使用 oncommand 则不会。

要获得与 oncommand 相同的功能,您需要自己处理 onclickonkeydown 事件,同时确保按钮仍处于启用状态。 您还可以使用 XBL 创建自定义按钮,该按钮具有诸如 onleftclickonmiddleclick 之类的自定义事件,然后如果满足,则回退到 oncommand未设置 - 但我还没有尝试过。

The main thing to keep in mind is that oncommand is fired for any action that results in an activation of the button which can include pressing the space bar when the button has the focus, using a keyboard shortcut attached to the button, or clicking the button with the mouse. The event handler is not really supposed to care how it was called, only that it was. (I imagine this was a conscious decision to keep a consistent user interface.)

The only actions that result in the button property being added to the Event object are onclick, ondblclick, onmousedown, and onmouseup. You'll note that even if you disable your onclick button, the event still fires whereas with oncommand it won't.

To get the same functionality as oncommand, you'd need to handle both onclick and onkeydown events yourself while making sure the button is still enabled. You also might be able to create a custom button using XBL which have custom events like onleftclick or onmiddleclick that then fall back to oncommand if they are not set--but I've not tried it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文