Click here to Skip to main content
13,549,285 members
Rate this:
Please Sign up or sign in to vote.
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!
Posted 27-Apr-11 8:54am
P1119r1m 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. :(
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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):[^]
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
ManfredRBihy 27-Apr-11 16:20pm
I was going to say that!
ManfredRBihy 27-Apr-11 16:20pm
Nice information! 5+
Nish-Nishant 27-Apr-11 16:21pm
Thanks :-)
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

If nothing else seems to do the trick, you can always consider using:
Windows Driver Kit
Kbfiltr - WDF Version

Espen Harlinn
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 :)
ManfredRBihy 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 :)
Espen-Harlinn 27-Apr-11 17:30pm
As I mentioned "if nothing else seems to do the trick", this is an option of last resort if you haven't done anything like this before. On the other hand, if you've done a filter driver before, it actually fairly straight forward as the driver kit comes with sample drivers :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

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.
EURaklap 21-May-11 4:59am
I know this very great lib, but it costs too much for only one free project.:(
Rate this: bad
Please Sign up or sign in to vote.

Solution 7

EURaklap 21-May-11 4:59am
Yes, It does not work on W7 x64 :(
Rate this: bad
Please Sign up or sign in to vote.

Solution 5


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?

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.
User-7840209 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)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:)
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02-2016 | 2.8.180515.1 | Last Updated 21 May 2011
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100