# DirectX/10.0/Direct3D/Direct Input

This tutorial will cover using Direct Input in DirectX 11. The code in this tutorial will be based on the previous font tutorial code.
Direct Input is the high speed method of interfacing with input devices that the DirectX API provides. In DirectX 11 the Direct Input portion of the API has not changed from previous versions, it is still version 8. However Direct Input was implemented very well in the first place (similar to direct sound) so there hasn't been any need to update it. Direct Input provides incredible speed over the regular windows input system. Any high performance application that requires highly responsive input devices should be using Direct Input.
This tutorial will focus on how to implement Direct Input for keyboard and mouse devices. We will also use the TextClass to display the current location of the mouse pointer. As the previous tutorials already had an InputClass we will just re-write it using Direct Input instead of the Windows methods that were previously used.

## Inputclass.h

You need to define the version of Direct Input you are using in the header or the compiler will generate annoying messages that it is defaulting to version 8.
The following two libraries need to be linked for Direct Input to work.
This is the required header for Direct Input.
The first three private member variables are the interfaces to Direct Input, the keyboard device, and the mouse device.
The next two private member variables are used for recording the current state of the keyboard and mouse devices.

## Inputclass.cpp

The class constructor initializes the Direct Input interface variables to null.
This function call will initialize the interface to Direct Input. Once you have a Direct Input object you can initialize other input devices.
The first input device we will initialize will be the keyboard.
Setting the cooperative level of the keyboard is important in both what it does and how you use the device from that point forward. In this case we will set it to not share with other programs (DISCL_EXCLUSIVE). This way if you press a key only your application can see that input and no other application will have access to it. However if you want other applications to have access to keyboard input while your program is running you can set it to non-exclusive (DISCL_NONEXCLUSIVE). Now the print screen key works again and other running applications can be controlled by the keyboard and so forth.
Just remember that if it is non-exclusive and you are running in a windowed mode then you will need to check for when the device loses focus and when it re-gains that focus so it can re-acquire the device for use again. This focus loss generally happens when other windows become the main focus over your window or your window is minimized.
Once they keyboard is setup we then call Acquire to finally get access to the keyboard for use from this point forward.
The next input device we setup is the mouse.
We use non-exclusive cooperative settings for the mouse. We will have to check for when it goes in and out of focus and re-acquire it each time.
Once the mouse is setup we acquire it so that we can begin using it.
The Shutdown function releases the two devices and the interface to Direct Input. Notice that the devices are always un-acquired first and then released.
The Frame function for the InputClass will read the current state of the devices into state buffers we setup. After the state of each device is read it then processes the changes.
ReadKeyboard will read the state of the keyboard into the m_keyboardState variable. The state will show any keys that are currently pressed or not pressed. If it fails reading the keyboard then it can be for one of five different reasons. The only two that we want to recover from are if the focus is lost or if it becomes un-acquired. If this is the case we call acquire each frame until we do get control back. The window may be minimized in which case Acquire will fail, but once the window comes to the foreground again then Acquire will succeed and we will be able to read the keyboard state. The other three error types I don't want to recover from in this tutorial.
ReadMouse will read the state of the mouse similar to how ReadKeyboard read in the state of the keyboard. However the state of the mouse is just changes in the position of the mouse from the last frame. So for example updates will look like the mouse has moved 5 units to the right, but it will not give you the actual position of the mouse on the screen. This delta information is very useful for different purposes and we can maintain the position of the mouse on the screen ourselves.
The ProcessInput function is where we deal with the changes that have happened in the input devices since the last frame. For this tutorial we will just do a simple mouse location update similar to how Windows keeps track of where the mouse cursor is. To do so we use the m_mouseX and m_mouseY variables that were initialized to zero and simply add the changes in the mouse position to these two variables. This will maintain the position of the mouse based on the user moving the mouse around.
Note that we do check to make sure the mouse location never goes off the screen. Even if the user keeps moving the mouse to the left we will just keep the cursor at the zero position until they start moving it to the right again.
I have added a function to the InputClass called IsEscapePressed. This shows how to utilize the keyboard to check if specific keys are currently being pressed. You can write other functions to check for any other keys that are of interest to your application.
GetMouseLocation is a helper function I wrote which returns the location of the mouse. GraphicsClass can get this info and then use TextClass to render the mouse X and Y position to the screen.

## Systemclass.cpp

I will just cover the functions that changed with the removal of the Windows Input system and addition of the DirectX Input system.
Initialization of the Input object is now different as it requires handles to the window, instance, and the screen size variables. It also returns a boolean value to indicate if it was successful or not in starting Direct Input.
Releasing the Input object now requires a Shutdown call previous to deleting the object.
The check for the escape key in the Run function is now done slightly different by checking the return value of the helper function in the InputClass.
During the Frame function we call the Input object's own Frame function to update the states of the keyboard and mouse. This call can fail so we need to check the return value.
After the input device updates have been read we update the GraphicsClass with the location of the mouse so it can render that in text on the screen.
We have removed the Windows keyboard reads from the MessageHandler function.
Direct Input handles all of this for us now.

## Graphicsclass.h

The Frame function now takes two integers for the mouse position updates each frame.

## Graphicsclass.cpp

I will just cover the functions that have changed since the previous tutorial.
The Frame function now takes in the mouse X and Y position and then has the TextClass object update the text strings that will write the position onto the screen.

## Textclass.h

TextClass now has a new function for setting the location of the mouse.

## Textclass.cpp

I will just cover the functions that have changed since the previous tutorial.
We now have a new function in the TextClass which converts the mouse X and Y position into two strings and then updates the two sentences so that the position of the mouse can be rendered to the screen.

## Summary

As you can see setting up Direct Input in DirectX 11 is very simple and it gives us high speed access to information from the input devices.

## To Do Exercises

1. Recompile and run the program. Move the mouse around the screen and watch the text position update.
2. Use the information from the 2D Rendering tutorial and combine it with this one to create your own mouse cursor that moves with the mouse movement.
3. Implement a function that reads the keyboard buffer and displays what you type on the screen.