I want to detect position of the cursor when the user click on a document.

Here is the kind of code I would like to use :

EventTarget root = (EventTarget) document.getDocumentElement();

root.addEventListener("click", new EventListener() {
    public void handleEvent(Event evt) {
        window.alert(Integer.toString(((DOMMouseEvent) evt).getClientX()));
        window.alert(Integer.toString(((DOMMouseEvent) evt).getClientY()));
    }
}, false);


For some reasons, this code doesn't work on an empty document.
Hoever, if I create an element inside my document, when I click on this
element, the eventListener is triggered :


Element root = document.getDocumentElement();
Element bg = document.createElementNS(svgNS, "rect");

// Create and append to the root a background to catch mouse events
bg.setAttributeNS (null, "width", "100%");
bg.setAttributeNS (null, "height", "100%");
bg.setAttributeNS (null, "fill", "white");
bg.setAttributeNS (null, "id", "bg");
root.appendChild(bg);

EventTarget root = (EventTarget) document.getDocumentElement();

root.addEventListener("click", new EventListener() {
    public void handleEvent(Event evt) {
        window.alert(Integer.toString(((DOMMouseEvent) evt).getClientX()));
        window.alert(Integer.toString(((DOMMouseEvent) evt).getClientY()));
    }
}, false);


I don't really like this solution, I'm sure using this background element
could be avoided.
Do you have any idea how I could achieve that ?

Regards,
Lrbabe

Reply via email to