[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to capture Keystrokes in .Net

Posted on 2004-09-30
9
Medium Priority
?
1,417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

649 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