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 programmatically, 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:
http://www.w3.org/TR/DOM-Level-2-Eve...ts-EventTarget
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="JavaScript">
<!--
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(loopid);
document.onclick = null;
document.onmousemove = null;
clickStarted = false;
return true;
}
function trackMove(e) {
if (arguments.length == 0) e = event;
if (document.layers) {
document.f.mX.value = e.pageX;
document.f.mY.value = e.pageY;
}
else {
document.f.mX.value = e.clientX;
document.f.mY.value = e.clientY;
}
}
function trackClick(e) {
if (arguments.length == 0) e = event;
if (document.layers) {
document.f.mouseX.value = e.pageX;
document.f.mouseY.value = e.pageY;
}
else {
document.f.mouseX.value = e.clientX;
document.f.mouseY.value = e.clientY;
}
return true;
}
function init() {
clickStarted = false;
document.onmousemove = trackMove;
if (document.layers) {
document.captureEvents(Event.MOUSEMOVE);
}
}
function startCapturing(e) {
if (clickStarted) {
alert("Already started!");
return true;
}
document.onclick = trackClick;
if (document.layers) {
document.captureEvents(Event.CLICK);
}
clickStarted = true;
return fireClickEvent();
}
function fireClickEvent() {
if (document.all) { // IEs
loopid = setInterval("document.fireEvent('onclick');", 100);
}
else if (document.layers) { // NS4
document.f.btnStart.onclick = trackClick;
loopid = setInterval("document.f.btnStart.click();", 100);
}
else if (document.implementation.hasFeature("MouseEvents", "2.0"))
{
if (typeof(evt) == "undefined") {
// create an document event.
evt = document.createEvent("MouseEvents");
}
// initialize the event before first dispatching
evt.initMouseEvent(
"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.addEventListener("push", trackClick, true);
loopid = setInterval("dispatchClickEvent(evt, document)", 100);
//dispatchClickEvent(evt, document); // testing purpose.
}
return true;
}
function dispatchClickEvent(evt, evtTarget) {
evtTarget.dispatchEvent(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="cleanup()"><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="startCapturing();"><br>
</form>
</body>
</html>