如何从 XUL 中的命令事件中按下鼠标按钮?
看起来 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要记住的主要事情是,任何导致按钮激活的操作都会触发
oncommand
,其中包括在按钮获得焦点时按空格键,使用附加的键盘快捷键按钮,或用鼠标单击按钮。 事件处理程序实际上并不应该关心它是如何调用的,而只是关心它是如何调用的。 (我想这是一个保持一致的用户界面的有意识的决定。)导致按钮属性添加到事件对象的唯一操作是
onclick
、ondblclick
、onmousedown
和onmouseup
。 您会注意到,即使禁用onclick
按钮,该事件仍然会触发,而使用oncommand
则不会。要获得与
oncommand
相同的功能,您需要自己处理onclick
和onkeydown
事件,同时确保按钮仍处于启用状态。 您还可以使用 XBL 创建自定义按钮,该按钮具有诸如onleftclick
或onmiddleclick
之类的自定义事件,然后如果满足,则回退到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
, andonmouseup
. You'll note that even if you disable youronclick
button, the event still fires whereas withoncommand
it won't.To get the same functionality as
oncommand
, you'd need to handle bothonclick
andonkeydown
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 likeonleftclick
oronmiddleclick
that then fall back tooncommand
if they are not set--but I've not tried it.