Get mousebuttonstate in NS

S

Silvio Bierman

Hello,

In an event handler for onmousemove I want to know if a mousebutton is
currently prtessed. In IE "event.button == 1" works fine for me. In NS I
always get event.button == 0 in onmousemove event if the mousebutton is
pressed.

How do I get the mousebutton state?

TIA

Silvio Bierman
 
J

Janwillem Borleffs

Silvio said:
In an event handler for onmousemove I want to know if a mousebutton is
currently prtessed. In IE "event.button == 1" works fine for me. In
NS I always get event.button == 0 in onmousemove event if the
mousebutton is pressed.

How do I get the mousebutton state?

By adding a listener for the mousedown event:
document.addEventListener("mousedown", showButtonFunction, false);


JW
 
D

DU

Silvio said:
Hello,

In an event handler for onmousemove I want to know if a mousebutton is
currently prtessed. In IE "event.button == 1" works fine for me. In NS I
always get event.button == 0 in onmousemove event if the mousebutton is
pressed.

How do I get the mousebutton state?

TIA

Silvio Bierman


You must query the button property of the created event object once
you've created an event listener in Mozilla-based browsers.
"0" is the left button, "1" is the middle button and "2" is the right
button.

E.g.:

function init()
{
document.addEventListener("mousedown", showButtonFunction, false);
}

function showButtonFunction(evt)
{
document.getElementById("idParg").style.visibility = "visible";
var txtMouseButton = evt.button == "0" ? "left" : evt.button == "1" ?
"middle" : "right";
document.getElementById("idMouseButton").firstChild.nodeValue =
txtMouseButton;
}
// this code could be further improved and made more robust
</script>

</head>

<body onload="init();">

<p id="idParg" style="visibility:hidden;">You just have clicked the
button of your <span id="idMouseButton">&nbsp;</span> mouse.</p>

Tested and working without a problem in Mozilla 1.6 beta, NS 7.1 and
K-meleon 0.8.2.

DU
 
S

Silvio Bierman

OK guys,

That is what I thought. I have solved it that way but thought there might be
a more elegant way of querying the button states during any other event. I
now use a document level event that sets/clear global button-down flags for
use in other event handlers, not nice but it works.

Thanks,

Silvio Bierman
 
T

Thomas 'PointedEars' Lahn

Silvio said:
In an event handler for onmousemove I want to know if a mousebutton is
currently prtessed. In IE "event.button == 1" works fine for me. In NS I
always get event.button == 0 in onmousemove event if the mousebutton is
pressed. How do I get the mousebutton state?

Use the "which" property instead and be sure to pass and use
a reference to the local event object ("event" in an intrinsic
HTML event handler attribute) in the case the IE-proprietary
global "event" object is undefined.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top