Mouse click detection

I have mouse tracking and clicking working a dream, really sinple, very good indeed.

Except one small thing, if you have a window over the top of your game window (Windows desktop) the mouse click 'goes through" the top window to the game beneath.

Is there a way to stop this behaviour?


My suggestion would be to only acknowledge mouse clicks if the game window is active. ‘Game’ has an attribute called ‘IsActive’ that tracks this.

A small warning though: it currently defaults to true in 3.6 on launch, even if you’re focused on another window. You have to actually make the game window active first before it registers it correctly.

That sounds pretty much perfect. Thanks gap.

should I be doing this for keyboard detection too?

Just to make sure I understand:
Private G As New Game
If G.IsActive Then
End If

And I need to G.IsActive somewhere in the startup procedures?

Thanks again.

I do it for both mouse and keyboard input. People don’t want to accidentally mess with game settings or whatever while they’re on the internet.

IsActive should be accessible in Game1 already because it inherits Game. So if your keyboard / mouse class has access to your Game1 instance, just do


I’m not sure what you mean by the last question, but if it’s referring to my warning above, let me try to explain it better:

IsActive returns true if the game window is the active window, and false if it isn’t. However, when you first launch the game, it will default to true even if you haven’t clicked on the window yet. So, if you launch your game and while it builds you decide to write an email, Game1.IsActive will be returning true even though you haven’t clicked the game window yet. So it will be registering all of your key strokes. Once you click the game window it will start being correct. I think this is a bug, but just be aware of it.