I am trying to catch mouse position on the entire screen by
dynamically generating mouse click event at every 100 ms. My code
only works for IEs but not any Netscape or Gecko-based browsers. The
following are the problems and I hope that there is someone who can
enlighten me or give me some pointers. Also, my testing code is
attached at the end. And please don't ask me why I am doing this - it
is one of functional requirements by all means that I don't know how
to explain.
For NN4.x:
Since NN4.x doesn't support document.click( ), I use the button click()
instead. I am sure that the click event is generated every 100 ms but
the mouse position is never captured. [x,y] are always [0,0] unless
it is a physical click by the mouse device.
For NN6.x and Gecko-based browsers:
In order to dispatch the event programmaticall y, I first created an
MouseEvent, initialized it and then dispatched it at every 100 ms.
Unfortunately, I have the same result as NN4.x: [x,y] are always [0,0]
unless it is a physical click by the mouse device. Then, I tried to
play around with the event type argument being set in
initMouseEvent( ). I changed the event type from "click" to "push".
In this case, I added an event listener to listen to this PUSH event.
The result remains unchanged. I also tried to change the press count
from 1 to 0. It is also in vain too.
I've read through the W3C DOM 2 Event document:
but I still have no clue to proceed this: capture the mouse positions
on the entire screen. I really hope that there is someone who can
help me out and give me some pointers of it. Hopefully, it is not a
mission impossible. Thanks. The following is my testing code for
reference.
<html>
<head>
<title></title>
<script type="text/JavaScript" language="JavaS cript">
<!--
window.onerror = function(mesg, url, line) {
alert("error: [line " + line +"]: " + mesg + "\n" + url);
return cleanup();
}
window.onunload = cleanup;
function cleanup(e) {
if (typeof(loopid) != "undefined" )
clearInterval(l oopid);
document.onclic k = null;
document.onmous emove = null;
clickStarted = false;
return true;
}
function trackMove(e) {
if (arguments.leng th == 0) e = event;
if (document.layer s) {
document.f.mX.v alue = e.pageX;
document.f.mY.v alue = e.pageY;
}
else {
document.f.mX.v alue = e.clientX;
document.f.mY.v alue = e.clientY;
}
}
function trackClick(e) {
if (arguments.leng th == 0) e = event;
if (document.layer s) {
document.f.mous eX.value = e.pageX;
document.f.mous eY.value = e.pageY;
}
else {
document.f.mous eX.value = e.clientX;
document.f.mous eY.value = e.clientY;
}
return true;
}
function init() {
clickStarted = false;
document.onmous emove = trackMove;
if (document.layer s) {
document.captur eEvents(Event.M OUSEMOVE);
}
}
function startCapturing( e) {
if (clickStarted) {
alert("Already started!");
return true;
}
document.onclic k = trackClick;
if (document.layer s) {
document.captur eEvents(Event.C LICK);
}
clickStarted = true;
return fireClickEvent( );
}
function fireClickEvent( ) {
if (document.all) { // IEs
loopid = setInterval("do cument.fireEven t('onclick');", 100);
}
else if (document.layer s) { // NS4
document.f.btnS tart.onclick = trackClick;
loopid = setInterval("do cument.f.btnSta rt.click();", 100);
}
else if (document.imple mentation.hasFe ature("MouseEve nts", "2.0"))
{
if (typeof(evt) == "undefined" ) {
// create an document event.
evt = document.create Event("MouseEve nts");
}
// initialize the event before first dispatching
evt.initMouseEv ent(
"click", //"push // event type
true,
true,
window,
0, // numbre of times mouse is pressed and released
0,
0,
0,
0,
false,
false,
false,
false,
0,
window
);
//document.addEve ntListener("pus h", trackClick, true);
loopid = setInterval("di spatchClickEven t(evt, document)", 100);
//dispatchClickEv ent(evt, document); // testing purpose.
}
return true;
}
function dispatchClickEv ent(evt, evtTarget) {
evtTarget.dispa tchEvent(evt);
}
//-->
</script>
</head>
<body onload="init()" >
Click on the document and start tracking.
<form name="f">
<input type="button" id="btnCancel" name="btnCancel "
value=" Click Me to Stop Mouse Tracking "
onclick="cleanu p()"><br>
<br><br>
MouseMove X: <input type="text" name="mX" value="0" size="4"><br>
MouseMove Y: <input type="text" name="mY" value="0" size="4"><br>
<br><br>
Click X: <input type="text" name="mouseX" value="0" size="4"><br>
Click Y: <input type="text" name="mouseY" value="0" size="4"><br>
<br><br>
<input type="button" id="btnStart" name="btnStart"
value="Start Click Tracking"
onclick="startC apturing();"><b r>
</form>
</body>
</html>
dynamically generating mouse click event at every 100 ms. My code
only works for IEs but not any Netscape or Gecko-based browsers. The
following are the problems and I hope that there is someone who can
enlighten me or give me some pointers. Also, my testing code is
attached at the end. And please don't ask me why I am doing this - it
is one of functional requirements by all means that I don't know how
to explain.
For NN4.x:
Since NN4.x doesn't support document.click( ), I use the button click()
instead. I am sure that the click event is generated every 100 ms but
the mouse position is never captured. [x,y] are always [0,0] unless
it is a physical click by the mouse device.
For NN6.x and Gecko-based browsers:
In order to dispatch the event programmaticall y, I first created an
MouseEvent, initialized it and then dispatched it at every 100 ms.
Unfortunately, I have the same result as NN4.x: [x,y] are always [0,0]
unless it is a physical click by the mouse device. Then, I tried to
play around with the event type argument being set in
initMouseEvent( ). I changed the event type from "click" to "push".
In this case, I added an event listener to listen to this PUSH event.
The result remains unchanged. I also tried to change the press count
from 1 to 0. It is also in vain too.
I've read through the W3C DOM 2 Event document:
but I still have no clue to proceed this: capture the mouse positions
on the entire screen. I really hope that there is someone who can
help me out and give me some pointers of it. Hopefully, it is not a
mission impossible. Thanks. The following is my testing code for
reference.
<html>
<head>
<title></title>
<script type="text/JavaScript" language="JavaS cript">
<!--
window.onerror = function(mesg, url, line) {
alert("error: [line " + line +"]: " + mesg + "\n" + url);
return cleanup();
}
window.onunload = cleanup;
function cleanup(e) {
if (typeof(loopid) != "undefined" )
clearInterval(l oopid);
document.onclic k = null;
document.onmous emove = null;
clickStarted = false;
return true;
}
function trackMove(e) {
if (arguments.leng th == 0) e = event;
if (document.layer s) {
document.f.mX.v alue = e.pageX;
document.f.mY.v alue = e.pageY;
}
else {
document.f.mX.v alue = e.clientX;
document.f.mY.v alue = e.clientY;
}
}
function trackClick(e) {
if (arguments.leng th == 0) e = event;
if (document.layer s) {
document.f.mous eX.value = e.pageX;
document.f.mous eY.value = e.pageY;
}
else {
document.f.mous eX.value = e.clientX;
document.f.mous eY.value = e.clientY;
}
return true;
}
function init() {
clickStarted = false;
document.onmous emove = trackMove;
if (document.layer s) {
document.captur eEvents(Event.M OUSEMOVE);
}
}
function startCapturing( e) {
if (clickStarted) {
alert("Already started!");
return true;
}
document.onclic k = trackClick;
if (document.layer s) {
document.captur eEvents(Event.C LICK);
}
clickStarted = true;
return fireClickEvent( );
}
function fireClickEvent( ) {
if (document.all) { // IEs
loopid = setInterval("do cument.fireEven t('onclick');", 100);
}
else if (document.layer s) { // NS4
document.f.btnS tart.onclick = trackClick;
loopid = setInterval("do cument.f.btnSta rt.click();", 100);
}
else if (document.imple mentation.hasFe ature("MouseEve nts", "2.0"))
{
if (typeof(evt) == "undefined" ) {
// create an document event.
evt = document.create Event("MouseEve nts");
}
// initialize the event before first dispatching
evt.initMouseEv ent(
"click", //"push // event type
true,
true,
window,
0, // numbre of times mouse is pressed and released
0,
0,
0,
0,
false,
false,
false,
false,
0,
window
);
//document.addEve ntListener("pus h", trackClick, true);
loopid = setInterval("di spatchClickEven t(evt, document)", 100);
//dispatchClickEv ent(evt, document); // testing purpose.
}
return true;
}
function dispatchClickEv ent(evt, evtTarget) {
evtTarget.dispa tchEvent(evt);
}
//-->
</script>
</head>
<body onload="init()" >
Click on the document and start tracking.
<form name="f">
<input type="button" id="btnCancel" name="btnCancel "
value=" Click Me to Stop Mouse Tracking "
onclick="cleanu p()"><br>
<br><br>
MouseMove X: <input type="text" name="mX" value="0" size="4"><br>
MouseMove Y: <input type="text" name="mY" value="0" size="4"><br>
<br><br>
Click X: <input type="text" name="mouseX" value="0" size="4"><br>
Click Y: <input type="text" name="mouseY" value="0" size="4"><br>
<br><br>
<input type="button" id="btnStart" name="btnStart"
value="Start Click Tracking"
onclick="startC apturing();"><b r>
</form>
</body>
</html>
Comment