Input is a general term referring to the process of receiving actions from the user. In XNA Game Studio Express, the Microsoft.Xna.Framework.Input namespace provides support for three different input devices: the Xbox 360 Controller, the keyboard, and the mouse. This overview discusses the differences between these input devices and offers guidelines for how to use them with your game.
Digital vs. Analog
Input devices have two types of controls: digital and analog. Digital controls report only two possible states—on and off—and are represented by Boolean values. Examples of digital controls are the START button on the Xbox 360 Controller and any button on a keyboard.
Analog controls report a range of values, rather than just off and on. Examples of analog controls are the Xbox 360 Controller stick and the movements of the mouse. Analog values can be represented in a variety of ways. In XNA Game Studio Express, an analog value on the Xbox 360 Controller is represented as a floating-point value between −1.0 and 1.0 for the sticks, and between 0.0 and 1.0 for the triggers. For the mouse, XNA Game Studio Express reports mouse cursor values in pixels.
Polling and States
For all input devices in XNA Game Studio Express, input is received from the user by way of polling: each frame, call a function to receive the current state of an input device, and compare to previous states if necessary. A state is a snapshot of an input device's interactions from the user. At any given moment, a control may have a variety of buttons pressed, analog sticks or triggers held in a direction. The current positions of these buttons and analog controls are reported in a state structure. The following sections describe the methods for retrieving and using state for each input device.
Input Device Types
Each input device has specific advantages and disadvantages. The following table shows a brief comparison.
|Input Device||Digital Buttons||Analog Controls||Vibration Effects||Supported on Windows||Supported on Xbox 360||Number Allowed on System|
|Xbox 360 Controller||14||4||Yes||Yes||Yes||4|
Xbox 360 Controller
The Xbox 360 Controller provides a good combination of digital buttons and analog sticks, so that many types of games can be played with it.
The controller can be used on either Windows or Xbox 360 systems. Up to four controllers are supported.
How to Use
- Each frame of your game, call the static GetState method from the GamePad class.
- Pass in the index of the player you wish to query. The player associated with a controller is visible on the Ring of Light on the controller.
Query the GamePadState structure you get back to retrieve information on the state of the controller. For more information on retrieving state, including how to determine whether the user has pressed or released a button since the last frame, see How to: Detect Whether a Controller Button Is Pressed.
You may store a previous controller state if you wish to compare states to determine input changes from one frame to the next.
|Controllers may become disconnected during normal gameplay. For information on how to properly detect and handle this situation, see How to: Detect Whether a Controller Is Disconnected.|
The keyboard, with a multitude of digital buttons, is best for complicated simulations with many functions, or when typing text is necessary—but a keyboard has no analog controls for precise movement.
Not all keyboards support the entire range of buttons listed in Keys; for example, older keyboards may not support the VolumeDown, VolumeUp, and VolumeMute members.
If the user's keyboard is connected via USB, it can be used on a Windows system, or on an Xbox 360.
How to Use
- Each frame of your game, call the static GetState method from the Keyboard class.
Query the KeyboardState structure you get back to retrieve information on the state of the keyboard.
You may store a previous keyboard state if you wish to compare states to determine input changes from one frame to the next. See How to: Detect Whether a Key Is Pressed for more information.
The mouse, with its precise analog control, is best for selecting objects, such as in a strategy game, or for moving a viewport, such as in a first-person action game—but a mouse has few digital buttons. Modern mouse devices have three extra buttons in addition to the standard left and right buttons: two digital buttons, usually on the side of the mouse, and a digital button that is activated by clicking down on the scroll wheel. The scroll wheel itself functions as an extra analog control in two directions, but does not provide precise input.
Mouse devices are used on Windows systems only; they are not supported on Xbox 360.
How to Use
- When your game starts, assign the game's window to the mouse by setting the static WindowHandle property of the Mouse class. Often, you'll set this to the Window property of your derived Game class.
- Each frame of your game, call the static GetState method from the Mouse class.
Query the MouseState structure that is returned to retrieve information on the state of the mouse. For more information on mouse state, including an example, see How to: Get the Current Mouse Position (Windows).
You may store a previous mouse state if you wish to compare states to determine input changes from one frame to the next.