Detect Inactive Input

Posted On 2018-03-01 by AndreaRossignoli
Keywords:
Tags: VB6 Windows API VB6 System Services VB6 Files and I/O 
Views: 66


This code sample shows how to detect activity or inactivity on the mouse and keyboard.

Option Explicit

'===========================================================
'TYPE
'===========================================================
Private Type POINTAPI
x As Integer
y As Integer
End Type
'===========================================================
'API
'===========================================================
Private Declare Sub GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI)
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer
'===========================================================
'Variables
'===========================================================
Private posOld As POINTAPI
Private posNew As POINTAPI

'===========================================================
'InputCheck
'===========================================================

Public Function InputCheck() As Boolean
    Dim i As Integer

    'Get the current mouse cursor coordinates
    Call GetCursorPos(posNew)
    'Compare with old coordinates
    If ((posNew.x <> posOld.x) Or (posNew.y <> posOld.y)) Then
        posOld = posNew
        InputCheck = True
        Exit Function
    End If
    'Check keys state
    For i = 0 To 255
    If (GetAsyncKeyState(i) And &H8001) <> 0 Then
            InputCheck = True
            Exit Function
        End If
    Next i
    InputCheck = False
End Function

And here is how to use it. Create a Form with a Label and a Timer, and place the following in the timer event:

'
Option Explicit

Private Sub Timer1_Timer()
Label1.Caption = InputCheck
End Sub


About the Author

has posted a total of 1 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.81.68.240

Before you can post, you need to prove you are human. If you log in, this test goes away.


Code Links