Game Development Reference
In-Depth Information
Mouse and keyboard input
Even though players can play games using gamepads and other types of controllers,
mouse and keyboard input are still quite important in PC game development. Some
games have too many commands to map all of them on a gamepad, for example.
When we assign an in-game action to a specific button on a keyboard, mouse, or
gamepad, we say that we have mapped that action to that particular button. This is
also sometimes referred to as binding, because we are, in effect, binding a certain
key or button to a specific in-game action.
Let's first implement our mouse and keyboard input. Start Visual Studio and open
the solution we worked on in the previous chapter. We are going to add a new class
that will handle user input for us. Right-click on the SlimFramework project in the
Solution Explorer pane and add a new class named UserInput.cs . We will make
this class implement the IDisposable interface, just like we did with our GameWin-
dow.cs class in Chapter 1 , Getting Started . So, we need to change the class declar-
ation from public class UserInput to public class UserInput : IDis-
posable .
We also need to add two using statements to the top of this class file. One for Dir-
ectInput and one for XInput:
using SlimDX.DirectInput;
using SlimDX.XInput;
Now, we are ready to set up the member variables for our new user input class. We'll
create a member variables section just like we did in Chapter 1 , Getting Started . Here
is the code:
bool m_IsDisposed = false;
DirectInput m_DirectInput;
Keyboard m_Keyboard;
KeyboardState m_KeyboardStateCurrent;
KeyboardState m_KeyboardStateLast;
Search WWH ::




Custom Search