Mouse - just like KB

 

 

To Use:

·       create mouse device w/ CreateDevice()

·       set cooperative level for KB – SetCooprativeLevel()

·       set data format – SetDataFormat()

·        acquire the KB – Acquire()

·       read currect state info – GetDeviceState()

 

Ex:

#define INITGUID         // defines kb guid

#include <OBJBASE.H>

#include <dinput.h>

…

 

LPDIRECTINPUTDEVICE8 lpdiMouse = NULL;  // mouse ptr

 

If ( CreateDevice(GUID_SysMouse, &lpdiMouse, NULL) !=    

          DI_OK)

{/* error */}

 

// At this point lpdiMouse points to your mouse interface,

 

// set coop level

If (lpdiMouse-> SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE) != DD_OK) // error

 

// set data format

lpdikey->SetDataFormat(&c_dfDIMouse);

 

// acquire mouse

lpdiMouse->Acquire();

 

Reading the Mouse

When you’re ready to read some data from the mouse use:

 

// read the mouse

If (lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE, (LPVOID) mousestate) != DI_OK)

{/* error */}

 

where DIMOUSESTATE is:

 

struct DIMOUSESTATE

{ LONG  lX;   // x pos

   LONG  lY;   // y pos

   LONG  lZ;   // z pos

   BYTE   rgbButtons[4];  // buttons, high bit signals down

};

 

Ex:

DIMOUSESTATE mouseState;

 

if (lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE,    

                                         (LPVOID) mousestate) == DI_OK)

    //   mouseState.lX, lY is the current xy position

 

if (mouseState.rgbButtons[0] & 0X80)  // left button down

if (mouseState.rgbButtons[1] & 0X80)  // right button down

 

 

When done with mouse:

    lpdiMouse->Unacquire();

    lpdiMouse->Release();