By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,304 Members | 3,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,304 IT Pros & Developers. It's quick & easy.

Cursor Position

P: n/a
Hi all,

I am making a DirectInput application wich must report correctly the
cursor position in windowed mode (it means it has to be the same as
Microsoft Window´s cursor position).
First of all, I get Windows System Mouse Speed by using the function :
SystemParametersInfo(SPI_GETMOUSESPEED, 0, &m_nMouseSpeed,0);
After I am reading the last Windows cursor position with GetCursorPos.
Then I read all mouse buffered data using GetDeviceData (from a
IDirectInputDevice8 interface), and when the field dwOfs from structure
DIDEVICEOBJECTDATA has value DIFMODS_X or DIFMODS_Y I sum the value from the
field dwData multiplied by the the value m_nMouseSpeed. After all buffered
data was read, the next GetCursorPos and my estimated position are
different.
Can anyone point me to the correct procedure in order from both, my
estimated cursor position and GetCursorPos meet?

Thank you all.
Paul.

For instance, my procedure is this:

class CInputMan
{
public:
CInputMan(CKernel* poKernel,const HWND hWnd=NULL);
virtual ~CInputMan();

static CInputMan*& GetInputMan();

const bool Update();

const unsigned int GetKeyboardSize();
const bool GetCurrentKeyboardState(unsigned char* pachKeyboardState);
const bool GetLastKeyboardState(unsigned char* pachKeyboardState);

const bool GetCurrentMouseState(float& fMouseX, float& fMouseY,unsigned
int& nWheelPosition,unsigned long& nMouseButtonState);
const bool GetLastMouseState(float& fMouseX, float& fMouseY,unsigned
int& nWheelPosition,unsigned long& nMouseButtonState);
protected:
class CMouseState
{
public:
CMouseState():m_nX(0),m_nY(0),m_nMouseButtons(0){}
virtual ~CMouseState(){};

void GetMouseState(int& nX,int& nY,int& nWheelPosition,unsigned
long& nMouseButtons)
{
nX=m_nX;
nY=m_nY;
nMouseButtons=m_nMouseButtons;
nWheelPosition=m_nWheelPosition;
}

void SetMouseState(const int nX,const int nY,const int
nWheelPosition,const unsigned long nMouseButtons)
{
m_nX=nX;
m_nY=nY;
m_nMouseButtons=nMouseButtons;
m_nWheelPosition=nWheelPosition;
}

int& GetMouseX()
{
return m_nX;
}

int& GetMouseY()
{
return m_nY;
}

int& GetWheelPosition()
{
return m_nWheelPosition;
}

unsigned long& GetButtons()
{
return m_nMouseButtons;
}
protected:
int m_nX;
int m_nY;
int m_nWheelPosition;
unsigned long m_nMouseButtons;
};

typedef vector<CMouseState> TDDeviceStateContainer;
typedef TDDeviceStateContainer::iterator
TDDeviceStateContainerIterator;

const bool Init();
const bool InitKeyboard();
const bool InitMouse();

const bool UpdateKeyboardState();
const bool UpdateMouseState();

static IDirectInput8*& GetDirectInputInterface();

IDirectInputDevice8* m_piSystemKeyboardDevice;
IDirectInputDevice8* m_piSystemMouseDevice;

HWND m_hApplicationWindow;
CKernel *m_poKernel;
static const unsigned int m_cnStandardBufferSize=16;
static const unsigned int m_cnStandardKeyboardSize=256;

unsigned char
m_auchCurrentKeyboardState[m_cnStandardKeyboardSize];
unsigned char
m_auchLastKeyboardState[m_cnStandardKeyboardSize];

int m_nMouseSpeed;
int m_nMouseWheelSpeed;

CMouseState m_oCurrentMouseState;
CMouseState m_oLastMouseState;

TDDeviceStateContainer m_cMouseStateContainer;
};

