Key Logging, How To's

the_atom

Active member
Joined
Apr 18, 2007
Messages
26
Programming Experience
Beginner
I'm just a beginner in VB.NET. Can anyone help me on how to monitor keystrokes that a user presses in the keyboard?
 
I did this once writing a financial calculator. Here is what I did.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000]'Handles all keystrokes.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Window_KeyDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Window.KeyDown
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Str [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = CatchKey(e.KeyCode)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Focuses all keystrokes to Window.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] WndProc([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] m [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Message)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] FocusMe [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Window.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].WndProc(m)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

This will catch keys and give them to you as Str. You can then handle them in the Window_Keydown Sub.

Hope this helps,
Chris
 
I have tried this too, with limited success. You can remove the taskbar with this:

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000]'Remove Taskbar
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] intReturn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = FindWindow([/SIZE][SIZE=2][COLOR=#800000]"Shell_traywnd"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2])
SetWindowPos(intReturn, 0, 0, 0, 0, 0, SWP_HIDEWINDOW)
[/SIZE]

Just remember to return with this:

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000]'Return Taskbar
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] intReturn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = FindWindow([/SIZE][SIZE=2][COLOR=#800000]"Shell_traywnd"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2])
SetWindowPos(intReturn, 0, 0, 0, 0, 0, SWP_SHOWWINDOW)
[/SIZE]

Then, you can make the form fullscreen:

VB.NET:
[SIZE=2][COLOR=#0000ff]
Me[/COLOR][/SIZE][SIZE=2].WindowState = FormWindowState.Maximized
[/SIZE]

Then set the form's controlbox property to False.

Now, the user can only get out of the form by CtrlAltDel, which I have been told is very difficult to get around.

Hope this helps or you find a better solution,
Chris
 
Back
Top