Creating a DirectInput Device

To get input data from a device, you first have to create an object to represent that device.

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 globally unique identifier (GUID) that identifies the instance of the device for which the interface is to be created. 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 of them 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.