DirectX Input
The DirectX Input component provides a unified interface for handling input from various devices, including keyboards, mice, joysticks, gamepads, and other human interface devices (HIDs). It simplifies the process of gathering input data, allowing developers to create more immersive and interactive applications and games.
Key Concepts
- Device Enumeration: Discovering and identifying available input devices connected to the system.
- Device Acquisition: Gaining exclusive or shared access to input devices.
- Input Data Acquisition: Retrieving real-time input states and events from devices.
- Force Feedback: Implementing haptic feedback for supported devices (e.g., joysticks).
- DirectInput vs. Win32 Input: Understanding the differences and when to use each.
Core Components and Interfaces
DirectInput utilizes several key COM interfaces:
IDirectInput8: The primary interface for interacting with the DirectInput system.IDirectInputDevice8: Represents a specific input device and provides methods for its management and data retrieval.DIDEVICEINSTANCE: Structure containing information about an input device.DIDEVCAPS: Structure describing the capabilities of a device.DIPROPRANGE,DIPROPSTRING, etc.: Structures for managing device properties.
Getting Started with DirectInput
The basic workflow for using DirectInput involves:
- Initialization: Create an instance of the
IDirectInput8interface. - Device Enumeration: Enumerate available devices and filter for the ones you need (e.g., keyboard, mouse, gamepad).
- Device Creation: Create an
IDirectInputDevice8object for each desired device. - Device Configuration: Set device properties, such as cooperative levels (exclusive/shared access) and device format (mapping input axes and buttons).
- Device Acquisition: Acquire the devices to begin receiving input.
- Input Polling/Event Handling: Periodically poll devices for their current state or set up notification mechanisms for input events.
- Data Processing: Interpret the input data to control game logic or application behavior.
- Cleanup: Release device objects and the DirectInput object when done.
Example: Acquiring Keyboard State
The following is a conceptual outline of how to acquire keyboard state:
#include <dinput.h>
// ... initialization ...
IDirectInput8* pDI = NULL;
DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&pDI, NULL);
IDirectInputDevice8* pKeyboard = NULL;
pDI->CreateDevice(GUID_SysKeyboard, &pKeyboard, NULL);
// Set cooperative level
pKeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
// Set data format
pKeyboard->SetDataFormat(&c_dfDIKeyboard);
// Acquire the device
pKeyboard->Acquire();
// Later, in your game loop:
DIDEVCAPTUREED_DEVICE_STATE keyboardState;
if (SUCCEEDED(pKeyboard->GetDeviceState(sizeof(DIDEVCAPTUREED_DEVICE_STATE), &keyboardState))) {
if (keyboardState.rgbButtons[DIK_SPACE] & 0x80) {
// Space bar is pressed
}
}
// ... cleanup ...
API Reference
Best Practices
- Always handle device loss and re-acquisition gracefully.
- Use appropriate cooperative levels to avoid interfering with other applications.
- Consider using DirectInput's axis mapping and dead zones for smooth analog input.
- For modern applications, consider the gamepad input features in the Windows.Gaming.Input API as an alternative or complement.