Microsoft DirectX 8.1 (Visual Basic)

Step 3: Mapping Actions to Devices

The ActionMapper Sample uses its class function CreateDevicesFromMap to, among other things, measure the action map defined above against available devices. To do so, it first assigns values to the other members of the DIACTIONFORMAT type containing the array of DIACTION types. The values used in the sample have been passed as parameters to the CreateDevicesFromMap method. For clarity, the following code sample uses the actual values rather than the parameter names.

    m_diaf.guidActionMap = "{20CAA014-60BC-4399-BDD3-84AD65A38A1C}"
    m_diaf.lGenre = DIVIRTUAL_SPACESIM
    m_diaf.lBufferSize = 16
    m_diaf.lAxisMax = 100
    m_diaf.lAxisMin = -100
    m_diaf.ActionMapName = "Semantic Mapper VB Sample"
    m_diaf.lActionCount = 18

The guidActionMap value is defined by the program. For more information on creating GUIDs, see Using GUIDs.

Once the desired mapping has been defined, it must be applied to physical devices. DirectInput8.GetDevicesBySemantics begins this process in the following code fragment, where m_DI is a previously defined DirectInput8 object, m_DIEnum is a previously defined DirectInputEnumDevices8 object, and m_strUserName is a previously defined string containing a friendly identifier for the user.

Set m_DIEnum = m_DI.GetDevicesBySemantics(m_strUserName, m_diaf, 0)

Flags could be used to limit the enumeration to a more specific search, but in this case no flags are set.

The results of the enumeration are examined individually and devices are created for each keyboard, mouse, and joystick found. In the following code sample, devinst is a previously defined DirectInputDeviceInstance8 object, m_Devices is a previously defined array of DirectInputDevice8 objects, and m_DeviceTypes is a previously defined array of Long values.

    For i = 1 To m_DIEnum.GetCount
    
        Set devinst = m_DIEnum.GetItem(i)
        Set m_Devices(i) = m_DI.CreateDevice(devinst.GetGuidInstance)
        m_DeviceTypes(i) = devinst.GetDevType
        Set devinst = Nothing

The loop continues by checking whether the enumerated device currently under examination is a mouse. If so, the AXISMODE property is set to relative. Other properties could be set within this loop as well, but in the case of the ActionMapper Sample, this is all that will be done here.

        If m_DeviceTypes(i) = DI8DEVTYPE_MOUSE Then
            Dim dipl As DIPROPLONG
            dipl.lHow = DIPH_DEVICE
            dipl.lData = DIPROPAXISMODE_REL
            m_Devices(i).SetProperty "DIPROP_AXISMODE", dipl
        End If

DirectInputDevice8.BuildActionMap takes the list of control assignments specified in the DIACTIONFORMAT type and attempts to map them to specific device objects on the device being enumerated for the user specified in the m_strUserName parameter. The results of the mapping are returned in the same structure. Each pass maps different controls depending on the nature of the enumerated device currently being examined. A keyboard, for instance, maps controls that specify a mapping to a particular key, but it leaves controls that specify buttons, sliders, or axes unmapped. If a joystick is enumerated next, BuildActionMap ignores those controls mapped to keyboard keys, but it attempts to map the buttons, sliders, and axes to the joystick's device objects.

        m_Devices(i).BuildActionMap m_diaf, m_strUserName, 0
    

Once again, no flags are used in this instance.

The results of this default mapping can be examined and manipulated. This is done in Step 4: Configuring and Applying the Action Map.