Older Version Newer Version

Alyce Alyce Oct 25, 2011

**GetLastInputInfo - System Idle Time** See also: [[http://alycesrestaurant.com|Alyce's Restaurant]] [[toc|flat]] This demo was written in response to a question on the [[http://libertybasic.conforums.com/|Community Forum]]. //"I want a function that executes if the user has NOT been working on a PC for 20 mins??"// =Tick Count= The user32 API function **GetTickCount** retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days. (The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.) The syntax for GetTickCount looks like this. [[code format="lb"]] calldll #kernel32, "GetTickCount",_ startTicks as ulong 'ticks since system start [[code]] =GetLastInputInfo= The user32 API function GetLastInputInfo retrieves information about the last user input by keyboard or mouse. It fills a struct with the information. The struct has two members. The first is the size of the struct. This can be retrieved by Liberty BASIC with the LEN() function. The second member will be filled by the function with the tick count at the last user input in the session. The struct looks like this: [[code format="lb"]] struct LASTINPUTINFO,_ cbSize as ulong,_ 'size of struct tickCount as ulong 'tick count at last session input LASTINPUTINFO.cbSize.struct=len(LASTINPUTINFO.struct) [[code]] =Session Idle Time= You can discover the number of ticks the system was idle by subtracting the number retrieved from GetLastInputINfo from the value of the current tick count. The function looks like this. [[code format="lb"]] calldll #user32, "GetLastInputInfo",_ 'time of last input event LASTINPUTINFO as struct,_ 'struct to hold data result as long 'nonzero=success ticksLastInput=LASTINPUTINFO.tickCount.struct [[code]] =Demo= The demo instructs the user to avoid keyboard and mouse input. It uses a timer to activate the routine after five seconds. After five seconds, it retrieves the current tick count with **GetTickCount**. It gets information about the last user input with **GetLastInputInfo**. It retrieves the tick count at last user input from the struct. It subtracts the tick count at last input from the current tick count to find out how many ticks (milliseconds) the system has been idle. (Divide by 1000 to get the value in seconds instead of milliseconds.) [[code format="lb"]] struct LASTINPUTINFO,_ cbSize as ulong,_ 'size of struct tickCount as ulong 'tick count at last session input LASTINPUTINFO.cbSize.struct=len(LASTINPUTINFO.struct) print "Please wait and do not touch keyboard or mouse." timer 5000, [getInfo] wait [getInfo] timer 0 calldll #user32, "GetLastInputInfo",_ 'time of last input event LASTINPUTINFO as struct,_ 'struct to hold data result as long 'nonzero=success ticksLastInput=LASTINPUTINFO.tickCount.struct print "Last input tick count: ";ticksLastInput calldll #kernel32, "GetTickCount",_ startTicks as ulong 'ticks since system start print "Ticks since system start ";startTicks print "Idle ticks: ";startTicks-ticksLastInput [[code]] ---- [[toc|flat]]