Solved

How to capture Keystrokes in .Net

Posted on 2004-09-30
9
1,384 Views
Last Modified: 2008-03-04
Hi Guys,
Am wondering if anyone has some code to capture keystrokes from a computer ...like i want to create an executable which logs the key strokes in a text file and mail it to remote server.
0
Comment
Question by:rushtosachin
9 Comments
 
LVL 18

Accepted Solution

by:
armoghan earned 43 total points
ID: 12197765
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12197773
0
 
LVL 27

Assisted Solution

by:planocz
planocz earned 41 total points
ID: 12198899
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 41 total points
ID: 12198922
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

0
 
LVL 8

Expert Comment

by:gregasm
ID: 12209187
Are you programming spyware? Your project is scary sounding.

Bad Luck with it!

just kidding =P
0
 
LVL 3

Expert Comment

by:atxondo
ID: 12238690
If you are using Visual studio.net 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
      }
}
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now