Keys.LButton VS MouseState.LeftButton?

(Edit: I’ve double-checked and I’m looking at Windows Form documentation, not XNA Input. But I think my question is still valid - as in “Why shouldn’t I just use Windows Form instead for both Keyboard and Mouse?” Or what?)

What are the pros and cons of using Keys (of Windows Forms, not XNA) instead of MouseState / ButtonState (of XNA Input) for Mouse input detection? I know that ButtonState provides Pressed and Released events, but I need a Held event as well, so I might as well write those three methods myself, since they’re literally just 2-3 lines long. Or are there other reasons why the Mouse class must be used?

// Edit: System.Windows.Input.Keyboard, not KeyboardState)
if (OldSystemState.IsKeyDown(Keys.LButton)
    && newSystemState.IsKeyDown(Keys.LButton))
{ 
    DoStuff();
}

Versus:

// _mouse object:
if (OldMouseState.LeftButton == ButtonState.Pressed
    && newMouseState.LeftButton == ButtonState.Pressed)
{
    DoStuff();
}

I’m in the middle of something else, but I’ll test it later myself. But if any of you have a quick answer or can explain why they both exist, then I’d appreciate it. Thanks.

Alright guys. I’ve heard elsewhere that using System is bad practice, in this case, and that I should be using XNA’s own Keyboard and Mouse classes. I got a solution for combining Keyboard and Mouse input into a single array (I’ll just write a higher level enum list, which is essentially what I’m doing right now, hence my question).

But if anyone wants to take a stab at this problem, then that’d be cool. I hear that XNA isn’t particularly good at combining input types (in the way that I want it, i.e. for the user to be able to rebind from a key to a mouse button, and vice versa).

Yeah, I recommend using the MG API, because it will keep your game portable.

It’s true that there’s a clear separation between Keyboard and Mouse input in the API. You could write an abstraction layer (an InputHandler or something) that handles mouse input as keys to work around this (by using a custom enum with all values of Keys and additionally values for mouse input).

1 Like

Yeah, my custom enum works like a charm. :slight_smile:

Thanks for the feedback though, either way. I been banging my head a lot but I got it all to work.

1 Like