Microsoft DirectX 8.1 (Visual Basic) |
Some Human Interface Devices both generate input and accept output. The DirectInputDevice8.SendDeviceData method is used to send packets of data to such devices.
SendDeviceData can be viewed as DirectInputDevice8.GetDeviceData in reverse. As with that method, it uses the DIDEVICEOBJECTDATA type as the basic unit of data. In this case, however, the lOfs member contains the instance ID of the device object associated with the data, rather than its offset in the data format for the device. This ID can be extracted from the value returned by DirectInputDeviceObjectInstance.GetType after device objects have been enumerated. (Because offset identifiers exist only for device objects that provide input in the selected data format, an object that accepts only output might not even have an offset.) The lData member contains whatever data is appropriate for the object. The lTimeStamp and lSequence members are not used and must be set to 0.
To send data to the device, first set up an array of DIDEVICEOBJECTDATA types, fill the required number of elements with data, and then pass its address and the number of elements used to SendDeviceData. Data for different device objects is combined into a single packet that is then sent to the device.
The form of the data packet is specific to the device, as is the treatment of unused fields in the packet. Some devices treat fields as optional. In other words, if no data is supplied, the state of the object remains unchanged. More commonly, all fields are significant, even when you do not specifically supply data for them. For example, if you send data to a single keyboard LED, it is assumed that the data for the other two LEDs is 0 and that they are turned off. However, you can override this behavior by using the DISDD_CONTINUE flag. In this case, the data for the other two LEDs is the value you most recently sent them.
The device object type identifiers are obtained after passing the HID usage page and usage code to DirectInputDevice8.GetObjectInfo.
The following sample function, when called repeatedly, causes the LEDs on the keyboard to flash in a recurring pattern. It is assumed that the high bit of the data byte determines the state of the LED. The device object instance identifiers NumLockID, CapsLockID, and ScrollLockID have all been previously obtained, and diDevKeyboard is a previously defined DirectInputDevice8 object.
Private Sub FlashLEDs() Const ARRAYSIZE = 4 Static rgiBits(ARRAYSIZE-1) As Integer rgiBits(0) = 1 rgiBits(1) = 2 rgiBits(2) = 4 rgiBits(3) = 2 Static iLooper As Integer iLooper = 0 Dim cdod As Long cdod = 3 ' Number of items Dim rgdod(3) As DIDEVICEOBJECTDATA Dim i as Integer ' lTimeStamp and lSequence must be set to 0 For i = 0 To 3 rgdod(i).lTimeStamp = 0 rgdod(i).lSequence = 0 Next i rgdod(0).lOfs = NumLockID rgdod(1).lOfs = CapsLockID rgdod(2).lOfs = ScrollLockID Select Case (rgiBits(iLooper) And 1) Case Is = 0 rgdod(0).lData = 0 Case Is = 1 rgdod(0).lData = &H80 ' NumLock light on/off End Select Select Case (rgiBits(iLooper) And 2) Case Is = 0 rgdod(1).lData = 0 Case Is = 1 rgdod(1).lData = &H80 ' CapsLock light on/off End Select Select Case (rgiBits(iLooper) And 4) Case Is = 0 rgdod(2).lData = 0 Case Is = 1 rgdod(2).lData = &H80 ' ScrollLock light on/off End Select iLooper = (iLooper + 1) Mod ARRAYSIZE ' Loops from 0 to 3 Call diDevKeyboard.SendDeviceData(cdod, rgdod, 0) End Sub