Microsoft DirectX 8.1 (Visual Basic)

Creating a DirectInput Device

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

The DirectInput8.CreateDevice method is used to obtain a DirectInputDevice8 object. Methods of this interface are then used to manipulate the device and obtain data.

The following code example, where di is the DirectInput8 object, creates a keyboard device:

Dim diDev As DirectInputDevice
Set diDev = di.CreateDevice("GUID_SysKeyboard")

The parameter is an alias for a GUID that identifies the instance of the device for which the interface is to be created. Microsoft® DirectInput® provides two predefined GUIDs, represented by the strings GUID_SysMouse and GUID_SysKeyboard, which stand for the system mouse and keyboard. You can pass either of these to the CreateDevice method.

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.

For devices other than the system mouse or keyboard, use the instance GUID for the device obtained from DirectInputDeviceInstance8.GetGuidInstance. 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.

In the following code example, it is presumed that the application has enumerated devices and found a suitable one, diDevInstance, which is to be created as a DirectInputDevice:

Dim guid As String
guid = diDevInstance.GetGuidInstance
Set diDev = di.CreateDevice(guid)

For more information on obtaining the DirectInputDeviceInstance8 object, see DirectInput Device Enumeration.