DirectInput
DirectInput is a component of DirectX that provides an interface for applications to access input devices such as keyboards, mice, joysticks, and gamepads. It offers a consistent and efficient way to handle input across a wide range of hardware.
Key Concepts
- Devices: Represents physical input hardware like a keyboard or joystick.
- Objects: Specific controls on a device, such as a key on a keyboard, a button on a mouse, or an axis on a joystick.
- Effects: Force feedback capabilities for devices that support them (e.g., vibration, spring effects).
- Acquire/Unacquire: The process of gaining or releasing control over an input device.
- Polling: Periodically querying the state of input devices.
Core Interfaces
DirectInput provides several core interfaces for managing input:
IDirectInput8: The main interface for initializing DirectInput and enumerating devices.IDirectInputDevice8: Represents an individual input device and provides methods for accessing its properties, capabilities, and data.IDirectInputEffect: Used for controlling and managing force feedback effects.
IDirectInput8 Methods
| Method | Description |
|---|---|
DirectInput8Create |
Initializes an instance of DirectInput. |
EnumDevices |
Enumerates the input devices available on the system. |
CreateDevice |
Creates an instance of an input device. |
IDirectInputDevice8 Methods
| Method | Description |
|---|---|
SetCooperativeLevel |
Sets the cooperative level for the device, determining how it interacts with other applications. |
GetDeviceState |
Retrieves the current state of the device's objects (e.g., key presses, axis positions). |
Acquire |
Acquires the device for input. |
Unacquire |
Releases the device. |
EnumEffects |
Enumerates the force feedback effects supported by the device. |
CreateEffect |
Creates a force feedback effect. |
Common Usage Patterns
- Initialization: Call
DirectInput8Createto get anIDirectInput8interface. - Device Enumeration: Use
EnumDevicesto find available devices and create specific device instances usingCreateDevice. - Setting Cooperative Level: Call
SetCooperativeLevelon the device interface to manage input focus. - Acquiring the Device: Call
Acquirebefore reading input. - Reading Input: Periodically call
GetDeviceStateto read the current state of the device. - Releasing the Device: Call
Unacquirewhen input is no longer needed or the application is exiting.
Note: DirectInput is part of the legacy DirectX API. For modern Windows applications, consider using the Raw Input API or the Game Input API for improved performance and broader device support.