# How to tell if a glut window has focus from c#

How can I tell if a glut window has focus? Im using c#/tao, Ill use p/invokes if necessary.

Basically I want to ignore input if it doesn't have focus.

• I've not used glut before, but it seems odd that a window would be receiving input messages if it didn't have focus. Are you sure this is something you need to do manually? – Mike Strobel Aug 9 '10 at 20:40
• Since these are not game development exclusive topics, this question belongs at Stack Overflow stackoverflow.com where you should get a much better response. – Ricket Aug 9 '10 at 20:41
• Im using GetAsyncKeyState for input rather that glut. – Sam Aug 12 '10 at 14:53

Here's the solution I've got so far

[DllImport("User32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
static extern IntPtr GetFocus();

public bool HasFocus(string windowName)
{
IntPtr windowHwnd = FindWindow(null, windowName);
if (windowHwnd == IntPtr.Zero)
return false;
return windowHwnd.ToInt32() == GetFocus().ToInt32()
}

• If you are ok with windows specific code, this would work fine i guess – Toad Aug 14 '10 at 13:11

I'd guess you have to use the regular win32 func to get the active window handle and compare it to your own. GLUT's focus functions don't behave as expected in Windows as they're based on move over to focus.
https://stackoverflow.com/questions/466354/how-can-i-tell-if-a-window-has-focus-win32-api

Edit: Wait...I assumed Windows. Not sure that's a correct assumption.

• On Windows, he may want to compare his window handle to both the active window and the foreground window, ensuring that both match. I vaguely recall a strange edge case in which the active window was not necessarily the foreground window. Or perhaps I've simply gone mad. – Mike Strobel Aug 11 '10 at 16:29