Mouse Cursor Position

How to get a real cursor position? (not the click position, I know about TouchInput.x || TouchInput.y)
I found the solution!

Code:
    TouchInput._onMouseMove = function(event) {
            var x = Graphics.pageToCanvasX(event.pageX);
            var y = Graphics.pageToCanvasY(event.pageY);
            this._onMove(x, y);
            console.log(x+' '+y);
    };
It's a bit complicated and depends on what exactly you want.

1. Do you want to get the position when the user clicks, or all the time?
2. Do you want to get the position relative to the game's window, or relative to the game's canvas?

1.
When the user clicks
(TouchInput.x and TouchInput.y)
Code:
document.addEventListener('mousedown', getMousePosition);

All the time
Code:
document.addEventListener('mousemove', getMousePosition);
document.addEventListener('mouseenter', getMousePosition); // In case the user doesn't move the mouse.

2.
Relative to the game's window

Code:
function getMousePosition(e) {
    var x = e.pageX;
    var y = e.pageY;
}

Relative to the game's canvas (TouchInput.x and TouchInput.y)
Code:
function getMousePosition(e) {
    var x = Graphics.pageToCanvasX(e.pageX);
    var y = Graphics.pageToCanvasY(e.pageY);
}


What's the difference between game window and game canvas?
By default, the window and the canvas have the same size. The window's size is adjusted to the canvas' size. This means that both are the same by default; our code can't detect if the mouse is on the window's title bar (which is the bar at the top, where the Close, Minimize, and Restore buttons are).

When you resize the window, it's possible that you'll see black bars (can also be white) either above and below, or left and right. Those bars belong to the window, but not the canvas. The canvas is where the game is rendered.


Edit:
I was a bit late, but I'll leave this around for the sake of completion.

Original thread: https://forums.rpgmakerweb.com/threads/mouse-cursor-position.95309/