Capture any keystrokes (e.g. keylogger), preferably C # .net, but any kind will

I need to capture everything that I type on the keyboard, and then store it in different ways. I would prefer it to be written in C # for .Net, but everything will be valid. My reasons for writing this keylogger are simple:

I recently became the owner of a Persian gaming glove. This is a very cool thing that allows you to issue commands by making gestures with your fingers, and at the same time, it is a very thin glove so you can dial this hand with a little discomfort.

In addition, I found a good program called AutoHotkey that can significantly improve performance by making macros like any other action. You can bind any key to any other key or series of keys or commands.

The problem is that you cannot say it simply like this: “this is what I do most” and “this is what I rarely use.” In fact, can you tell which key you use more on the page down or down? Do you use alt + tab more often to exit or move the switch (ctrl-shift or alt-shift)? I can’t say that. I can’t say what actions should be automated or switch to a simpler interface without statistics.

So, I want to write a program to run in the background and write down everything that I type. Then this program will store a histogram of the first, second and third order of my actions (for example, it will store how many times I pressed any one key, for example, input, how many times I pressed a sequence of two keys, for example, alt, and then the tab, and how many times I pressed a sequence of three keys, for example ctrl, alt, and then deleted or ctrl, shift, and then escaped)

Then, after some time spent working / playing / something else, I will have information about what actions I should try to associate with this interface (glove) or automate using AutoHotkey to improve the speed of interaction with a PC.

In other words, a simple scientific experiment, just for fun and progress :)

+6
source share
2 answers

You need a global keyboard handler.

If these are 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 calls the .Net keyboard and mouse event arguments, but I'm not going to publish the code on the Internet, as this kind of activity can be used for dishonest means.

Why did we do this? We embedded powerpoint in our application, but we did not want the user to edit PowerPoint slides when viewing them, so from our C # application we set a hook to control and intercept mouse and keyboard events if they were in the powerpoint control. Great, but we had to make sure:

They were controlled in our application, our application was active, etc. After you set the hook, it listens / captures everything, including other applications, since we cancel certain keyboard actions that we only need when we need it,

Here is the API link.

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

+5
source

Old question, but ...

On Windows, you can use the user32.dll API.

For a very simple keylogger, you can use the GetAsyncKeyState () method, which checks to see if each character in the ASCII table is clicked.

All the code for a very simple and silly keylogger written in the Console application will 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 reliable and clean, I created an API that simplifies the job. You only need to do this:

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

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

+1
source

Source: https://habr.com/ru/post/891277/


All Articles