Showing posts with label APIs. Show all posts
Showing posts with label APIs. Show all posts

Wednesday, 1 April 2009

Detecting and recording key strokes in C#

There are two APIs are used for this purpose.

DllImport("User32.dll")]

private static extern short GetAsyncKeyState(

System.Windows.Forms.Keys vKey);

[DllImport("User32.dll")]

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


(int)Keys

enumeration in calling function.


for example

// Check for keypresses

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


or

// Check for keypresses

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



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