Trap which Mouse Button is Clicked

I am trying to trap if the mouse button is currently down or up. I can get it to work in IE but Cannot get it to work in FireFox.

This is my function that I Add to the OnMouseMove event . I simply want to be able to determine if the mouse button is up or down.
Thanks
URGENT

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head >
    <title>Untitled Page</title>

</head>
    <script type="text/javascript">
      function CheckingMouse(e){
    if(navigator.appName == 'Netscape' && e.which == 3){
       document.getElementById('Text1').value = 'DOWN';
      return;
    }else if(navigator.appName == 'Microsoft Internet Explorer' && event.button==2 || navigator.appName == 'Microsoft Internet Explorer' && event.button==1 ){
        document.getElementById('Text1').value = 'DOWN';
     return;
    }
    document.getElementById('Text1').value = 'UP';
    return ;
   }
 
 

    </script>

<body>

  <script type="text/javascript">

   document.onmousemove = CheckingMouse;
   if(navigator.appName == 'Netscape') window.captureEvents(Event.ONMOUSEMOVE);
   window.onmousemove=CheckingMouse;

  </script>
    <input id="Text1" type="text" onclick="javascript:CallNumberCheck();" />
 

</body>
</html>
EazyWorksAsked:
Who is Participating?
 
ZylochConnect With a Mentor Commented:
Unfortunately, I do not know of any easy way of accomplishing this. I am unsure of how e.which is set for onmousemove. If anyone else knows the answer, I'm sure they will help you out. Again, I'm sorry I do not have a solution.

Ted
0
 
ZylochCommented:
It would perhaps be simpler to just use the onmousedown and onmouseup to trap your event:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-us" xml:lang="en-us">
<head>
<title>Trapping Mouse Button Click</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */

function CheckingMouse(evt)
{
    if (!evt)
    {
        evt = window.event;
    }

    var right_click = false;
    if (evt.which)
    {
        right_click = (evt.which == 3 || evt.which == 1);
    }
    else if (evt.button)
    {
        right_click = (evt.button == 2 || evt.button == 1);
    }

    if (right_click)
    {
        document.getElementById('displayField').value = 'DOWN';
    }
    else
    {
        document.getElementById('displayField').value = 'UP';
    }
}

//For Netscape 4
if (document.layers)
{
    document.onmousedown = CheckingMouse;
    document.onmouseup = CheckingMouse;
    window.captureEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP);
}
else
{
    window.onmousedown = CheckingMouse;
    window.onmouseup = CheckingMouse;
}


/* ]]> */
</script>
</head>
<body>
<input id="displayField" type="text" />
</body>
</html>


Unfortunately, I am not too certain what you were trying to do. It seems your code tries to trap only the right mouse button for all browsers other than IE, in which you seem to be matching both the left and the right buttons. In my code, I made it so that both IE and other browsers would detect a mouse button down whether it was left or right. The only mouse button currently that would not register would be the middle mouse button (and this is one of the only reasons why this code should be used. Otherwise, just use a Javascript variable and set it on mousedown and mouseup!)

Ted
0
 
EazyWorksAuthor Commented:
Sorry About that I will explain a little further.
I have some Movable and Resizable Objects on a web page, what happens is if a user moves his/her mouse very quickly and releases the mouse button the mouseup event is missed and the objects continue to move as if you where holding down the mouse button.

I wanted to add some code on the "onmousemove" event while it is running to check to see if the mouse is still has its button depressed and if not stop the drag and re-size event.

I have looked in many places and no one seems to double check to see mouse buttons current state "if the button is still depressed", this therefore could  lead to a situation where the user could loose control of these active elements in the page.

I have managed to easily trap the Mouse state on IE6 but am having difficulties trapping the mouse state in FireFox.

to make it simple all I need is a blank page with one input box that reads the mouse state
"left is Down"
"Right is Down"
"Nothing is Down"

running on the "onmousemove" event.

Please let me know if this makes more since.

Thanks
0
 
EazyWorksAuthor Commented:
Thanks,
0
All Courses

From novice to tech pro — start learning today.