Jump to content

How to detect system inactivity?


seanhart
 Share

Recommended Posts

Would anyone know how to detect system inactivity? I know some programs have the option of kicking in after x minutes of inactivity and I was wondering if there is an easy way of reading that from within AutoIt. I'm assuming DllCall would be needed.

I could write a function to detect lack of mouse movement (and have done that before) but I wanted something a little slicker.

Thanks!

Sean

Link to comment
Share on other sites

Would anyone know how to detect system inactivity? I know some programs have the option of kicking in after x minutes of inactivity and I was wondering if there is an easy way of reading that from within AutoIt. I'm assuming DllCall would be needed.

I could write a function to detect lack of mouse movement (and have done that before) but I wanted something a little slicker.

Thanks!

Sean

I'm fairly sure the only way to detect inactivity is to monitor keyboard and mouse events (C#) http://expertanswercenter.techtarget.com/e...i984317,00.html ;) If there's a nicer way I'm also intrested in finding out!

I did some searching and apparently monitoring window and controle changing can also work: (VB) http://support.microsoft.com/?id=128814

Edited by Idea
Link to comment
Share on other sites

I'm fairly sure the only way to detect inactivity is to monitor keyboard and mouse events (C#) http://expertanswercenter.techtarget.com/e...i984317,00.html ;) If there's a nicer way I'm also intrested in finding out!

I did some searching and apparently monitoring window and controle changing can also work: (VB) http://support.microsoft.com/?id=128814

OK thanks, I guess I'll stick with my mouse movement detection for the moment.

Link to comment
Share on other sites

OK thanks, I guess I'll stick with my mouse movement detection for the moment.

give me a few minutes... going to borrow some code from another script and i'll have a script for you that watches keyboard and mouse for inactivity. it will require the beta, or _IsPressed() function to be included.
Link to comment
Share on other sites

give me a few minutes... going to borrow some code from another script and i'll have a script for you that watches keyboard and mouse for inactivity. it will require the beta, or _IsPressed() function to be included.

Ah... thanks, I always forget to check the beta docs ;) .

It looks like _IsPressed () uses DllCall to do it's magic, I'm assuming I could use this function in 3.1.1 without needing the beta?

Link to comment
Share on other sites

give me a few minutes... going to borrow some code from another script and i'll have a script for you that watches keyboard and mouse for inactivity. it will require the beta, or _IsPressed() function to be included.

here ya go. this is a combination of some code that gafrost wrote to watch keyboard entry for a secret password, and a script i contributed to watch mouse for inactivity. currently it just watches to see if 10 seconds pass without mouse or keyboard input, and sends a msgbox when that occurs. beta is required on this one.

dim $oldpos[2]
dim $pos[2]
#include <Misc.au3>
HotKeySet("{Esc}","_Exit")
Dim $s_keys[117] = [116, _
"01","02","04","05","06","08","09","0C","0D","10", _
"11","12","13","14","1B","20","21","22","23","24", _
"25","26","27","28","29","2A","2B","2C","2D","2E", _
"30","31","32","33","34","35","36","37","38","39", _
"41","42","44","45","46","47","48","49","4A","4B","4C", _
"4D","4E","4F","50","51","52","53","54","55","56","57","58","59", _
"5A","5B","5C","60","61","62","63","64","65","66", _
"67","68","69","6A","6B","6C","6D","6E","6F","70", _
"71","72","73","74","75","76","77","78","79","7A", _
"7B","7C","7D","7E","7F","80H","81H","82H","83H","84H", _
"85H","86H","87H","90","91","A0","A1","A2","A3","A4","A5"]
$dll = DllOpen("user32.dll")

$pos = MouseGetPos()
$start = TimerInit()
While 1
    Sleep ( 100 )
          For $y = 1 To $s_keys[0]
            If _IsPressed($s_keys[$y], $dll) Then 
                $start = TimerInit()
            EndIf
            Next
    $oldpos[0] = $pos[0]
    $oldpos[1] = $pos[1]
    $pos = MouseGetPos()
    if Not $oldpos[1] <> $pos[1] Or $oldpos[0] <> $pos[0] Then
        $start = TimerInit()
    EndIf
    if TimerDiff($start) > 10000 Then
        MsgBox(0,"inactive","you have not pressed any keys or used your mouse for 10 seconds")
        $start = TimerInit()
    EndIf
WEnd
Func _Exit()
DllClose($dll)
Exit
EndFunc
Link to comment
Share on other sites

@Cameronsdad

I tried compiling and executing this script, waited 5 mins doing nothing and never got the msgbox. If I right clicked on tray icon it says the script is paused. The only thing going is my cdplayer which should not affect this.

This is not important as I am just playing around with other peoples examples.


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Thanks for that cameronsdad, but I'm concerned about missing a key press. I was doing some testing with the following code:

while 1
if _IsPressed (31) then
msgbox (0, "debug", "The '1' key was pressed")
EndIf
Sleep (1000)
WEnd

If I press the key during the sleep it gets missed, only if I'm holding it down during the _IsPressed call does it seem to work. Am I missing something?

Second comment, would it be possible to use the "GetKeyboardState" function in DllCall? This appears to read all the keys at once, thus you could simply match two variables to check if something has changed. My programming knowledge isn't enough to get it working myself though (so far I can only succeed in crashing AutoIt)!

Link to comment
Share on other sites

@Cameronsdad

