Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check whether Left mouse button is pressed

Status
Not open for further replies.

sreenath205

Programmer
Dec 9, 2003
17
0
0
IN
Hi

I have a button whose onmouseenter event calls a function say "foo",in foo
i need to check whether the left mouse button is pressed or not?

Code:
<html>
<script>

function foo(){
alert("came")

//How to check here whether the Left button is clicked??



}


</script>

<body>
<input type=button onmouseenter="foo()">

</body>
</html>

Thanks
Sreenath
Edit/Delete Message
 

Here is a small snippet that will alert every time you click the left mouse button on the page. You can change it (obviously) to work with other criteria.

Code:
<script type="text/javascript">
function doNotClick() {
 if (event.button==1) {
  alert('You clicked the left mouse button.')
 }
}
document.onmousedown= doNotClick
</script>

Hope this gets you looking in the right places.

Cheers,
Jeff
 
Sreenath,

onmouseenter is IE only. Do you need a solution that is cross-browser, or IE-only?

If you need only IE to be supported, Jeff's solution will work just fine. If you need FF and otehrs, too, then you would have to use something like this:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] lang="en" xml:lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta http-equiv="content-language" content="en" />
	<title>LMB Test</title>

	<script type="text/javascript">
	<!--

		document.onmousedown = function(evt) {
			try {
				if ((evt && evt.button == 0) || (event && event.button == 1)) alert('LMB pressed!');
			} catch (e) {}
		};

	//-->
	</script>
</head>

<body>
   ...
</body>
</html>

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]
 
Hi

I think i did not present my problem correctly.What i want is FROM the foo function i need to check whether user is pressing the left button ie consider this scenario my mousepointer is outside the button ,i pressed the left mouse button and draged the pointer over the button now foo event will be called and user is still pressing left button.HOW TO KNOW THAT,bcoz a user can simply drag the mouse pointer to the button and still mouse enter t will be triggered

Thanks
 
Sreenath,

You did not answer my question - do you require this to work in any browser other than IE?

If not, check out the MSDN pages for onmouseenter:


And click the "show available properties" link to see how to get the button from the event data.

If you do require it to work outside of IE, then different code would need to be used.

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]
 

Hi Dan

Sorry for not answering your Question,its for IE only.
Thanks for the input.

But MSDN states that

"This(event.button")property is used with the onmousedown, onmouseup, and onmousemove events. For other events, it defaults to 0 regardless of the state of the mouse buttons."

How to over come this??
Thanks
 
You could try using onmousemove instead, then. Just use a boolean flag to test whether the event has already fired or not (simulating an onmouseenter). Here's some logic that should get you started:

- initialise a flag to false
- onmouseout = set flag to false, regardless of its value
- onmousemove =
if flag = false
set flag to true
do button testing code here
end if

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top