|
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
|