Skin designed by Auron89.Find more great skins at Silent Designs!
Image hosted by Photobucket.com


 

 Direct Input, Tired of Window's way to use keyboard ?
Chindril
Posted: Oct 8 2005, 03:48 PM


newcomer


Group: Members
Posts: 16
Member No.: 147
Joined: 6-October 05



This is a quick introduction to Direct Input, which is basicly an alternative way to use the keyboard and mouse. In my opinion, it works a LOT better.

This tutorial is used in a Direct3D application, but it can be used with anything else with little to no changes.

You need to add the Reference "Microsoft.DirectX.DirectInput"

Start by creating a class "clsInput"

CODE

Imports Microsoft.DirectX.DirectInput

Public Class clsInput
   Private mobjDeviceKeyboard As Device
   Private mobjDeviceMouse As Device
   Public MouseState As MouseState
   Public KeyboardState As KeyboardState

   Public Sub New()
       mobjDeviceKeyboard = New Device(SystemGuid.Keyboard)
       mobjDeviceKeyboard.Acquire()
       mobjDeviceMouse = New Device(SystemGuid.Mouse)
       mobjDeviceMouse.Acquire()
   End Sub

   Public Sub Poll()
       mobjDeviceKeyboard.Poll()
       mobjDeviceMouse.Poll()

       KeyboardState = mobjDeviceKeyboard.GetCurrentKeyboardState()
       MouseState = mobjDeviceMouse.CurrentMouseState()
   End Sub
End Class


That's it ! It's pretty small, isn't it ? That's all you need to do for the dInput class, never think about it again.

Now to use it, go in the GameClass class and declare :
Private dInput as clsInput

During the Initialization of the Device, use the constructor
CODE

dInput = new clsInput() 'Check the code in clsInput to see what it does

Finally, go to Render function and add those 3 lines.
CODE

D3Ddev.BeginScene()
....
.... Render your stuff here
....

   dInput.Poll()     'Update the state of Keyboard and Mouse
   PollForKeys()    'Create this function below
   PollForMouse()  'Same
D3Ddev.EndScene()



Private Sub PollForKeys()
   If dInput.KeyboardState(DirectInput.Key.Escape) Then 'This line to see which key is pressed
       GameOver = True 'Actions to do when it is pressed
   End If
   If dInput.KeyboardState(DirectInput.Key.W) Then
       MainCharacterController.MoveCharacter(0.5)
   End If
End Sub

Private Sub PollForMouse()
   WorldviewCamera.MoveCamera(dInput.MouseState.X, dInput.MouseState.Y)
   WorldviewCamera.ChangeDistance(dInput.MouseState.Z / 100)
End Sub

How the MouseState works ?
Basicly X and Y are a constant number, and NOT the position on the screen.
If you move your mouse left, X will be negative, or positive if you move it to the right. Same goes for Y and up and down.
Z is the wheel. 1 Roll = 100, which means I wanted to ChangeDistance(1 or -1) in the above code. Just play around with these and you will get the hang out of it.

This is a quick tutorial and there is only small code, but it is surely useful. One you made it, just reuse your class and never think about it again.

- Chindril
Top
The Pentium Guy
Posted: Oct 9 2005, 02:30 AM


The Boss.


Group: Admin
Posts: 743
Member No.: 1
Joined: 4-July 03



Nice job dude...I was actually planning on coming out with a dInput tutorial. It's pretty straightforward really, once you have it done, it's pretty much done.

Good job.
Top
Chindril
Posted: Oct 9 2005, 02:34 AM


newcomer


Group: Members
Posts: 16
Member No.: 147
Joined: 6-October 05



Thanks.

I read that you planned on writing a tutorial but I needed to learn it right away so I searched the web and I thought I'ld share this with you guys.

I personnaly love your tuto (they thought be how to use D3D) and I hope your forum will be filled with active members ;-)

- Chindril

P.S.: If you don't wanna write a full tutorial on Direct Input you can use mine and modify it as you wish.
Top
The Pentium Guy
Posted: Oct 27 2005, 09:32 PM


The Boss.


Group: Admin
Posts: 743
Member No.: 1
Joined: 4-July 03



"GetCurrentMouseState() no longer seems to be an existing method."
Wait WHAT?
Has the latest directX really changed that much?

mState = devMouse.CurrentMouseState
kState = devKeyboard.GetCurrentKeyboardState

Is exactly what I have in my code, and I'm on DirectX 9 (August 2005). It seems there's an October update. This might possibly be a bug in the new version. Or you could just use the DirectInput DLL from the older version...? I'll download/install the newer one later.
Top
Chindril
Posted: Oct 30 2005, 08:21 PM


newcomer


Group: Members
Posts: 16
Member No.: 147
Joined: 6-October 05



Actually I use the SDK October 2004. I know its old but it's working pretty well and I dont wanna check all of my code during each version. Anyway, onto the problem. I think it they just renamed the method.

By any chances, is there a "GetCurrentMouseState()" method ?

If you look at the code, you notice this

KeyboardState = mobjDeviceKeyboard.GetCurrentKeyboardState()
MouseState = mobjDeviceMouse.CurrentMouseState()

I always wondered why they didn't name both "GetCurrentBlablaState"

- Chindril
Top


Topic Options Quick Reply




Hosted for free by InvisionFree (Terms of Use: Updated 7/7/05) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.1511 seconds | Archive

Check out the Shoutbox! Type in your name and the message to shout ;).