Jump to content
  • Advertisement
Sign in to follow this  
kyoto

Get window w/ keyboard focus

This topic is 4742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings. I need to keep track of which child window has keyboard focus for the current foreground window. Right now i'm attempting to use AttachThreadInput then GetFocus to obtain a handle to the window with focus. I can't get AttachThreadInput to work however :(. Can someone please tell me what i'm doing wrong?
DWORD CurrentProcessID=GetCurrentProcessId();
HWND hOldWindow = 0;
while(true)
{
Sleep(1000);
HWND hNewWindow = GetForegroundWindow();
if(hNewWindow != hOldWindow)//foreground window has changed
{
	DWORD id=0;
	if(hOldWindow)
	{
		//detach from old window
		GetWindowThreadProcessId(hOldWindow, &id);
		if(!AttachThreadInput(id, CurrentProcessID,false))
			break;
	}

	GetWindowThreadProcessId(hNewWindow, &id);
	if(!AttachThreadInput(id, CurrentProcessID,true))//error!
		break;
	hOldWindow = hNewWindow;
}
else
{
	HWND hWindowInFocus = GetFocus();

	char szClassName[128] = {0};
	char szWindowText[128] = {0};
	GetClassName(hWindowInFocus, szClassName, 128);
	GetWindowText(hWindowInFocus, szWindowText, 128);
	
	SendMessage(hWindowInFocus, WM_GETTEXT, 128, (LPARAM)szWindowText);


	outFile <<"[" << szClassName<<"]" << szWindowText << endl <<flush;
}

}




[Edited by - kyoto on May 31, 2005 10:02:33 PM]

Share this post


Link to post
Share on other sites
Advertisement
I believe you are passing in the wrong ID for the first parameter of AttachThreadInput(). GetWindowThreadProcessId() returns the thread ID and sets the process ID via the second parameter. You are only paying attention to the process ID and passing that to AttachThreadInput() when you should be paying attention to the thread ID instead.

Also, to nitpick a little, instead of true and false you should be using the Win32 constants TRUE and FALSE.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!