Solved

Trap which Mouse Button is Clicked

Posted on 2006-07-03
4
262 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult 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…

707 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