❔ Keyboard hook

How to get all keys pressed anywhere in windows (like globally just like how a keylogger would work) WITHOUT USING A WINDOWS FORM. Like if we just start a simple .net core console application, how to do this? I cannot find the answer to this anywhere in the internet.
12 Replies
infernus8349
infernus8349OP2y ago
@implicit electron i did try with few of them like GetKeyboardState() and it would return me a 256 array of bytes which they say is status data for each virtual key but i do not find any difference when i press any key.
[DllImport("user32.dll")]
public static extern bool GetKeyboardState(byte[] keys);
[DllImport("user32.dll")]
public static extern bool GetKeyboardState(byte[] keys);
public static Task Keyboard_Key_Extractor()
{
while (true)
{
byte[] keys = new byte[256];

bool condition = GetKeyboardState(keys);

string stuff = condition + " ";

for (int i = 0; i < 100; i++)
stuff += keys[i].ToString();

Console.WriteLine(stuff);

Thread.Sleep(50);

}

return Task.CompletedTask;
}
public static Task Keyboard_Key_Extractor()
{
while (true)
{
byte[] keys = new byte[256];

bool condition = GetKeyboardState(keys);

string stuff = condition + " ";

for (int i = 0; i < 100; i++)
stuff += keys[i].ToString();

Console.WriteLine(stuff);

Thread.Sleep(50);

}

return Task.CompletedTask;
}
here i tried getting first 100 bytes from 256 to display to see if there is any difference if i press any key and i dont find any difference at all
infernus8349
infernus8349OP2y ago
GetKeyboardState function (winuser.h) - Win32 apps
Copies the status of the 256 virtual keys to the specified buffer.
infernus8349
infernus8349OP2y ago
so how to do without any form just a normal application any idea how to do this? i have been googling stuff for past 2 days nvm, i solved it here is the new code which worked for me
public static extern int GetAsyncKeyState(Int32 i);
public static extern int GetAsyncKeyState(Int32 i);
public static Task Keyboard_Mouse_Key_Extractor()
{
while (true)
{
Thread.Sleep(2000);
Console.Clear();

for (int i = 0; i <= 255; i++)
{
int state = GetAsyncKeyState(i);
if (state != 0)
{
Console.Write(((Keys)i).ToString());
}
else
{
Console.Write("NO input");
}
}
}


return Task.CompletedTask;
}
public static Task Keyboard_Mouse_Key_Extractor()
{
while (true)
{
Thread.Sleep(2000);
Console.Clear();

for (int i = 0; i <= 255; i++)
{
int state = GetAsyncKeyState(i);
if (state != 0)
{
Console.Write(((Keys)i).ToString());
}
else
{
Console.Write("NO input");
}
}
}


return Task.CompletedTask;
}
This outputs a set of display which contain a lot of "NO input" text and maybe some other which depends on what you press (this can take up mouse commands too)
Jester
Jester2y ago
you should use raw input, but i can be some work to set up (a bunch of functions and structs and a messsage pump). there are probably wrappers out there https://learn.microsoft.com/en-us/windows/win32/inputdev/about-raw-input
Raw Input Overview - Win32 apps
This topic discusses user-input from devices such as joysticks, touch screens, and microphones.
infernus8349
infernus8349OP2y ago
i did see that but i didn't know how to do it, so anyway i already solved it as you can see the solution above but thanks for the response
thebeardedquack
GetAsyncKeyState() is still probably not the correct solution, you suggested you needed something like a key logger. While this function will return the current keypress states, it doesn't provide you with which order the keys are pressed in, and you're required to poll, which means likely missing events, or wasting a bunch of CPU in a tight loop. There is SetWindowsHookEx() where you can tell Windows which events you're interested in. One of the values is WM_KEYBOARD, which requires a KeyboardProc callback. This is probably the most "correct" solution, based on what you've described. Unfortunately I can't tell you whether this solution requires a main event loop or not as I've never used it myself (I have used GetAsyncKeyState() before). If you do need an event loop, the easiest way for me would probably just do this all in C++ and use WinMain and register an empty, hidden, window object.
Cattywampus
Cattywampus2y ago
Save your sweats, use SharpHook instead ✨ ...
thebeardedquack
I've not looked into RawInput as Jesteeee suggested
Cattywampus
Cattywampus2y ago
https://github.com/TolikPylypchuk/SharpHook the only downside is that it doesn't support multiple keyboards
thebeardedquack
Not many things do Just download AutoHotkey, I'm sure there's a few-liner that'll key-log 😄
Jester
Jester2y ago
if you are fine with using autohotkey id say use that
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server