Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting when the mouse is not moving

Tags:

I am able to find the cursor position. But I need to find out if the mouse is stable. If the mouse wasn't moved for more than 1 minute, then we have to alert the user.

How its possible, are there any special events for this? (Only for IE in javascript)

like image 805
praveenjayapal Avatar asked Mar 04 '09 10:03

praveenjayapal


People also ask

Why is my mouse not moving?

Check that the battery of the mouse is charged. Make sure that the receiver (dongle) is firmly plugged in to the computer. If your mouse and receiver can operate on different radio channels, make sure that they are both set to the same channel.

How do I find my mouse on my laptop?

Windows 10 gives you a fast way to find your mouse pointer by circling it when you hit the CTRL key. Here's how to set it up. In the search box on your task bar, search for Mouse, and select it from the list. Once you're in Mouse settings, select Additional mouse options from the links on the right side of the page.


2 Answers

Set a timeout when the mouse is moved one minute into the future, and if the mouse is moved, clear the timeout:

var timeout; document.onmousemove = function(){   clearTimeout(timeout);   timeout = setTimeout(function(){alert("move your mouse");}, 60000); } 
like image 57
Marius Avatar answered Oct 22 '22 12:10

Marius


Here's a one-and-done function that can check any element for movement:

function mouse (element, delay, callback) {     // Counter Object    element.ms = {};     // Counter Value    element.ms.x = 0;     // Counter Function    element.ms.y = function () {        // Callback Trigger       if ((++element.ms.x) == delay) element.ms.callback(element, element.ms);    };     // Counter Callback    element.ms.callback = callback;     // Function Toggle    element.ms.toggle = function (state) {        // Stop Loop       if ([0, "off"][state]) clearInterval(element.ms.z);        // Create Loop       if ([1, "on"][state]) element.ms.z = setInterval(element.ms.y, 1);    };     // Function Disable    element.ms.remove = function () {        // Delete Counter Object       element.ms = null; return delete element.ms;    };     // Function Trigger    element.onmousemove = function () {        // Reset Counter Value       element.ms.x = -1;    };     // Return    return element.ms; }; 

Usage: mouse(element, delay, callback)

Examples: Make a video player hide the mouse after 5 seconds when idle and fullscreen

let x = mouse(video, 5000, function (a) {    if (document.webkitIsFullScreen) video.style.cursor = "none"; });  x.toggle(1); addEventListener("mousemove", function () {    video.style.cursor = "auto"; }); 

Chat Room AFK (45 Seconds) (assuming you have a chat box and a send message function):

let x = mouse(chatBox, (45e3), function (a) {    chatBox.send({ text: chatBox.username + " is AFK.", italic: true }); });  x.toggle(1); x.addEventListener("mousemove", function () {   chatBox.send({ text: chatBox.username + " is no longer AFK", italic: true }); }); 
like image 30
imaxwill Avatar answered Oct 22 '22 11:10

imaxwill