Window Focus

Window focus just means that the game window is the current window in the foreground.
Let's say you are playing a game, which requires keyboard input. You switch to another
window, and start typing. But, you notice that the game is still responding to your input!
That's bad, a window that doesn't have focus shouldn't respond to user input, in most cases.

In this article, we will explore how to prevent user input from being processed when the window
loses focus. Let's get started.

Example Code

I will assume you know what public static classes/singletons are, since this example will be using
one, to keep it super simple. First, let's create a class that contains a bool to represent if our
game has focus or not.



Next, let's subscribe our game to the activated and deactivated events.



Then later in code, we can check to see if the window has focus like so.



There are many different ways to accomplish this, so feel free to make your own to fit
into your game, according to the patterns you have chosen.