Click to See Complete Forum and Search --> : Key logging in C#


dosman711
March 31st, 2003, 03:22 PM
Is there any way in C# to create a simple keylogger? Thanks!

pareshgh
March 31st, 2003, 03:35 PM
what do you mean by keylogger ?

Paresh

dosman711
March 31st, 2003, 07:17 PM
Sorry about that, I was just wondering if there was a way to catch all the keystrokes that are going to other applications and log them without affecting the standard keyboard I/O. Basically, I'm trying to find out what happens to my computer when I'm not in my room.
Thanks!

Oobe
April 1st, 2003, 02:24 AM
Originally posted by dosman711
...I was just wondering if there was a way to catch all the keystrokes that are going to other applications and log them without affecting the standard keyboard I/O. Basically, I'm trying to find out what happens to my computer when I'm not in my room.
You can catch keys pressed at any window focused and at any time. Here a way to do this via Windows API:
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);

And my own know-how to use above code:

public static int[] WhichKeysPressedNow()
{
System.Collections.ArrayList list =
new System.Collections.ArrayList();
for(int i = 0; i < 500; i++)
{
if (GetAsyncKeyState(i) != 0)
{
list.Add(i);
}
//list.Add((int)GetAsyncKeyState(i));
}
int[] res = new int[list.Count];
for(int i = 0; i < list.Count; i++)
{
res[i] = (int)list[i];
}
return res;
}

Now you can at any time see, which keys are pressed using my function "WhichKeysPressedNow". You can insert this function into an Timer code and check which keys are pressed each ... 20 ms. (this is faster than keys is possible to be pressed).

pareshgh
April 1st, 2003, 12:33 PM
this can be done without using user32.dll.
however you will need to implement the key events. if you capture it properly then the game is on your side.

I agree that at some point user32.dll or some system native dll reference needs to be mentioned.


-Paresh