Sign in to follow this  
Kenny77

[MDX] Mouse Input

Recommended Posts

Kenny77    164
How do you properly check for mouse input in a DirectX application? In the tutorials I'm reading they give the lines:
if ( 0 != mouseButtons[0] )
   Console.Writeline( "Primary Button pressed" );

But it doesn't declare mouseButtons as any sort of array, or explain why it's there.

Share this post


Link to post
Share on other sites
Evil Steve    2017
The same way you do in a normal application. I'm not a C# person, so I can't help there, but in C++ you'd respond to WM_LBUTTONDOWN and so on, and manage the array yourself.

Share this post


Link to post
Share on other sites
Kenny77    164
I've tried out the example from the documentation, but I still can't figure out how to get input the way I want it (just like the keyboard, using an if statement to check for one button at a time, not every button).

Share this post


Link to post
Share on other sites
AvengerDr    751
In my User Interface library, I've hooked into the windows forms message system. I've created delegate methods and attached them to the onmousedown/clieck/up/etc events of the form container. When those events are fired, you'll find the pressed mousebutton into the MouseEventArgs structure.

Share this post


Link to post
Share on other sites
Blacal    122
Hello

you can use DirectInput. Just look into the Managed DirectX Documentation. Using DirectInput you can create a device object for you keyboard and then loop over all keys pressed at the moment.

Share this post


Link to post
Share on other sites
glaeken    294
Here's an example of an Input class using DirectInput (although DirectInput is going away, but so is mdx so hey...)


using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX;
//using Microsoft.DirectX.Direct3D;

namespace TerrainDemo
{
public class Input
{
private Device mMouse;
private Device mKeyboard;
private KeyboardState mKeyState;
private MouseState mMouseState;
private byte[] mMouseButtons;

public Input(TerrainDemo terrainDemo)
{
mMouse = new Device(SystemGuid.Mouse);
mKeyboard = new Device(SystemGuid.Keyboard);

mMouse.SetCooperativeLevel(terrainDemo, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
mKeyboard.SetCooperativeLevel(terrainDemo, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);

mKeyboard.Acquire();
mMouse.Acquire();
}

public void poll()
{
mKeyState = mKeyboard.GetCurrentKeyboardState();
mMouseState = mMouse.CurrentMouseState;

mMouseButtons = mMouseState.GetMouseButtons();
}

public bool keyDown(string key)
{
//example for getting a key //
if (key.Equals("W") && mKeyState[Key.W])
return true;

else if (key.Equals("S") && mKeyState[Key.S])
return true;

else if (key.Equals("A") && mKeyState[Key.A])
return true;

else if (key.Equals("D") && mKeyState[Key.D])
return true;
else
return false;
}

public bool MouseLeftPressed()
{
byte[] mouseButtons = mMouseState.GetMouseButtons();
if(mMouseButtons[0] != 0)
return true;
return false;
}

public bool MouseRightPressed()
{
if(mMouseButtons[1] != 0)
return true;
return false;
}
public double mouseX
{
get {return mMouseState.X;}
}

public double mouseY
{
get {return mMouseState.Y;}
}

public double mouseZ
{
get {return mMouseState.Z;}
}
}
}





And here's how you can use it in your render/update function.

mInput.poll();

float deltaX = (float)mInput.mouseX * 0.005f;
float deltaY = (float)mInput.mouseY * 0.005f;

if (mInput.keyDown("W"))
{
//just an example
mCamera.Walk(mLandVelocity * mElapsedTime);

}





Hope this helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this