Global keyboard with C ++

I have already seen many tutorials and articles about connecting, but I do not quite understand. Mostly because each individual example uses a different solution.

I know that I will need to implement something that will keep the hook alive. Usually this is some kind of cycle. Q1: If this loop was in some class with callbacks, will the thread prevent them from executing?

I know this will take some time, but I would really appreciate a well-explained global keyboard example. Or just connect me with some working binary example. (Believe me, I have been trying to do this in the last few hours).

thanks

+4
source share
3 answers

I know that I will need to implement something that will keep the hook alive

No, it's not a problem. A global hook requires a DLL with a callback. This DLL is injected into all running processes. It will remain loaded in the process until you name UnHookWindowsHookEx (), or the process ends, whichever comes first.

Please note that you can also connect the keyboard using WH_KEYBOARD_LL. This is not a global hook; Windows will switch the context to your program and make a callback. This is much easier to use, since you do not need the IPC mechanism with the embedded DLL that the global hook requires. The low-level hook remains active until you unhook it, the thread that owns the message queue ends, or your process ends, whichever comes first.

+3
source

There is a skeletal keyboard hook with downloadable code on my website here (pay attention to the β€œdownload” button at the bottom of the page). The article discusses some things you need to understand as shared sections and informs you of an excellent article Kyle Marsh on Win32 hooks (if some idiot on MSDN hasn't removed it yet, because it's not enough .netty).

The source code is in the example, it just requires the creation of makefile / sln (I don’t know which compiler / version you will use). The code is strangely similar to the one that has been in the commercial product for delivery for a decade, so I know that it works.

Note that integrity issues may reduce connectivity in Vista and W7.

+2
source

your program must stay alive during the hook. Your program is a program that calls SetWindowsHookEx / UnSetWindowsHookEx (or something else). Between these calls, you really have a message loop (this is probably the while loop you are talking about), like any regular Windows program.

But since your program is different from the ones you plug in, your message loop will not cause other processes to freeze. This is called multitasking :)

+1
source

All Articles