Game Development Community

Detecting whether Torque window has focus

by Ian Omroth Hardingham · in Torque Game Engine · 09/01/2009 (8:49 am) · 2 replies

Hey guys.

I have a very simple question. How do I find out if the Torque window has focus (ie, is the "selected" window).

Thanks,
Ian

#1
09/04/2009 (3:39 pm)
HWND window = GetForegroundWindow();
if (window && window != winState.appWindow && gWindowCreated)
{
// check to see if we are in the foreground or not
// if not Sleep for a bit or until a Win32 message/input is recieved
DWORD foregroundProcessId;
GetWindowThreadProcessId(window, &foregroundProcessId);
if (foregroundProcessId != winState.processId)
MsgWaitForMultipleObjects(0, NULL, false, getBackgroundSleepTime(), QS_ALLINPUT);
}
// if there's no window, we sleep 1, otherwise we sleep 0
else if(!Game->isJournalReading() && gWindowCreated )
Sleep( 1 ); // give others some process time if necessary...
#2
09/09/2009 (3:28 pm)
Somewhere around line 714 in winWindow.cc

case WM_ACTIVATE:
         windowActive = LOWORD(wParam) != WA_INACTIVE;
         if( Game->isRunning() ) 
         {
            if (Con::isFunction("windowFocusChanged"))
               Con::executef( 2, "windowFocusChanged", windowActive ? "1" : "0" );
         }

In TorqueScript:

function windowFocusChanged(%state)
{
   if (%state == 1)  
   {
       // Window got the focus
   } 
   else     
   {
      // Window lost the focus
   }
}