bosojo
bosojo

Reputation: 11

Run Batch Script only once if Mouse movement happen for first time |Batch Script |

I have a scenario where I want to detect the mouse movement when movement happens with certain timing slot and it is a first-time movement happened of the mouse when I logged in then catch the current date and time and store to file

Below code works fine:

@echo off
for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
for /f "tokens=1" %%u in ('time /t') do set t=%%u
if "%t:~1,1%"==":" set t=0%t%

For /f "tokens=1,2,3,4,5 delims=/. " %%a in ('date/T') do set dt=%%c-%%b-%%d

set logfile=C:\Users\vvoor\OneDrive\Desktop\wirite\n\login_time_%dt%.txt
echo Login Time : %d% %t% %time:~-5,2% >> %logfile%

This above works fine as expected gives me date and time

But I want the script should automatically trigger and capture the date and time when I log in for the first time and move his mouse for the first time

The time slot is between 12:00 to 2:00 => if mouse movement happen for first time within given time slot then capture current date and time to a text file

Upvotes: 1

Views: 1180

Answers (1)

Stephan
Stephan

Reputation: 56190

cmd has no method to use the mouse. I used Npocmaka's answer to a similar question from here

The batch file Mouse.bat (written by Npocmaka) creates a mouse.exe, which you can use to get the mouse position (and a lot more stuff not needed for your task):

mouse position

which will give you a string like 514x312

First thing is to capture the mouse position into a variable (no need to split it into x and y coordinates, as you just want to compare for equality later)

Then do a simple loop and compare the current position to the saved position:

@echo off
for /f %%a in ('mouse.exe position') do set "initialPos=%%a"
:loop
timeout 2 >nul
for /f %%a in ('mouse.exe position') do set "currentPos=%%a"
if "%currentPos%" == "%initialPos%" goto :loop
echo Mouse has been moved.

Create a scheduled task ("new task", not "simple new task") to run it within the desired time frame.

Upvotes: 3

Related Questions