I tried compiling and executing this script, waited 5 mins doing nothing and never got the msgbox. If I right clicked on tray icon it says the script is paused. The only thing going is my cdplayer which should not affect this.

This is not important as I am just playing around with other peoples examples.

whenever you click on the systray icon it automatically pauses script. make sure you're running with beta, and don't touch keybaord or mouse, you should get the message box.

***edit***

well shoot. it worked before i posted it but now i'm having the same issue, let me debug and i'll update code.

Edited by cameronsdad
Link to comment
Share on other sites

whenever you click on the systray icon it automatically pauses script. make sure you're running with beta, and don't touch keybaord or mouse, you should get the message box.

***edit***

well shoot. it worked before i posted it but now i'm having the same issue, let me debug and i'll update code.

found the issue, i had messed up on the mouse check so it was reseting $start regardless of mouse inactivity. corrected it...

Opt("TrayIconDebug",1)
dim $oldpos[2]
dim $pos[2]
#include <Misc.au3>
HotKeySet("{Esc}","_Exit")
Dim $s_keys[117] = [116, _
"01","02","04","05","06","08","09","0C","0D","10", _
"11","12","13","14","1B","20","21","22","23","24", _
"25","26","27","28","29","2A","2B","2C","2D","2E", _
"30","31","32","33","34","35","36","37","38","39", _
"41","42","44","45","46","47","48","49","4A","4B","4C", _
"4D","4E","4F","50","51","52","53","54","55","56","57","58","59", _
"5A","5B","5C","60","61","62","63","64","65","66", _
"67","68","69","6A","6B","6C","6D","6E","6F","70", _
"71","72","73","74","75","76","77","78","79","7A", _
"7B","7C","7D","7E","7F","80H","81H","82H","83H","84H", _
"85H","86H","87H","90","91","A0","A1","A2","A3","A4","A5"]
$dll = DllOpen("user32.dll")

$pos = MouseGetPos()

$start = TimerInit()
While 1
    Sleep ( 100 )
          For $y = 1 To $s_keys[0]
            If _IsPressed($s_keys[$y], $dll) Then 
                $start = TimerInit()
            EndIf
            Next
    $oldpos[0] = $pos[0]
    $oldpos[1] = $pos[1]
    $pos = MouseGetPos()
    if Not $oldpos[1] = $pos[1] Or not $oldpos[0] = $pos[0] Then
        $start = TimerInit()
        
        
    EndIf
    if TimerDiff($start) > 10000 Then
        MsgBox(0,"inactive","you have not pressed any keys or used your mouse for 10 seconds")
        $start = TimerInit()
    Else
        
    EndIf
WEnd
Func _Exit()
DllClose($dll)
Exit
EndFunc
Link to comment
Share on other sites

Cool script, I forgot to close it and it kept going off. Someone could have some fun with that idea.


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Thanks for that cameronsdad, but I'm concerned about missing a key press. I was doing some testing with the following code:

while 1
if _IsPressed (31) then
msgbox (0, "debug", "The '1' key was pressed")
EndIf
Sleep (1000)
WEnd

If I press the key during the sleep it gets missed, only if I'm holding it down during the _IsPressed call does it seem to work. Am I missing something?

Second comment, would it be possible to use the "GetKeyboardState" function in DllCall? This appears to read all the keys at once, thus you could simply match two variables to check if something has changed. My programming knowledge isn't enough to get it working myself though (so far I can only succeed in crashing AutoIt)!

well this isn't real a key capture, and in order for it to fail you'd have to press a single key fast enough for it not to register during the sleep. since you're usually not going to be pressing a single key, the odds are you're not going to run into false inactivity messages. now the program that gafrost's original script was for, that was an issue that i had pointed out and suggested a work around for, because it was to watch for a password to be typed, report success after the right keystrokes were sent. then if you typed too quickly or slowly, a keypress was missed, and your attempt invalidated. i suggested a work around there that you could implement if you want to write the code for it, if every keystroke is that important. one thing i'd suggest though, is to add a line

ToolTip("Keyboard Input")
after the

$start = TimerInit()

in the keyboard check section, so you can see how much it actually is capturing. you may be surprised at how few keystrokes it actually misses during normal execution.

Link to comment
Share on other sites

well this isn't real a key capture, and in order for it to fail you'd have to press a single key fast enough for it not to register during the sleep. since you're usually not going to be pressing a single key, the odds are you're not going to run into false inactivity messages. now the program that gafrost's original script was for, that was an issue that i had pointed out and suggested a work around for, because it was to watch for a password to be typed, report success after the right keystrokes were sent. then if you typed too quickly or slowly, a keypress was missed, and your attempt invalidated. i suggested a work around there that you could implement if you want to write the code for it, if every keystroke is that important. one thing i'd suggest though, is to add a line

ToolTip("Keyboard Input")
after the

$start = TimerInit()

in the keyboard check section, so you can see how much it actually is capturing. you may be surprised at how few keystrokes it actually misses during normal execution.

Good point, it should work for most purposes. I'll keep playing around with it. Any ideas on using the "GetKeyboardState" function? Thanks for the help.
Link to comment
Share on other sites

Isn't this kind of a neater solution?

idle_time.au3

Mapping every key and do the test with _IsPressed seems a bit messy to me. Ex. Would it cacth my activity,on a laptop, only using the fn+ keys?

Regards

Uten

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...