[.net] DirectXInput

Started by
3 comments, last by Krisc 18 years ago
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.
Advertisement
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]
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
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 frameelse if keyState[ Key.Right ]  //Move right this frameelse ...
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker
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]
You can see my working lame class that does DirectInput from the Keyboard here

This topic is closed to new replies.

Advertisement