473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cursor Position

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
1 2356
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PeP | last post by:
Good morning, I have a form containing a text-area, I'd like to know if it exists a function that, when I activate an event, returns the position of the cursor in the text-area. For example, I...
1
by: Dan H. | last post by:
Hello, I want to try to retrieve the cursor position x and y relative to the form the cursor is over. The way I am doing it now is: 1. Retrieve the left and top position of the form relative...
1
by: Tantra Veda | last post by:
Hello C# gurus, I have a question about finding cursor position in NumericUpDown control. On my form I have a numericUpDown control with 2 decimal places. I want to increment value in the numeric...
1
by: objectref | last post by:
Hi to all, we have the MousePosition property that we can get the Point of the position of the mouse cursor on the screen, but is it there a way to get the respective Point of a cursor in a...
22
by: DraguVaso | last post by:
Hi, For my application I need the following behavior: When I press F4 the cursor has to move to the next line in my multiline textbox which begins with "0". Finding lines starting with 0 isn't...
2
by: tony | last post by:
Hello! Assume I catch a double click in a form by using the event handler that forms designer create for me. Now to my question when I double click in the form the event handler is trigged...
1
by: jobs | last post by:
I'm trying to understand this and don't get it... I found some code that looks like it's going to work if I can make sense of how to position the div. The code displays a div with some data...
4
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through...
4
by: mike | last post by:
I have the opportunity to rescue a project that uses a mouse to sense the relative position of a machine. The hardware is built...just needs to be programmed. Stop snickering!!! I didn't do it...I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.