Click to See Complete Forum and Search --> : keyboardhook


Crazy D
February 3rd, 2000, 07:18 AM
Hi,
I created a kind of toolbar control; it has commandbuttons on it, and the user can both click or use the function keys.
I set up a keyboard hook so you don't have to use the forms keydown event to check for the funtionkeys. I save a pointer to the usercontrol as property of the window and I use GetActiveWindow to get the hwnd to read out the property; copymemory to copy the pointer to a variable of the type usercontrol and I call a function. That function raises an event. This works nice. Oh, I setup the hook in the show event of the control and remove it in the hide event.
But the problem is, when I show a 2nd form as vbModal and both forms do have the control placed on it, the form that is shown modal gets the event twice. I'm sure this is because of the CallNextHook function. It works perfectly if I return 1 in my keyboardproc, but I'm wondering what can happen if I do that. I haven't setup much keyboard hooks (actually none,, wanted to do it using subclassing, but that didn't work...) and I'm not sure if I can safely return 1. This is what msdn says:

"If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_KEYBOARD hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure"

Any thoughts on this?

Crazy D :-)
"One ring rules them all"

Aaron Young
February 3rd, 2000, 08:31 AM
What you could do, if I read how you set it up correctly, is to check in your KeyboardProc to see if the ActiveWindow is the Window with the UserControl of this Particular Hook, if so Process the message and return a non zero value, else return the value of CallNextHookEx to pass the Message along the Thread.

Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com

Crazy D
February 3rd, 2000, 11:33 AM
That's what I'm basically doin, yes. Although GetActiveWindow returns me the MDI window, i can enum the childs and that works fine.
I worked on it in a testproject; with a main MDI window and a handfull of childs. This basically works fine, in the testproject.
But, in the real project, I found this out just before I went home, when I put a msgbox in the enum childs functions with the classname, I get an MDIChild first, and 2nd vb's thunderform, this works perfect. But as soon as I get rid of the msgbox, I get in a neverending loop and need to use the taskmanager to close VB :-( I'll figure that tomorrow (I hope... :-)

Crazy D :-)
"One ring rules them all"