Wednesday, 1 April 2009

Detecting and recording key strokes in C#

There are two APIs are used for this purpose.


private static extern short GetAsyncKeyState(

System.Windows.Forms.Keys vKey);


private static extern short GetAsyncKeyState(

System.Int32 vKey);

Note that this type of coding in .Net environment is called PInvoke
( platform invoke) feature as we are declaring and calling unmanaged libraries in .Net environment.

This Code works for all kind of Application domains running in Windows OS whether it is notepad, Messenger etc.
Calling APIs


enumeration in calling function.

for example

// Check for keypresses

if (GetAsyncKeyState((int)Keys.Down) != 0)


// Check for keypresses

if (GetAsyncKeyState((int)Keys.Up) != 0)

use stringbuilder variable to store all captured keystrokes in that variable.


  1. opppps Normal 0 false false false MicrosoftInternetExplorer4 is not related to the topic, it is word document fault.canbody suggest me a better html editor for pasting code.

  2. I need your Help to developing the keyloger.