14

If I alt-tab and do stuff outside of the game window, the game still captures mouse clicks (both left and right clicks).

The game doesn't "consume" the clicks, since they are also sent to the application that has the focus.

But it's rather annoying to hear a gunshot each time you click in your internet browser...

Also, the problem occurs even if the game is set to fullscreen.

1 Answers1

16

The best solution I have found to this is to stop responding to input (and probably pause your game as well) if it goes inactive.

You can check Game.IsActive or hook the Activated and Deactivated events.

There is one minor drawback - I would just about consider this a bug in the framework - the game always starts in the Active state, even if the game isn't actually active when it starts running. (For example: you click to another window between launching your game and your game actually starting.)

You could work around this by checking what the active window is when your game starts, probably using GetForegroundWindow. Or you could just not worry about it - it occasionally happens as a developer, but I suspect it doesn't really affect end-users.

Andrew Russell
  • 21,268
  • 7
  • 56
  • 103