I am trying to create a script (later a program) to run a program if there is no activity for an hour. i know that you could use WaitKeyDown and test for any input, however that would be an extremely long code.
Is there a simpler way of doing this.
Thanks for any suggestions,
Joejr
newbie needs help with testing for user activity
Moderators: Dorian (MJT support), JRL
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
This script will monitor if a user has moved the mouse or pressed the spacebar. (I figured the spacebar is the most used key on the keyboard.)
The NoActiveCounter will kick in and start counting the seconds of inactivity when the mouse hasn't moved and the spacebar hasn't been pressed. If the user moves the mouse or presses the spacebar, the NoActiveCounter will be reset to 0 and start over when the user becomes inactive again.
See my notes in the script.
I hope this helps
The NoActiveCounter will kick in and start counting the seconds of inactivity when the mouse hasn't moved and the spacebar hasn't been pressed. If the user moves the mouse or presses the spacebar, the NoActiveCounter will be reset to 0 and start over when the user becomes inactive again.
See my notes in the script.
Code: Select all
//<-- Dialog and OnEvent> Can be deleted
//Dialog for display purposes only
Dialog>Dialog1
Caption=User Active Monitor
Width=250
Height=74
Top=CENTER
Left=CENTER
Label= ,16,8,true
EndDialog>Dialog1
show>Dialog1
//Exit Sript if Dialog is Closed
OnEvent>WINDOW_NOTOPEN,User Active Monitor,2,Exit
//--> Dialog and OnEvent> Can be deleted
//Set inactive time - 3600 sec = 1 hour
Let>NoActiveTime=3600
//Count Seconds of inactivity
Let>NoActiveCounter=0
label>CheckIfUserActiveLoop
//Get Current Cursor Position
gcp>XPOS1,YPOS1
wait>1
//Check if Cursor has been mooved
gcp>XPOS2,YPOS2
//Check if Spacebar has been pressed
lib>User32,GetKeyState,SPACEBAR_STATE,32
//Start Counter
//If mouse position hasn't changed and spacebar hasn't been pressed
//in the last second
IF>{(%XPOS2%=%XPOS1%) AND (%YPOS2%=%YPOS1%) AND (%SPACEBAR_STATE%=0)}
add>NoActiveCounter,1
if>%NoActiveCounter%>%NoActiveTime%
//User has been inactive for more than an hour if NoActiveCounter is greater than NoActiveTime
mdl>You have been inactive for over an hour!
//Do something...
endif
//<-- Can be deleted
//Display how long user has been inactive in Dialog (Display purpose Only)
let>Dialog1.msLabel1=User Inactive for %NoActiveCounter% sec.
rda>Dialog1
//--> Can be deleted
ELSE
//User is active - Reset NoActiveCounter to 0
Let>NoActiveCounter=0
//Reset spacebar Key State if it was pressed
if>SPACEBAR_STATE=1
sen>SPACE
Press Backspace
endif
//<-- Can be deleted
//Display User is Active in Dialog (Display purpose Only)
let>Dialog1.msLabel1=User Active
rda>Dialog1
//--> Can be deleted
ENDIF
goto>CheckIfUserActiveLoop
srt>Exit
I hope this helps
A suggested improvement
If found the basic idea of this script very useful. However, it often happens that the user is just scrolling a websites, or using the arrows to navigate etc.
You can add OnEvent-s the check these, I did it this way:
//left arrow
OnEvent>KEY_DOWN,VK37,0,ActiveUser
//right arrow
OnEvent>KEY_DOWN,VK39,0,ActiveUser
//down
OnEvent>KEY_DOWN,VK40,0,ActiveUser
//up
OnEvent>KEY_DOWN,VK38,0,ActiveUser
//Right mouse
OnEvent>KEY_DOWN,VK2,0,ActiveUser
//Left mouse
OnEvent>KEY_DOWN,VK1,0,ActiveUser
//space
OnEvent>KEY_DOWN,VK32,0,ActiveUser
SRT>ActiveUser
Let>NoActiveCounter=0
Wait>1
END>ActiveUser
You can add OnEvent-s the check these, I did it this way:
//left arrow
OnEvent>KEY_DOWN,VK37,0,ActiveUser
//right arrow
OnEvent>KEY_DOWN,VK39,0,ActiveUser
//down
OnEvent>KEY_DOWN,VK40,0,ActiveUser
//up
OnEvent>KEY_DOWN,VK38,0,ActiveUser
//Right mouse
OnEvent>KEY_DOWN,VK2,0,ActiveUser
//Left mouse
OnEvent>KEY_DOWN,VK1,0,ActiveUser
//space
OnEvent>KEY_DOWN,VK32,0,ActiveUser
SRT>ActiveUser
Let>NoActiveCounter=0
Wait>1
END>ActiveUser