0
\$\begingroup\$

I'm wondering how it's possible to detect if a mousebutton is pressed or released.

var di = new DirectInput();
_mouse = new Mouse(di);
_mouse.Acquire();
_mouseState = _mouse.GetCurrentState()

All I can access is now the X Y and Z Axis of the Mouse and the Boolean array Buttons. Any Ideas?

\$\endgroup\$
5
  • \$\begingroup\$ So, what is the problem? \$\endgroup\$ Commented Sep 4, 2013 at 13:03
  • \$\begingroup\$ How to detect if a Button is pressed? Like if I press the left button how do I get this information? \$\endgroup\$
    – Rey
    Commented Sep 4, 2013 at 13:05
  • \$\begingroup\$ It's probably set in _mouseState. \$\endgroup\$
    – Sidar
    Commented Sep 4, 2013 at 13:07
  • \$\begingroup\$ Rey, your edit has kind of changed this into a "fix my code" type question. Those types of questions aren't really welcome here. Maybe you can at least narrow down the code the the minimum code possible to show the problem area? \$\endgroup\$
    – House
    Commented Sep 4, 2013 at 14:37
  • \$\begingroup\$ Oh Ok no thats not my intention. I'm just editing it so its updated. I know the error is within my MouseButtonDown(this, new MouseEventArgs{Button = mb, Position = new Point { x = mouseEventArgs.X, y = mouseEventArgs.Y }}); segment or at least I think it is. I'm still working on it and as soon as i solved it I'll post it. wont edit it again now. Besides that the question is actually answered. to get the is pressed stat just access the array slot and check if its true. my current problem is related but nvm. I'm going to fix it soon \$\endgroup\$
    – Rey
    Commented Sep 4, 2013 at 15:03

2 Answers 2

1
\$\begingroup\$

The SharpDX MouseState.Buttons property is what you're after. As you've found, you can get access to the MouseState with GetCurrentState(). You can reference the order of the buttons you'll find in the MouseState.Buttons array here. (I'm pretty sure that "None" is not stored in the array, so start with "Left").

So, if you wanted to know if the left mouse button was pressed you'd check to see if its position in the array was set to True. Like so:

if(_mouseState.Buttons[0] == True)
    //Left button pressed
if(_mouseState.Buttons[2] == True)
    //Right button pressed

That'll tell you when the buttons are down. But how do you know exactly when the button was pressed? Well, you need to keep track of the state of the mouse separately. Keep your own copy of the Buttons array. Every frame, copy the current MouseState.Buttons array into another array called something like PreviousButtonsState. Now each frame you can do a check and find out if a button was just pressed. This allows you to respond only once to clicks. You can also trigger events for when the button is pressed, or when the button is released. For example:

if(_mouseState.Buttons[0] == True && PreviousButtonsState[0] == False)
    //Left button was just pressed! 
    OnMouseDown(0);
if(_mouseState.Buttons[2] == True && PreviousButtonsState[2] == False)
    //Right button was just pressed!
    OnMouseDown(2);
if(_mouseState.Buttons[0] == False && PreviousButtonsState[0] == True)
    //Left button was just released!
    OnMouseUp(0);


....


OnMouseDown(int button) {
    switch(button){
    case 0:
        //Do something when left button is pressed
        break;
    case 1:
        //Do something when middle button is pressed
        break;
    case 2:
        //Do something when right button is pressed
    } 
}

OnMouseUp(int button) {
    switch(button){
    case 0:
        //Do something when left button is released
        break;
    case 1:
        //Do something when middle button is released
        break;
    case 2:
        //Do something when right button is released
    } 
}
\$\endgroup\$
2
  • \$\begingroup\$ thanks Byte56 I tried this way and it didn't work properly. I edited my code again and inserted your example again. Because it's a nice Idea to do it that way. Anyways I added some Code into my "Question" \$\endgroup\$
    – Rey
    Commented Sep 4, 2013 at 14:06
  • \$\begingroup\$ Well, it looks like you have access to the events already, you don't need to do most of the above. See that RenderForm inherits from Form, which already implements OnMouseDown and OnMouseUp events. Just use the existing events. \$\endgroup\$
    – House
    Commented Sep 4, 2013 at 14:27
0
\$\begingroup\$

Ok so the error wasn't if it's pressed or not. These things were already right. My error was the MousePosition, it seems like sharpDx mouseposition is a bit different. So the Solution was instead of using the SharpDX mouse position X Y state, to use System.Windows.Forms.Cursor.Position.X Y thats all

\$\endgroup\$
1
  • \$\begingroup\$ So, the accepted answer is, you were asking the wrong question? This isn't really an answer to your question as it's asked. \$\endgroup\$
    – House
    Commented Sep 6, 2013 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .