Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 14 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.