if ( 0 != mouseButtons[0] )
Console.Writeline( "Primary Button pressed" );
[MDX] Mouse Input
How do you properly check for mouse input in a DirectX application? In the tutorials I'm reading they give the lines:
But it doesn't declare mouseButtons as any sort of array, or explain why it's there.
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.
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).
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.
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.
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.
Here's an example of an Input class using DirectInput (although DirectInput is going away, but so is mdx so hey...)
And here's how you can use it in your render/update function.
Hope this helps.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement