MZ Get Position of Mouse while outside window

Hi guys, I am writing a method that gets data about the current tile the Mouse (or Touch Input) is currently hovering over, using TouchInput._x and TouchInput._y (I've also tried TouchInput.x and TouchInput.y; it works similarly) as arguments for the $gameMap.canvasToMapX method to determine which tile to get info for.

But if the Mouse is outside of the Game Window, I want to return a 0 for all data. What's actually happening in practice is it returns the data at the last point on the Game Window that the mouse was at before moving it off the Game Window - TouchInput._x and ._y aren't updated anymore until returning to the Game Window.

Even stranger, it seems that if I hold the mouse button down while moving off the Game Window, it will continue to update correctly with every movement (even outside the window), until I release the mouse button.

Is there any way I can get the current mouse position, regardless of clicking or holding the mouse, even while the mouse is offscreen? Alternatively, is there any graceful (not super-expensive) way to continually update TouchInput.x/y while the mouse is outside the game window?
Node JS used in MV/MZ seems to not have native methods to obtain cursor position, maybe this
package will help you:

Hi there!
I was just looking at something related today 😮

Maybe you can try the html approach for this and use the mouse events.
https://www.w3schools.com/tags/ref_eventattributes.asp
Then add your functions on the eventListeners of the methods.
A Document Object Model (a.k.a. DOM) Event is bound to an element, so I think Soulrender is correct here in saying you'll need an extension (looks promising, haven't tried it myself).

When you click inside the window, on the current document element, I think it registers all movement until the button is released as events for that element. If you click outside, there is no DOM element associated with the game window to capture the event, so the game does not "see" it by default.
Oh, now that @caethyril says...
Maybe I have misunderstood your question. When you mean outside the game window, you mean the red part?
Spoiler
lk5cYIs.png
If yes, then it's like caethyril said.
@Wavelength I just re-read your post and it seems you might actually be interested in assigning listeners to the document's mouseenter and mouseleave events. These should fire when the cursor is moved into, or out of, the document area, i.e. the game canvas.

You could try something along these lines:
Spoiler: Suggestion
JavaScript:
(() => {
'use strict';

  window.mouseHere = false;    // global "mouseHere" variable

  // Functions to fire when events are triggered
  const onEnter = function(event) { mouseHere = true; };
  const onLeave = function(event) { mouseHere = false; };
  const onMove  = function(event) { mouseHere = true; };

  // Assign functions to events
  document.addEventListener('mouseenter', onEnter);
  document.addEventListener('mouseleave', onLeave);
  document.addEventListener('mousemove',  onMove);

})();
This should keep a variable, mouseHere in this case, updated with the current status of the cursor. (I included a mouseMove event as well just to be safe, not sure if that's necessary.) 😮‍💨
@caethyril : not needed by technical POV, but this event is essential, because classic JS (used on websites) is capable to get X and Y of cursor mouse.

Now to think of... I also I'm not sure... Because in classic JS to get X and Y of mouse is clientX and clientY
@caethyril : not needed by technical POV, but this event is essential, because classic JS (used on websites) is capable to get X and Y of cursor mouse.

Now to think of... I also I'm not sure... Because in classic JS to get X and Y of mouse is clientX and clientY
clientX|Y are properties of a MouseEvent; MZ (and probably MV, haven't checked) uses the pageX|Y properties to fetch coordinates instead.
Thank you so much to everyone who's helped so far!
And how cool! I was just about to respond and follow up to everything today (sorry for not doing it sooner), but it seems like mouseenter and mouseleave might be a more expedient way to solve my particular need?

@Wavelength I just re-read your post and it seems you might actually be interested in assigning listeners to the document's mouseenter and mouseleave events. These should fire when the cursor is moved into, or out of, the document area, i.e. the game canvas.

You could try something along these lines:
Spoiler: Suggestion
JavaScript:
(() => {[/INDENT][/INDENT]
[INDENT][INDENT]'use strict';[/INDENT][/INDENT]
[INDENT][INDENT][/INDENT][/INDENT]
[INDENT][INDENT]  window.mouseHere = false;    // global "mouseHere" variable[/INDENT][/INDENT]
[INDENT][INDENT][/INDENT][/INDENT]
[INDENT][INDENT]  // Functions to fire when events are triggered[/INDENT][/INDENT]
[INDENT][INDENT]  const onEnter = function(event) { mouseHere = true; };[/INDENT][/INDENT]
[INDENT][INDENT]  const onLeave = function(event) { mouseHere = false; };[/INDENT][/INDENT]
[INDENT][INDENT]  const onMove  = function(event) { mouseHere = true; };[/INDENT][/INDENT]
[INDENT][INDENT][/INDENT][/INDENT]
[INDENT][INDENT]  // Assign functions to events[/INDENT][/INDENT]
[INDENT][INDENT]  document.addEventListener('mouseenter', onEnter);[/INDENT][/INDENT]
[INDENT][INDENT]  document.addEventListener('mouseleave', onLeave);[/INDENT][/INDENT]
[INDENT][INDENT]  document.addEventListener('mousemove',  onMove);[/INDENT][/INDENT]
[INDENT][INDENT][/INDENT][/INDENT]
[INDENT][INDENT]})();
This should keep a variable, mouseHere in this case, updated with the current status of the cursor. (I included a mouseMove event as well just to be safe, not sure if that's necessary.) 😮‍💨

If I were to add the listeners for mouseenter and mouseleave, and assign the mouseleave function in particular to something that would change TouchInput._x and ._y to 0, then would that allow me to return 0 for a Plugin Command that references the current TouchInput._x/._y, while returning the mouse's current location on the game window if the player leaves the game window and then returns to it before using that Plugin Command?

It sounds like this will be possible but I just want to confirm before I start (re-)coding using this approach.
If I were to add the listeners for mouseenter and mouseleave, and assign the mouseleave function in particular to something that would change TouchInput._x and ._y to 0, then would that allow me to return 0 for a Plugin Command that references the current TouchInput._x/._y, while returning the mouse's current location on the game window if the player leaves the game window and then returns to it before using that Plugin Command?
I would not recommend changing the TouchInput coordinates here, since it seems unnecessary and might potentially mess with other stuff, e.g. a mouselook plugin. 😅

Instead, you could do something like my example to set a flag tracking whether the cursor is inside the window, then check that flag in your plugin command, e.g.
JavaScript:
return mouseHere ? [TouchInput.x, TouchInput.y] : [0, 0];
I.e. "return [x, y] coordinates if the cursor is inside the window, or [0, 0] otherwise". Obviously you can rephrase that as necessary for your plugin's structure~ 😮‍💨
I would not recommend changing the TouchInput coordinates here, since it seems unnecessary and might potentially mess with other stuff, e.g. a mouselook plugin. 😅

Instead, you could do something like my example to set a flag tracking whether the cursor is inside the window, then check that flag in your plugin command, e.g.
JavaScript:
return mouseHere ? [TouchInput.x, TouchInput.y] : [0, 0];
I.e. "return [x, y] coordinates if the cursor is inside the window, or [0, 0] otherwise". Obviously you can rephrase that as necessary for your plugin's structure~ 😮‍💨
Oh, that's a very good point. Would be better to use a variable to track whether the mouse in onscreen, and then return either 0's, or the value of TouchInput._x & _y, based on the value of that variable. Thanks for the catch!

Original thread: https://forums.rpgmakerweb.com/threads/get-position-of-mouse-while-outside-window.131501/