Solved

How to capture Keystrokes in .Net

Posted on 2004-09-30
9
1,387 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

14 Experts available now in Live!

Get 1:1 Help Now