?
Solved

How to capture Keystrokes in .Net

Posted on 2004-09-30
9
Medium Priority
?
1,427 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
6 Comments
 
LVL 18

Accepted Solution

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

Expert Comment

by:armoghan
ID: 12197773
0
 
LVL 27

Assisted Solution

by:planocz
planocz earned 164 total points
ID: 12198899
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 164 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 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