Time since last user input

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Time since last user input

Post by Grovkillen » Wed Dec 02, 2020 11:26 am

I would like my server scripts to pause/resumed based on last user interaction/input. I found this but how do I define the size of the structure?

https://docs.microsoft.com/en-us/window ... tinputinfo
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Time since last user input

Post by Grovkillen » Wed Dec 02, 2020 3:02 pm

I managed to get it working using Python. MS is such a bad*ss tool :)

Code: Select all

/*
python_code:
import ctypes, ctypes.wintypes
class LASTINPUTINFO(ctypes.Structure):
    _fields_ = [
      ('cbSize', ctypes.wintypes.UINT),
      ('dwTime', ctypes.wintypes.DWORD),
      ]
PLASTINPUTINFO = ctypes.POINTER(LASTINPUTINFO)
liinfo = LASTINPUTINFO()
liinfo.cbSize = ctypes.sizeof(liinfo)
user32 = ctypes.windll.user32
GetLastInputInfo = user32.GetLastInputInfo
GetLastInputInfo.restype = ctypes.wintypes.BOOL
GetLastInputInfo.argtypes = [PLASTINPUTINFO]
kernel32 = ctypes.windll.kernel32
GetTickCount = kernel32.GetTickCount
GetLastInputInfo(ctypes.byref(liinfo))
print(GetTickCount() - liinfo.dwTime)
*/

LabelToVar>python_code,temp

// idle time in "ticks"... 1tick=1ms?
Let>k=0
Repeat>k
  Add>k,1
  PYExec>temp,idleTime
  Message>idleTime
  Wait>1
Until>k=10
Move your mouse during the time and you'll see that the time gets reset.
Let>ME=%Script%

Running: 15.0.27
version history

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

Re: Time since last user input

Post by Marcus Tettmar » Wed Dec 09, 2020 9:15 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
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Time since last user input

Post by Grovkillen » Thu Dec 10, 2020 4:17 am

Marcus Tettmar wrote:
Wed Dec 09, 2020 9:15 pm
is Timer no help?

https://www.mjtnet.com/manuals/v15/HTML/timer.html
Not really since I want to halt/idle the script when a user (me) make any interaction with the computer (my server).
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Time since last user input

Post by Grovkillen » Mon Dec 11, 2023 1:47 pm

A vbscript based code snippet. I use the IO read of the csrss.exe process. There's multiple sessions open but for me the second seems to be my users input.

Code: Select all

VBSTART
Function IdleComputer()
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_process where name='csrss.exe'")
For Each objItem in colItems
    IdleComputer = IdleComputer & vbNewLine & objItem.ReadOperationCount
Next
End Function
VBEND

Let>REFERENCE_TIMER=0
Let>LAST_INPUT_TICKS_TIME_STAMP=0
Label>START
  VBEval>IdleComputer(),TEMP_LAST_INPUT_BASED_ON_IO
  Trim>TEMP_LAST_INPUT_BASED_ON_IO,TEMP_LAST_INPUT_BASED_ON_IO
  Separate>TEMP_LAST_INPUT_BASED_ON_IO,%CRLF%,TEMP_check
  Let>CURRENT_INPUT_IO_READ=TEMP_check_2
  IfNot>CURRENT_INPUT_IO_READ=LAST_INPUT_IO_READ
    Timer>REFERENCE_TIMER
  Else>
    Timer>LAST_INPUT_TICKS_TIME_STAMP
  Endif>
  Let>LAST_INPUT_IO_READ=CURRENT_INPUT_IO_READ
  Let>USER_IDLE_TIME=LAST_INPUT_TICKS_TIME_STAMP-REFERENCE_TIMER
  If>USER_IDLE_TIME<0
    Let>USER_IDLE_TIME=0
  Endif>
Message>USER_IDLE_TIME
Wait>0.1
Goto>START
Let>ME=%Script%

Running: 15.0.27
version history

Jiyahana
Newbie
Posts: 10
Joined: Thu Jan 18, 2024 10:03 am
Location: India
Contact:

Re: Time since last user input

Post by Jiyahana » Fri Feb 02, 2024 9:05 am

Track timestamps in your script define the structure size based on your server requirements for accurate tracking.

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