Detecting if the User is Active or Idle – SystemWide using VB 6.0 and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
 

There are times when you want to know not only if your user is interacting with your application but actually interacting anywhere on the computer. Especially if you are waiting for somekind of user Input. Microsoft thankfully made it easy as I found a API call on MSDN.com and I converted it from C++ to work with Visual Basic (Works for .NET, VB 2008/2010, VB 2013, and 2015). I actually have a VB 6.0 Example for doing this at my www.vbcodesource.com website. Below is the little API call…

Visual Basic 6.0

    '
    'The actual API call to use.
    Private Declare Function GetLastInputInfo Lib "user32.dll" (ByRef inputStructure As inputInfo) As Boolean
    '
    Private Type inputInfo

        structSize As Long

        tickCount As Long

    End Type

Visual Basic.NET Version…

    '
    'The actual API call to use.
    Private Declare Function GetLastInputInfo Lib "user32.dll" (ByRef inputStructure As inputInfo) As Boolean
    '
    Private Structure inputInfo

        Dim structSize As Int32

        Dim tickCount As Int32

    End Structure

Now that the APIs are out of the way, you only need to setup and use the codes. The code below will work for ALL VBs with just some minor changes…


Visual Basic 6.0

        '
        'The variable that will be passed to the API call and receive the activity report.
        Private info As inputInfo
        '
        'Visual Basic 5.0/6.0…
        Dim firstTick As Long
        Dim lastTick As Long

Visual Basic.NET…

        '
        'The variable that will be passed to the API call and receive the activity report.
        Private info As inputInfo
        '
        'Visual Basic.NET…
        '
        Dim firstTick As Int32
        '
        Dim lastTick As Int32

All I did next was use the code in a Timer control and set the Interval to 1000ms (1 Second) which will update the lblTime Label Control the Last time of day the user was active. The InputInfo structure will actually give you the tickcount for when the user was last active if you want to use that. I decided to simply get the actual time they were last active

        '
        'This timer will fire every 1000ms(One Second) or so displaying the last time the user was active.
        '
        'The size of the structure for the API call.
        info.structSize = Len(info)
        '
        'Call the API.
        GetLastInputInfo(info)
        '
        'Compare the tickcount values to determine if activity has occurred or not.
        If firstTick <> info.tickCount Then
            '
            'Display the current time of the users last activity.
            '
            'Change lblTime.Caption to lblTime.Text if using .NET and likewise change Time
            'to Now instead.
            lblTime.Caption = "Last Active: " & Time
            '
            'Get the new tick value.
            firstTick = info.tickCount

        End If

The example program wth the code in action


As I mentioned already, I put that code above in a Timer control, then I set to enable and disable the timer in Two Button controls.

Now your application will know when the user was last active on the computer. But remember, just because it says they are Not active doesn’t mean that they are not reading a document, a website, or some other task that doesn’t require user input. Have fun!

          Jason

Revised: 2015

7 thoughts on “Detecting if the User is Active or Idle – SystemWide using VB 6.0 and VB.NET

  1. Anderson

    I tried but seems doesn’t work 🙁

    Public Sub testetick()

    ‘This timer will fire every 1000ms(One Second) or so displaying the last time the user was active.

    Dim firstTick As Long
    Dim lastTick As Long
    ‘The size of the structure for the API call.
    info.structSize = Len(info)

    ‘Call the API.
    Debug.Print GetLastInputInfo(info)

    ‘Compare the tickcount values to determine if activity has occurred or not.
    If firstTick info.tickCount Then

    ‘Display the current time of the users last activity.

    ‘Change lblTime.Caption to lblTime.Text if using .NET and likewise change Time to Now instead.

    ‘Get the new tick value.
    Debug.Print “Last Active: ” & Time
    firstTick = info.tickCount

    End If

    End Sub

    Result:
    Last Active: 07:45:01
    True
    Last Active: 07:45:02
    True
    Last Active: 07:45:03
    True
    Last Active: 07:45:04
    True
    Last Active: 07:45:05
    True
    Last Active: 07:45:06
    True
    Last Active: 07:45:07
    True

    What I did wrong??

    Also, your website address is showing some errors:
    http://www.vbcodesource.com/
    A página XML não pode ser exibida
    Não é possível exibir a entrada XML usando a folha de estilos XSL. Corrija o erro e clique no botão Atualizar ou tente novamente mais tarde.

    ——————————————————————————–

    Um nome foi iniciado com um caractere inválido. Erro ao processar o recurso ‘http://www.vbcodesource.com/’. Linha 25, posi…

    if (Width > 0 && Height > 0) {
    —————————————^

    Reply
  2. Jason Post author

    Hi, remember if the mouse moves even a small amount or ANY keyboard key is pressed then it will show activity has taken place.

    Looks like your using VB 6.0. So, download the example from this link: http://www.vbcodesource.org/downloads/getLastUserActivity.zip and tell me if it appears to work ok for you.

    I also tried the vbcodesource.com site and it appears to be loading ok now. Let me know if there are problems still.

    Thanks for letting me know 🙂

    Jason

    Reply
  3. Steve

    You know how long I have been looking for something like this. All the other threads want you to jump through hoops or have you do something very complicated for something so simple. Thanks!

    Reply
  4. Rami Reyhan

    Thank you! This is very simple and nice!

    For my application I also added:


    Dim lastActiveTime As DateTime = Now

    and in the tick sub (“Private Sub MyTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTimer.Tick”):


    ‘1800 is 30 minutes, in seconds
    If Date.Now.Second – lastActiveTime.Second = 1800 Then
    ‘Do Somthing
    End If

    and then your code:


    ‘This timer will fire every 1000ms(One Second) or so displaying the last time the user was active.

    ‘The size of the structure for the API call.
    info.structSize = Len(info)

    ‘Call the API.
    GetLastInputInfo(info)

    ‘Compare the tickcount values to determine if activity has occurred or not.
    If firstTick info.tickCount Then

    ‘Display the current time of the users last activity.

    Time
    ‘to Now instead.
    lblTime.Text = “Last Active: ” & Now

    ‘Get the new tick value.
    firstTick = info.tickCount

    End If

    Also, a small note, the vb.net variable assignment is defining two ” Dim lastTick as Int32″, instead of one “lastTick” and one “firstTick”…

    Reply

Leave a Reply