Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trap which Mouse Button is Clicked

Posted on 2006-07-03
4
Medium Priority
?
265 Views
Last Modified: 2008-03-06
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>
0
Comment
Question by:EazyWorks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 17032914
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
 

Author Comment

by:EazyWorks
ID: 17032961
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
 
LVL 36

Accepted Solution

by:
Zyloch earned 1000 total points
ID: 17033078
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
 

Author Comment

by:EazyWorks
ID: 17033255
Thanks,
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question