Click here to Skip to main content
15,667,738 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:

Sorry If I posted this to the wrong place.

I would like to catch user32.dll GetKeyState and similar function calls of an already running process.
I am not so good in hooking so tried to dig up something usable library (source code) that works on all Windows version from Windows XP: XP, Vista, Windows 7 etc. (both of x86 and x64!!!).
I have not found any. Could anyone link a working solution for my problem?

Thank you!
Viktor Signaievskyi 27-Apr-11 14:59pm    
Are you interested in keyboard hook?
EURaklap 27-Apr-11 15:05pm    
I am writing a so called "helper application" for a game and this game using GetKeyState to read states of modifier keys, so I think the only thing what I can do if I want to send CTRL+O to this game that I hook these functions. :(

This one works for pre-Windows 7 and is very popular:

API hooking revealed[^]

And this one claims to work for Windows 7 too (I have not heard of it until today though, so evaluate it and then decide):[^]
Share this answer
EURaklap 27-Apr-11 15:24pm    
Already tried each without success. API hooking revealed not works W7 (x64), neither EasyHook. :(
Nish Nishant 27-Apr-11 15:26pm    
Yeah, it would seem that there are no public libraries yet (none that are popular anyway). There are apps that do it but their authors have not revealed their methods :-)
Espen Harlinn 27-Apr-11 15:40pm    
Nice links, my 5
Manfred Rudolf Bihy 27-Apr-11 16:20pm    
I was going to say that!
Manfred Rudolf Bihy 27-Apr-11 16:20pm    
Nice information! 5+
If nothing else seems to do the trick, you can always consider using:
Windows Driver Kit
Kbfiltr - WDF Version

Espen Harlinn
Share this answer
Nish Nishant 27-Apr-11 15:41pm    
A good lower level approach, but sometimes that's just what's needed. My vote of 5.
Espen Harlinn 27-Apr-11 15:49pm    
Thanks Nishant :)
Manfred Rudolf Bihy 27-Apr-11 16:19pm    
Good answer! 5+
Espen Harlinn 27-Apr-11 17:25pm    
Thanks Manfred :)
EURaklap 27-Apr-11 16:28pm    
Ouch, writing a driver is too specific, professional and unneccesary I think, but very good idea :)
It's not free but probably the best API hook system is "madCodeHook".

Best because it's very robust and stable.
Supports all 32 and 64 bit Windows operating systems from Windows 95 to Windows 7, etc.
Note, I make no money out of this, I'm just a paid user of it.
I've made my own little hooking engines, used others, etc., and madCHook is the best.
Share this answer
EURaklap 21-May-11 4:59am    
I know this very great lib, but it costs too much for only one free project.:(
Share this answer
EURaklap 21-May-11 4:59am    
Yes, It does not work on W7 x64 :(

I saw your question.

Your problem can be solved by several ways.

1. Hooking Keyboard in Kernel land.
2. Hooking Keystate in User land by using SetWindowsHookex() function.
3. I think that in your case, you can use a keybd_event() function to send keypress sign to your game.

Which is your case?

Share this answer
EURaklap 28-Apr-11 5:09am    
Neither :)
My app should work in background without disturbing the user, so user can do anything what he wants. I would like to modify keystate in a particular running process.
[no name] 28-Apr-11 5:42am    
Yeah. It can be possible enough and be solved by two ways.
In user land, you can use SetWindowsHookex() function and you can change wParam and lParam for your purpose.
then call CallNextHookex() function.
In another way, you can hook Keyboard Driver and change Parameter.
If you need, I will post an article.
How about?
EURaklap 28-Apr-11 8:40am    
I would be very happy If you share with me some links where I found a working solution for my problem: modify keystates of another thread on x86 and x64 windows (XP and above) while my application nor the modified application is not actived/focused. I did not find any working solution. (Searching 5 days. lol)
You can start using this code snippet. I've used it in my practice.

#include <code><stdio.h>
#include <windows.h>

void CheckKey(int key);

int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // pointer to structure with message data

typedef struct tagKBDLLHOOKSTRUCT {
DWORD vkCode; // virtual key code
DWORD scanCode; // scan code
DWORD flags; // flags
DWORD time; // time stamp for this message
DWORD dwExtraInfo; // extra info from the driver or keybd_event

HHOOK hHook;

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
hHook = SetWindowsHookEx(13, KeyboardHook, hInstance , 0);
while (GetMessage(NULL,NULL,0,0)) ; // NOP while not WM_QUIT
return UnhookWindowsHookEx(hHook);

LRESULT CALLBACK KeyboardHook (int nCode, WPARAM wParam, LPARAM lParam )
if (nCode == HC_ACTION)
if (wParam == WM_SYSKEYDOWN || wParam == WM_KEYDOWN)
CheckKey (((PKBDLLHOOKSTRUCT)lParam)->vkCode);
return CallNextHookEx(hHook, nCode, wParam, lParam);

void CheckKey(int key)
if (key==VK_CONTROL)
// do what you need to do at "CTRL" button clicked:)
Share this answer
EURaklap 27-Apr-11 16:25pm    
Main problem is how to injecting and hooking an already running process on all windows versions including x64 and the mentioned application using GetKeyState function to read states of modifier keys and my application should work in the background.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900