Game Development Community

Window Focus Issues

by Harry Durnan · in Torque Game Builder · 06/27/2012 (8:56 am) · 7 replies

So, I know there is a call back onWindowFocusChange() for when the state of focus of a window changes, but is there anyway to check the state of the window's focus without having it change?

The basic issue I'm having is that if you start a game, but switch focus away while it is still loading up, any keyboard input still hits the game. I have code to deactivate keyboard input inside OnWindowFocusChange, but if you start it up without focus - it stays active.

I didn't see any currently coded ways of calling the state, and I'm not very familiar with modifying the engine from scratch. It looks like the variable windowActive in winWindow.cc keeps track of the focus, but I'm not sure how to write a function to return it.

I tried simply adding:

bool getWindowFocus()
{
return (windowActive);
}

to winWindow.cc, but I get an error trying to call it from TorqueScript. What else do I need to do to be able to make a new call to return the state of this variable?

#1
06/27/2012 (9:50 am)
In order to use that function in torquescript, you need to add a console function macro for it in the source, try this:

consoleFunction(getWindowFocus, bool, 1, 1, "- returns whether or not the window is active.")
{
return (windowActive);
}

The syntax is "consoleFunction(<functionName>,<return type>,<min args>,<max args (or -1 for inf)>,<description>)
#2
06/27/2012 (10:23 am)
Hmm, interesting. As a follow up question, any idea why it would compile fine under TGBGAME, but give the following errors under TorqueGameBuilder?

1>consoleFunctions.cc
1>..\..\source\console\consoleFunctions.cc(1408) : error C2065: 'getWindowFocus' : undeclared identifier
1>..\..\source\console\consoleFunctions.cc(1408) : error C2062: type 'bool' unexpected
1>..\..\source\console\consoleFunctions.cc(1409) : error C2143: syntax error : missing ';' before '{'
1>..\..\source\console\consoleFunctions.cc(1409) : error C2447: '{' : missing function header (old-style formal list?)

#3
06/27/2012 (10:40 am)
Nevermind, it's ConsoleFunction (case sensitive) - which fixed the errors. Hmm, still having trouble getting it to take it without giving me an error in Torquescript. Does it need to be in the ConsoleFunctions.cc, or can it be in the WinWindow.cc? It translated fine in the latter, but I get an error about windowActive not being declared in the former.
#4
06/27/2012 (4:15 pm)
Since it deals with the window and requires the static variable "windowActive", I would put it at the end of the winWindow.cc file.

Here is what I added to mine - I tested it and it worked.

ConsoleFunction( getWindowActive, bool, 1, 1, "()" )
{
	return windowActive;
}

Assuming you are using VC 2008, I would check your compilation configuration under Build->Configuration Manager. If you build it in debug mode it won't work when you start it normally. (Debug mode is only used when you launch via VC, which is actually incredibly helpful for debugging crashes!)

Note: I believe in order for any engine changes to take effect on a mac, you have to compile the solution in xcode (most likely on a mac), and in this case you'd have to modify "macCarbVideo.cc" as well. So be aware.
#5
06/27/2012 (4:30 pm)
Yup, I think I was compiling it wrong :o I managed to get it working :)Thanks for the help!
#6
07/15/2012 (10:46 am)
Hmm, still having a small issue with this. If you start running the game and immediatly tab away, it still takes in keyboard input even though the game doesn't have focus. I'm guessing mistakenly not realizing that it lost it. But, if you let it load up for a few seconds and then alt-tab away, it recognizes that focus has been lost correctly.

#7
07/15/2012 (10:51 am)
Actually, think I figured this out right after I posted the above (figures)... I think I just needed to move my pause code back into immediately after the canvas is created in the common stuff, and not in the game startup code.