Sign in to follow this  

[.net] DirectXInput

This topic is 4262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, could someone pls teach me how to read keyboard input with DirectXInput. I wrote a simple game using Window Form's OnKeyDown()& OnKeyUP() event handlers, and i'm moving on to DirectXInput. So far i've managed to get this far: DirectXInput.Device inputDevice = null; inputDevice = new DI.Device(SystemGuid.Keyboard); inputDevice.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); inputDevice.Acquire(); now, where do i go from there? (i need to gather key inputs to move my game character.) I'd appreciate any help. Thanks.

Share this post


Link to post
Share on other sites
I'm going to be the guy that replies, but does not help answer your question at all. Now that that is out of the way:

DirectInput is unecessary in 99% of the cases. The only reasons DirectInput is necessary is to:
Read from multiple mice (mouses) on the same computer.
Read from multiple keyboards on the same computer.
Read from a joystick.

I would suggest using win32 msgs. You program has to process them no matter what, so you might as well remember which keys the player hit. The trick is to set variables when the keys get pressed and clear the variables when the keys get released. Sounds trivial, but you wouldn't believe how many people just read in that the player pressed left and move the player left a little. Its much better to move the player left continuously (preferably based on time) until the user releases the button.

And about your question, read the tutorials, thats what they are there for.

Edit: I almost forgot about the new XInput that got released with DirectX 10. It includes some nice functions for reading/manipulating the xbox 360 controller.

[Edited by - blaze02 on April 17, 2006 4:11:28 AM]

Share this post


Link to post
Share on other sites
Try something like this for responding to pressed keys.
foreach ( Key k in inputDevice.GetPressedKeys( ) )
{
if ( k == Key.Left )
//Move left this frame
else if ( k == Key.Right )
//Move right this frame
else ...
}

Another way would be to get all the key states at once using
KeyboardState keyState;
keyState = inputDevice.GetCurrentKeyboardState( );

if ( keyState[ Key.Left ] )
//Move left this frame
else if keyState[ Key.Right ]
//Move right this frame
else ...

Share this post


Link to post
Share on other sites
Quote:
Original post by blaze02
...
DirectInput is unecessary in 99% of the cases. The only reasons DirectInput is necessary is to:
Read from multiple mice (mouses) on the same computer.
Read from multiple keyboards on the same computer.
Read from a joystick.
...


On Windows XP, it´s not possible to distinguish between multiple mice or keyboards. All you get from DirectInput is a combined output of them all.

Microsoft suggests to use WM_INPUT, you can find information about it here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/rawinput/rawinputreference/rawinputmessages/wm_input.asp

[Edited by - mikesc on April 17, 2006 6:32:48 AM]

Share this post


Link to post
Share on other sites

This topic is 4262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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