troubleshooting Question

How to Detect WM_KEYUP and WM_KEYDOWN in a KeyboardHook procedure using WPARAM and LPARAM

Avatar of ramavorray
ramavorrayFlag for United Kingdom of Great Britain and Northern Ireland asked on
C++Microsoft Development
29 Comments1 Solution9277 ViewsLast Modified:
I have a KeyboardHook (as a DLL) which senses all keyboard activity and it is of type WH_KEYBOARD

The keyboard hook is working well and the keyboard callback procedure is called with parameters WPARAM and LPARAM

I am able to sense the key pressed (like VK_A, VK_DOWN) but I am not able to sense KEYUP/KEYDOWN

I tried many alternatives including code found online (even on EE) but it doesn't work.

When a key is hit my Keyboard Callback Procedure is being called multiple times instead of only one time (that is, when key is pressed)

Code snippets are attached.

I am using Visual Studio 2005 and running on Windows XP Professional 32bit system
//Keyboard Hook Procedure inside in the DLL which posts a message to my application
KEYDLL3_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
{
	if(ncode>=0)
	{
		if((lparam & 0x80000000) == 0x00000000)//Check whether key was pressed(not released).
		{
			hwnd = FindWindow("#32770","Keylogger Exe");//Find application window handle
			PostMessage(hwnd,WM_USER+755,wparam,lparam);//Send info to app Window.
		}
	}
	return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
}
 
 
 
//the keyboard callback function in my application which received 
//WPARAM and LPARAM. During the multiple number of times this function 
//is getting called it is always going to the line if(w == VK_SPACE || w == VK_DOWN). It is not resulting any KEYUP/KEYREPEAT events
LRESULT CKeyexeDlg::processkey(WPARAM w, LPARAM l)//This block processes the keystroke info.
{
	if (l & 0x80000000) // check bit 31 for up/down
	{
		//KEY UP
	}
	else
	{
		if (l & 0x40000000) // check bit 30 for previous up/down
			//KEY REPEAT
 
		else
		{
			if(w == VK_SPACE || w == VK_DOWN)
			{
				//the virtual character
			}
		}
	}
 
//I tried the other alternative. But it doesn't even enter into the switch block.
	LPMSG m=(LPMSG)l;
	switch(m->message)
	{
	case WM_KEYDOWN:
		l=l;
		break;
	case WM_KEYUP:
		break;
	case WM_CHAR:
		break;
	};
ASKER CERTIFIED SOLUTION
JohnGaby

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 29 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 29 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros