Click here to Skip to main content
16,017,240 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody!
I need to read events from an existing window, so I cannot just create a new one.

To read Kbd events, I need to use GetRawInput, but before I can use it I need to receive WM_INPUT from the WndProc callback. Since the window has already been created I cannot assign the callback function like I could from the call to CreateWindow. So how do I get the WM_INPUT message?

Thanks!
Posted
Updated 13-Feb-11 4:16am
v3
Comments
Manfred Rudolf Bihy 13-Feb-11 10:10am    
Edit for clarity. Adjusted title for clarity as well.

In case hooking is too much (it gets all the messages from the system, not only from the window you need) The more proper technique (since the time of the first Petzold!) is subclassing:

just write something like:

C++
WNDPROC oldproc;
LRESULT CALLBACK yourproc(HWND h, UINT u, WPARAM w, LPARAM l)
{
   /* DO YOUR MESSAGE PROCESSING AND TEHN ... */

   return CallWindowProc(oldproc,h,u,w,l);
}


and setup the subclassing as

oldproc = (WNDPROC)SetWindowLongPtr(yourHwnd, GWL_WNDPROC, (LONG_PTR)yourproc);


and from there, yopuproc will start receiving th messages of your window.
 
Share this answer
 
Comments
Andrew Brock 15-Feb-11 10:45am    
This is what I would do. 5+.
You need to install a hook. Google for "global keyboard mouse hook" and you'll get what you want.

For some sample code on creating a CBT hook, take a look at this article I wrote (several years ago, but the core concepts remain the same today):

Manipulating Windows using messages and simple CBT hooking[^]
 
Share this answer
 
v3
Comments
Manfred Rudolf Bihy 13-Feb-11 10:15am    
Nice link! 5+
I see that the article employs the same function I advertized. :)
Nish Nishant 13-Feb-11 10:16am    
Thank you.
Sergey Alexandrovich Kryukov 13-Feb-11 11:56am    
All right, 5.
--SA
Harrison H 13-Feb-11 16:06pm    
Good link, to your article no less! My 5
micard 15-Feb-11 10:40am    
i created 2 files, one for the exe and one for the dll

My code looks like this:
//EXE file
typedef void (*SetHook)(HMODULE);

int main()
{
SetHook SetHookFunc;
HMODULE hModule = LoadLibrary("Hook.dll");

SetHookFunc = (SetHook)GetProcAddress(hModule, "SetHook");
SetHookFunc(hModule);
while(true)
{
Sleep(100);
}
return 0;
}

//Hook.dll file
static HHOOK hKBHook;

extern "C"
{
_declspec (dllexport) LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
{
//some processing
}

_declspec (dllexport) void SetHook(HMODULE hMod)
{
HMODULE hDLL = hMod;

hKBHook = SetWindowshookEx(WH_KEYBOARD, KeyboardProc, hDLL, 0);
printf("SetHook\n");
}
_declspec (dllexport) void UnsetHook(void);
{
//some processing
}
_declspec (dllexport) BOOL APIENTRY DLLMain()
{
//some processing
}
}


I compile then debug by breakpoint using VS2008, but after successful return from SetHook, callback function is not called, I tried typing on a notepad application, or the actual VS2008 or the cmd prompt used for debugging, but nothing happens.

Can anyone help me.
thanks!
This function will do the nescessary manipulations for you: SetWindowsHookEx[^]

Hope that helps!

Cheers!
 
Share this answer
 
Comments
Manfred Rudolf Bihy 13-Feb-11 10:16am    
I just realized Nishant posted a link to a great article. Please also look there!
Thanks!
Nish Nishant 13-Feb-11 10:16am    
Voted 5, the API you gave him is all the OP really needs.
Manfred Rudolf Bihy 13-Feb-11 10:18am    
Thanks! I appreciate it.
Sergey Alexandrovich Kryukov 13-Feb-11 11:57am    
Quite enough, my 5.
--SA
Manfred Rudolf Bihy 13-Feb-11 12:06pm    
Thanks SA!
I hope you don't mind refering to you as SA as always seem to break my fingers when type SAkryukov. :)

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