How to capture Keystrokes in .Net

Hi Guys,
Am wondering if anyone has some code to capture keystrokes from a computer i want to create an executable which logs the key strokes in a text file and mail it to remote server.
Who is Participating?
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

iboutchkineConnect With a Mentor Commented:
This step-by-step article demonstrates how to trap keystrokes in Windows Forms controls.
By using the sample code in this article, you can intercept almost any individual keystroke.
You also can intercept key combinations, including CTRL and ALT.
The Print Screen key is not affected by this technique. Additionally, some keystrokes from keyboards
with additional keys, such as keys that control a Web browser or a CD-ROM player, might not be captured.

For most purposes, the standard KeyUp, KeyDown, and KeyPress events are enough to capture and handle
keystrokes. However, not all controls raise these events for all keystrokes under all conditions.

For example, consider the DataGrid control: If no data has been assigned to the grid, the arrow keys
(LEFT ARROW, RIGHT ARROW, UP ARROW, and DOWN ARROW) raise only the KeyUp event. Other keys,
such as A or 4, raise all three events. If the DataGrid is currently displaying data, none of the standard
keyboard events are raised for the navigation keys. Keystrokes such as A or 4 raise no events, raise only
KeyUp, or raise all three events, depending on what is currently selected in the control. In these situations,
you can follow the steps in this article to capture keystrokes, regardless of the state of the control.

The code samples in this article are written to work with the DataGrid, because this is the control for which
this feature is most frequently requested. You can use this same approach with other .NET controls.

Set Up the Key Trap
To trap keystrokes in a Windows Forms control, you derive a new class that is based on the class of
the control that you want. You override the ProcessCmdKey method. In this overridden method, you will
place the code to process the keystrokes that you want to trap. The following sample code is an example
of the basic structure for such a class:

Class MyDataGrid
    Inherits DataGrid

    Protected Overrides Function ProcessCmdKey( _
        ByRef msg As Message, _
        ByVal keyData As Keys _
                                ) As Boolean
    End Function
End Class      

Implement the Overridden Method
The system passes two parameters to the ProcessCmdKey method: msg and keyData. The msg parameter
contains the Windows Message, such as WM_KEYDOWN. The keyData parameter contains the key code
of the key that was pressed. If CTRL or ALT was also pressed, the keyData parameter contains
the ModifierKey information.

Using the msg parameter is not mandatory; you can ignore it. It is good practice, however, to test
the message. In this example, you test WM_KEYDOWN to verify that this is a keystroke event.
You also test WM_SYSKEYDOWN, so that it is possible to catch keystroke combinations that involve
control keys (primarily ALT and CTRL).

To trap specific keys, you can evaluate the keyCode by comparing it to the Keys enumeration.
The following code sample demonstrates how to catch the keystrokes UP ARROW, DOWN ARROW, TAB,
CTRL+M, and ALT+Z:
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104

If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
   Select Case (keyData)
      Case Keys.Down
         Console.WriteLine("Down Arrow Captured")
      Case Keys.Up
         Console.WriteLine("Up Arrow Captured")
      Case Keys.Tab
         Console.WriteLine("Tab Key Captured")
      Case (Keys.Control And Keys.M)
         Console.WriteLine("<CTRL> + m Captured")
      Case (Keys.Alt And Keys.Z)
         Console.WriteLine("<ALT> + z Captured")
    End Select
End If

Are you programming spyware? Your project is scary sounding.

Bad Luck with it!

just kidding =P
If you are using Visual you can use the key events to detect when an key is pressed.
You can detect  KeyDown, KeyPress and KeyUp in any form or any object like a TextBox, Button, ..
in C# you can do with this:
First declare the event function in the object.

this.txbEnviar.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txbEnviar_KeyPress);

You only have to decalre the actions to be done after the keys are pressed.
private void txbEnviar_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
      if (13 == e.KeyChar)
            //Enter key has been pressed
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.