How do I get a mouse click from a command event in XUL?

It seems that XUL commands and click events are slightly different.

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

My question is, how can I determine which mouse button 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>

      

0


source to share


1 answer


The main thing to keep in mind is that oncommand

triggered for any action that causes a button to be activated, which can include pressing the spacebar when the button has focus, using the keyboard shortcut attached to the button, or clicking the mouse button. The event handler doesn't really have to care about how it was called, just the way it was. (I assume this was a conscious decision to keep the UI consistent.)

The only actions that lead to adding properties buttons Event object are onclick

, ondblclick

, onmousedown

and onmouseup

. You will notice that even if you disable the button onclick

, the event still fires, whereas with oncommand

it it won't.



To get the same functionality as oncommand

you will need to handle both the tags onclick

and onkeydown

making sure the button is still enabled. You can also create a custom button using XBL that has custom events like onleftclick

or onmiddleclick

, which are then returned to oncommand

if not set, but I haven't tried it.

+2


source







All Articles