❔ 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
@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.
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
here is the reference link : https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeyboardstate
GetKeyboardState function (winuser.h) - Win32 apps
Copies the status of the 256 virtual keys to the specified buffer.
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
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)
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.
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
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.Save your sweats, use SharpHook instead ✨ ...
I've not looked into RawInput as Jesteeee suggested
https://github.com/TolikPylypchuk/SharpHook
the only downside is that it doesn't support multiple keyboards
Not many things do
Just download AutoHotkey, I'm sure there's a few-liner that'll key-log 😄
if you are fine with using autohotkey id say use that
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.