I am working on pretty much my first program, which is supposed to save when a key press has started and ended into a txt file. The problem is, I can't figure out how to do it all so that the application runs in the background. I have tried with windows forms application, which worked only when it was the focused window. Now I tried it with a console application, but with no luck.
I have searched the web for answers to this problem of mine, but I just can't find one for some reason. I've spent multiple hours trying to solve this problem, and I'm going nowhere.
I managed to get the program to work kind of like a keylogger with GetAsyncKeyState as a console application, but I'd need to know when press started and ended, which I'm not sure if it's possible with it.
So this is the closest I've got to, but this isn't quite what I'm after.
class Program
{
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(Int32 i);
static void Main(string[] args)
{
while (true)
{
Thread.Sleep(5);
int keyA = GetAsyncKeyState(65);
if (keyA == 32769) //if "A" key is pressed
{
Console.WriteLine("A");
}
}
}
}