const bool CInputMan::InitMouse()
{
IDirectInput8*&
piDirectInputInterface=GetDirectInputInterface();
HRESULT hResult(DI_OK);
DIPROPDWORD dipdw;

hResult = piDirectInputInterface->CreateDevice(GUID_SysMouse,
&m_piSystemMouseDevice, NULL);
if (hResult!=DI_OK)
{
return false;
}

hResult = m_piSystemMouseDevice->SetDataFormat(&c_dfDIMouse);
if (hResult!=DI_OK)
{
return false;
}

hResult =
m_piSystemMouseDevice->SetCooperativeLevel(m_hApplicationWindow,DISCL_NO NEXC
LUSIVE| DISCL_FOREGROUND);
if (hResult!=DI_OK)
{
return false;
}

// the header
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
// the data
dipdw.dwData = m_cnStandardBufferSize;

hResult = m_piSystemMouseDevice->SetProperty(DIPROP_BUFFERSIZE,
&dipdw.diph);
if (hResult!=DI_OK)
{
return false;
}

return true;
}
const bool CInputMan::UpdateMouseState()
{
DIDEVICEOBJECTDATA stMouseState;
DWORD dwElementsRead(1);
HRESULT hResult(DI_OK);
DWORD dwSequenceNumber(-1);
unsigned int nCount(0);
unsigned long nMouseButtons(0);
unsigned long nBitMask(1);
unsigned int nBitOffset(0);
CMouseState oMouseState(m_oCurrentMouseState);
int nMouseX(oMouseState.GetMouseX());
int nMouseY(oMouseState.GetMouseY());
int nMouseWheel(oMouseState.GetWheelPosition());
POINT stPoint;

do
{
hResult =
m_piSystemMouseDevice->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),&stMouse Stat
e, &dwElementsRead,0);
if ((hResult==DIERR_INPUTLOST)||(hResult==DIERR_NOTAC QUIRED))
{
hResult = m_piSystemMouseDevice->Acquire();
if (hResult!=DI_OK)
{
return false;
}
hResult =
m_piSystemMouseDevice->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),&stMouse Stat
e, &dwElementsRead,0);
if (hResult!=DI_OK)
{
return false;
}
}
else if (hResult!=DI_OK)
{
return false;
}

switch (stMouseState.dwOfs)
{
case DIMOFS_X:
nMouseX+=stMouseState.dwData*m_nMouseSpeed;
break;
case DIMOFS_Y:
nMouseY+=stMouseState.dwData*m_nMouseSpeed;
break;
case DIMOFS_Z:
nMouseWheel+=stMouseState.dwData*m_nMouseWheelSpee d;
break;
}

nBitOffset=0;
for (nCount=DIMOFS_BUTTON0;nCount<=DIMOFS_BUTTON7;nCou nt++,nBitOffset++)
{
if (stMouseState.dwOfs==nCount)
{
if (stMouseState.dwData&0x80)
{
nMouseButtons|=nBitMask<<nBitOffset;
}
else
{
nMouseButtons&=~(nBitMask<<nBitOffset);
}
}
}

if (stMouseState.dwSequence==dwSequenceNumber)
{
oMouseState.GetButtons()|=nMouseButtons;
oMouseState.GetMouseX()=nMouseX;
oMouseState.GetMouseY()=nMouseY;
oMouseState.GetWheelPosition()=nMouseWheel;

if (m_cMouseStateContainer.empty())
{
m_cMouseStateContainer.push_back(oMouseState);
}
else
{
m_cMouseStateContainer.back()=oMouseState;
}
}
else
{
oMouseState.GetButtons()=nMouseButtons;
oMouseState.GetMouseX()=nMouseX;
oMouseState.GetMouseY()=nMouseY;
oMouseState.GetWheelPosition()=nMouseWheel;
m_cMouseStateContainer.push_back(oMouseState);
}
}
while (dwElementsRead>0);

GetCursorPos(&stPoint);
ScreenToClient(m_hApplicationWindow,&stPoint);
oMouseState.GetMouseX()=stPoint.x;
oMouseState.GetMouseY()=stPoint.y;

m_oLastMouseState=m_oCurrentMouseState;
m_oCurrentMouseState=oMouseState;

return true;
}
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Paul,
Can I please have the working code of your cursor position program. I
am trying to simulate the mouse like a digitizer pen.
I need to save mouse co-ordinates when a user moves the mouse FAST
inside a window (like a jerky movement). The coordinates will be saved
into a notepad file with 'exact time' (millisecond precision) for each
co-ordinate. A normal mouse has a sampling rate of less than 200 Hz.
So, if a mouse moves for 1 sec, it sends about 200 coordinates to the
system. I am expecting a notepad file in the following form:

Time (s) X Y
0.000 156 86
0.005 156 89
0.010 157 90
.... ... ...

I am trying to use the "DIDEVICEOBJECTDATA Structure" to get the mouse
cursor positoin and also the time stamp. A sample working code from you
can be a great help for me.

Regards,
Aziz

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.