I have an Idle handler that run every 15 seconds to check computer activity. After a certain period of inactivity (user and/or system activity) the computer will return to the login window and be put to sleep.
How can I confirm that the mouse was moved or that the keyboard was pressed ? If I could do that, at the next return of my handler, I could get read «Current Date» and set it to a variable. «Current Date» would have told me that has been mouse/keyboard activity.
That is all I need to know … if after the computer has awaken there was a mouse move or a keyboard press.
As you said, I didn’t find anything precise. So I wrote something. I wrote a handler that checks with a 1 second interval and looped for as many times you wish, if the mouse has moved. If the mouse moved during the time the handler checks, the handler will return «true». If you place the handler a few times in an «idle handler», the test would checks sufficiently to confirm that the mouse has moved. You will need «Extra Suites» to run the handler. Here is the little handler:
on MousingAround(vLoops)
repeat vLoops times
tell application "Extra Suites"
set mouselocation1 to ES mouse location
delay 1
set mouseLocation2 to ES mouse location
if mouselocation1 is not equal to mouseLocation2 then
set mouseMove to mouseMove + 1 -- the «mouveMove» variable adds the number of times the mouse moved
end if
end tell
end repeat
if mouseMove > 0 then
set mouseHasMoved to true
end if
end MousingAround
The idea is to check if the mouse moves as many times as possible during a small amount of time. I wanted to do the same thing with a «key press» but didn’t understand how «Extra Suites» dealed with that one.
I have these two scripts in my DB, though I’m not sure they’re what you want you can play around with them:
repeat
set idleTime to (do shell script "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,\"\";last}'")
display dialog idleTime as string giving up after 5
end repeat
And
property howLong : 600 -- ten minutes (known to work in Tiger)
on idle
set idleTime to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'") as number
if idleTime > howLong then
-- Post a notice and do your maintenance
end if
return howLong
end idle
I am using the same ES mouse position mechanism as you, but I measure along intervals of more than 1 secs.
I’d set the interval at the minimum of your computer sleep time. Also, to prevent the fact that the delay cmd sometimes gets ignored after a system sleep use do shell script “sleep 15”, that might work better.
Thank you Adam and Eelco for sharing your thoughts. I will see if they can simplify working out my problem.
I wrote 2 handlers to check if my mouse moved. Both will be nested in an IDLE handler. The first is the one a posted previously. It repeats a loop of 1 second «vLoops» times to check if the mouse has moved. I nested that handler 4 times in an IDLE handler. I feel that this handler could interfere with any other processes of the system or because repeating too many times (1 second for each loop x vLoops times of the handler x 4 for each cycle of the IDLE handler).
The second determines if the mouse moved, by comparing the mouse location of 2 cycles of the IDLE handler. I think it is simpler and that it should less interfere with processes. This is the second handler:
on MousingAround()
if OddEven(vCycles) is "odd" then
tell application "Extra Suites"
set mouseLocation1 to ES mouse location
end tell
else
tell application "Extra Suites"
set mouseLocation2 to ES mouse location
if mouseLocation1 is not equal to mouseLocation2 then
set mouseMove to mouseMove + 1
end if
end tell
end if
end MousingAround
on OddEven(theNumber) -- tell if a number is odd or even
if (theNumber mod 2 is 0) then
return "even"
else
return "odd"
end if
end OddEven
Am I caring for nothing or is there a real difference between the 2 ?