Search Tools Links Login

Monitor Mouse and Keyboard Activity


With just a little bit of API goodness, you can monitor mouse and keyboard activity.

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

Usage

Create a Form with a Label and a Timer, then add this code:

Option Explicit

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

About this post

Posted: 2019-09-25
By: AndreaRossignoli
Viewed: 326 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Special Instructions

This code originally appeared on AndreaVB.com, and has been republished here with the permission of Andrea Tincani.


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.