newbie needs help with testing for user activity

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Joejr
Newbie
Posts: 6
Joined: Fri Aug 28, 2009 9:06 am
Location: WA. USA

newbie needs help with testing for user activity

Post by Joejr » Sun Oct 25, 2009 10:36 pm

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.:oops:

Thanks for any suggestions,
Joejr

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Oct 26, 2009 3:21 pm

Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Thu Oct 29, 2009 5:22 pm

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.

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

gulyasan
Newbie
Posts: 1
Joined: Thu Dec 13, 2012 12:22 pm

A suggested improvement

Post by gulyasan » Thu Dec 13, 2012 12:27 pm

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

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts