Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the inactivity timeout for an application or globally?

Status
Not open for further replies.

hackoo

Programmer
Jan 31, 2003
28
TN
Hi !
I'm coding an application like a Keylogger in VB6 that take a screenshot every 5 minutes.
My question is: if there is a Tip that stop recording the screenshot when there is No Activity on the Computer?

NoActivity means :if NO keyboard or mouse activity detected
The Algorithm is like this :

If NoActivity Then Stop recording the screenshot
If Activity Then Continue recording the screenshot

Thank you for your Help !
 
One way would be to sub class your app and have a boolean value (e.g. NoActivity) that is set to True after a certain amount of time (your discression).

You could then set the boolean to false when your app recieves certain windows messages (e.g. WM_KEYDOWN, WM_LBUTTONDOWN, WM_MBUTTONDOWN, WM_RBUTTONDOWN, WM_MOUSEMOVE etc, it might also be worth checking the non-client versions of these messages prefixed with WM_NC as opposed to just WM_) and start the process again.

This might not however, be the most efficient way of achieving this though.

Hope this helps

HarleyQuinn
---------------------------------
Black coat, white shoes, black hat, cadillac. The boy's a timebomb!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
Actually looking at the scope required by the OP again my suggested solution is a little limited, I would probably do this with a low level (or not even necessarily low-level as it's just to check any activity) keyboard (and mouse) hook to detect keyboard (and mouse again) events (SetWindowsHookEx is a good starting point).

Another way could be to use a WH_JOURNALRECORD hook but it's not the most efficient...

HarleyQuinn
---------------------------------
Black coat, white shoes, black hat, cadillac. The boy's a timebomb!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
Should have read Hugh's post, I've just regurgitated alot of what was said in that thread... [sad]

HarleyQuinn
---------------------------------
Black coat, white shoes, black hat, cadillac. The boy's a timebomb!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
Thank you for your reply !
After Looking up in the internet I just found it ! and it works for me very well !. I should share it with you ! anyway it could help someone else ;)
Code:
'**************************************
' Name: An Idle Check
' Description:Idle Check tests your syst
'     em whether it is in an idle state. Once 
'     the system is idle for a specified amoun
'     t of time it performs a certain function
'     . After it stops being idle a further fu
'     rther procedure is called. (Perfect for 
'     screensavers). This is done through chec
'     king any mouse cursor movements and any 
'     key presses.
' By: Soluch
'
'This code is copyrighted and has' limited warranties.Please see [URL unfurl="true"]http://w[/URL]
'     ww.Planet-Source-Code.com/vb/scripts/Sho
'     wCode.asp?txtCodeId=44355&lngWId=1'for details.'**************************************

Option Explicit
'BEFORE YOU BEGIN:
'- Place timer control on an empty form,
'     name it "TimerIdle"
'- Set the interval on the timer to 1 (o
'     ne)
'- Copy this code into the form
'- Ensure you can see the Immediate Wind
'     ow to see results
'Note: No error control, insert if you l
'     ike
'May encounter problems if computer pass
'     es midnight (timer resets)
'Peter Soluch - 2003
'Function to get state of keys

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    'Function to get position of mouse cursor
    
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
'The time (in seconds) a computer must be idle before running sub
        
    Private Const IDLESECONDS As Long = 5
    'Type used with GetCursorPos

Private Type POINTAPI
    X As Long
    Y As Long
    End Type

Private Sub TimerIdle_Timer()

    Dim newMousePos As POINTAPI 'Var For values of "current" Mouse Position
    'Static variables
    Static oldMousePos As POINTAPI 'Old / Previous values of the mouse position
    Static isIdle As Boolean'Checks If state is currently idle
    Static wasIdle As Boolean'Checks If state was "declared" idle before
    Static idleStartTime As Single 'When did the idle first start
    Static idleTimeCount As Single 'Idle time counter
    Static idleTimeSecs As Single'Idle time in seconds
    Static passedOnce As Boolean'Used For first time timer started
    Dim i As Integer'Just a counter
    'Check for first pass to set timer

    If passedOnce = False Then
        'Get what time the timer started
        idleStartTime = Timer
        passedOnce = True
    End If

    'Set that idle is true, check for mouse 
    '     and keys movements, etc
    'If there are any then isIdle will become false
     
    isIdle = True
    'Check API for keypress

    For i = 1 To 256
        'If pressed state becomes -32767

        If GetAsyncKeyState(i) = -32767 Then
            isIdle = False
        End If

    Next i

    'Get CURRENT position of the mouse cursor
   
    GetCursorPos newMousePos
    'Compare mouse position with last time (
    '     has the mouse moved?)

    If newMousePos.X <> oldMousePos.X Or newMousePos.Y <> oldMousePos.Y Then
        'Mouse moved, not idle
        isIdle = False 'Not idle
        'Replace old coordinates with new ones t
        '     o check next time
        oldMousePos.X = newMousePos.X
        oldMousePos.Y = newMousePos.Y
    End If

    '1. Check if computer WAS idle and user has come back
        
    If wasIdle And Not isIdle Then
        'Run procedure for when computer comes o
        '     ut of idle state
        IdleFinished
        'Reset wasIdle, so procedure does not ru
        '     n again till next idle time
        wasIdle = False
        'Clear timers
        idleTimeSecs = 0
        idleTimeCount = 0
        idleStartTime = Timer
    End If

    'Check for how long has been idle (seconds - i.e. convert to longs)
         
    If CLng(idleTimeSecs) > CLng(idleTimeCount) Then
        Debug.Print CLng(idleTimeSecs) & " second(s) have passed on idle"
        idleTimeCount = idleTimeSecs
    End If

    'Computer was not idle but has become id
    '     le after x seconds

    If Not wasIdle And isIdle And idleTimeSecs >= IDLESECONDS Then
 'Computer becomes idle, set wasIdle to true so can run
 'procedure after computer comes out of idle state

        wasIdle = True
        'Run procedure for "Idle"
        IdleStarted idleTimeSecs
    End If

'If idle then update time that has been  idle, else reset timers 
        
    If isIdle Then
        idleTimeSecs = Timer - idleStartTime
    Else
        Debug.Print "User pressed a key or moved the mouse"
        idleTimeCount = 0
        idleStartTime = Timer
        idleTimeSecs = 0
    End If

End Sub

Private Sub IdleStarted(Optional ByVal numSeconds As Long)

'Code when idling starts, i.e. user has gone away for x secs
         
    Debug.Print "Computer was declared idle at " & Now & " after " & numSeconds & " seconds"
    'Put your code here
End Sub

Private Sub IdleFinished()

'Code when idling stops, i.e. user returns
        
    Debug.Print "Computer stopped being IDLE at " & Now
    'Put your code here
End Sub
 
Er ... did you actually look at the link HughLerwill provided? It contains a shorter, more efficient and (in my opionion obviously ;-) ) more elegant variant of the code you have posted above.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top