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

后端 未结 2 1728
野趣味
野趣味 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条回答
  • 2020-12-04 21:06

    You need a global keyboard handler.

    If this is windows you will have to use the Windows API and use the following:

    SetWndowsHookEx

    UnhookWindowsHookEx

    CallNextHookEx

    You need to set up a global "hook" to listen for keyboard and mouse events.

    I have a C# class that does this and raises .Net keyboard and mouse event args, but I am not going to post the code online as this type of activity can be used for nefarious means.

    Why did we do this? We embedded powerpoint into our application, but we didn't want the user editing the powerpoint slides while viewing them so from our C# application we set up a hook to monitor and intercept mouse and keyboard events if they were in the powerpoint control. Worked great but we had to make sure:

    They were in control in our application, our application was active, etc. Once you set up the hook, it listens/captures for everything including other applications, since we were canceling specific keyboard actions we only wanted it when we needed it.

    Here's an API link.

    http://msdn.microsoft.com/en-us/library/ff468842(v=VS.85).aspx

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题