Solved

Trap which Mouse Button is Clicked

Posted on 2006-07-03
4
256 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
  • 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 250 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now