Compartilhar via


Using DirectInput

This topic is a guide to implementing Microsoft DirectX. For a broader overview, see Understanding DirectInput.

This programming guide also provides the following subsections:

Creating DirectInput

The first step in any Microsoft DirectInput application is obtaining the IDirectInput8 Interface. You can do this most easily by calling the DirectInput8Create function.

You should create a single DirectInput object and not release it until the application terminates.

DirectInput Device Enumeration

DirectInput can query the system for all available input devices, determine whether they are connected, and return information about them. This process is called enumeration.

If your application is using only the standard keyboard or mouse, or both, you do not need to enumerate the available input devices. As explained under Creating a DirectInput Device, you can use predefined global variables when calling the IDirectInput8::CreateDevice method.

The following code example, where lpdi is a pointer to the IDirectInput8 Interface interface, creates a keyboard device:

LPDIRECTINPUTDEVICE8  lpdiKeyboard; 

lpdi->CreateDevice(GUID_SysKeyboard, &lpdiKeyboard, NULL); 

The first parameter in IDirectInput8::CreateDevice is an instance GUID that identifies the instance of the device for which the interface is to be created. Microsoft DirectInput has two predefined GUIDs, GUID_SysMouse and GUID_SysKeyboard, which represent the system mouse and keyboard. You can pass one of these identifiers into the IDirectInput8::CreateDevice method. The global variable GUID_Joystick should not be used as a parameter for IDirectInput8::CreateDevice because it is a product GUID, not an instance GUID.

Note

If the computer has more than one mouse, input from all mice is combined to form the system device. The same is true for multiple keyboards.

DirectInput provides four other predefined GUIDs primarily for testing.

  • GUID_SysKeyboardEm

  • GUID_SysKeyboardEm2

  • GUID_SysMouseEm

  • GUID_SysMouseEm2

Passing one of these GUIDs to IDirectInput8::CreateDevice grants access to the system keyboard or mouse through an emulation layer, at either level 1 or level 2. These GUIDs always represent the system mouse or keyboard. They are aliases for GUID_SysKeyboard and GUID_SysMouse, so they are not enumerated by IDirectInput8::EnumDevices or IDirectInput8::EnumDevicesBySemantics unless the DIEDFL_INCLUDEALIASES flag is passed.

For devices other than the system mouse or keyboard, use the instance GUID for the device returned by IDirectInput8::EnumDevices or IDirectInput8::EnumDevicesBySemantics. The instance GUID for a device is always the same. You can allow the user to select a device from a list of those enumerated, then save the GUID to a configuration file and use it again in later sessions.