473,908 Members | 5,327 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 :
SystemParameter sInfo(SPI_GETMO USESPEED, 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
IDirectInputDev ice8 interface), and when the field dwOfs from structure
DIDEVICEOBJECTD ATA 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(CKern el* poKernel,const HWND hWnd=NULL);
virtual ~CInputMan();

static CInputMan*& GetInputMan();

const bool Update();

const unsigned int GetKeyboardSize ();
const bool GetCurrentKeybo ardState(unsign ed char* pachKeyboardSta te);
const bool GetLastKeyboard State(unsigned char* pachKeyboardSta te);

const bool GetCurrentMouse State(float& fMouseX, float& fMouseY,unsigne d
int& nWheelPosition, unsigned long& nMouseButtonSta te);
const bool GetLastMouseSta te(float& fMouseX, float& fMouseY,unsigne d
int& nWheelPosition, unsigned long& nMouseButtonSta te);
protected:
class CMouseState
{
public:
CMouseState():m _nX(0),m_nY(0), m_nMouseButtons (0){}
virtual ~CMouseState(){ };

void GetMouseState(i nt& nX,int& nY,int& nWheelPosition, unsigned
long& nMouseButtons)
{
nX=m_nX;
nY=m_nY;
nMouseButtons=m _nMouseButtons;
nWheelPosition= m_nWheelPositio n;
}

void SetMouseState(c onst int nX,const int nY,const int
nWheelPosition, const unsigned long nMouseButtons)
{
m_nX=nX;
m_nY=nY;
m_nMouseButtons =nMouseButtons;
m_nWheelPositio n=nWheelPositio n;
}

int& GetMouseX()
{
return m_nX;
}

int& GetMouseY()
{
return m_nY;
}

int& GetWheelPositio n()
{
return m_nWheelPositio n;
}

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

typedef vector<CMouseSt ate> TDDeviceStateCo ntainer;
typedef TDDeviceStateCo ntainer::iterat or
TDDeviceStateCo ntainerIterator ;

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

const bool UpdateKeyboardS tate();
const bool UpdateMouseStat e();

static IDirectInput8*& GetDirectInputI nterface();

IDirectInputDev ice8* m_piSystemKeybo ardDevice;
IDirectInputDev ice8* m_piSystemMouse Device;

HWND m_hApplicationW indow;
CKernel *m_poKernel;
static const unsigned int m_cnStandardBuf ferSize=16;
static const unsigned int m_cnStandardKey boardSize=256;

unsigned char
m_auchCurrentKe yboardState[m_cnStandardKey boardSize];
unsigned char
m_auchLastKeybo ardState[m_cnStandardKey boardSize];

int m_nMouseSpeed;
int m_nMouseWheelSp eed;

CMouseState m_oCurrentMouse State;
CMouseState m_oLastMouseSta te;

TDDeviceStateCo ntainer m_cMouseStateCo ntainer;
};

const bool CInputMan::Init Mouse()
{
IDirectInput8*&
piDirectInputIn terface=GetDire ctInputInterfac e();
HRESULT hResult(DI_OK);
DIPROPDWORD dipdw;

hResult = piDirectInputIn terface->CreateDevice(G UID_SysMouse,
&m_piSystemMous eDevice, NULL);
if (hResult!=DI_OK )
{
return false;
}

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

hResult =
m_piSystemMouse Device->SetCooperative Level(m_hApplic ationWindow,DIS CL_NONEXC
LUSIVE| DISCL_FOREGROUN D);
if (hResult!=DI_OK )
{
return false;
}

// the header
dipdw.diph.dwSi ze = sizeof(DIPROPDW ORD);
dipdw.diph.dwHe aderSize = sizeof(DIPROPHE ADER);
dipdw.diph.dwOb j = 0;
dipdw.diph.dwHo w = DIPH_DEVICE;
// the data
dipdw.dwData = m_cnStandardBuf ferSize;

hResult = m_piSystemMouse Device->SetProperty(DI PROP_BUFFERSIZE ,
&dipdw.diph) ;
if (hResult!=DI_OK )
{
return false;
}

return true;
}
const bool CInputMan::Upda teMouseState()
{
DIDEVICEOBJECTD ATA stMouseState;
DWORD dwElementsRead( 1);
HRESULT hResult(DI_OK);
DWORD dwSequenceNumbe r(-1);
unsigned int nCount(0);
unsigned long nMouseButtons(0 );
unsigned long nBitMask(1);
unsigned int nBitOffset(0);
CMouseState oMouseState(m_o CurrentMouseSta te);
int nMouseX(oMouseS tate.GetMouseX( ));
int nMouseY(oMouseS tate.GetMouseY( ));
int nMouseWheel(oMo useState.GetWhe elPosition());
POINT stPoint;

do
{
hResult =
m_piSystemMouse Device->GetDeviceData( sizeof(DIDEVICE OBJECTDATA),&st MouseStat
e, &dwElementsRead ,0);
if ((hResult==DIER R_INPUTLOST)||( hResult==DIERR_ NOTACQUIRED))
{
hResult = m_piSystemMouse Device->Acquire();
if (hResult!=DI_OK )
{
return false;
}
hResult =
m_piSystemMouse Device->GetDeviceData( sizeof(DIDEVICE OBJECTDATA),&st MouseStat
e, &dwElementsRead ,0);
if (hResult!=DI_OK )
{
return false;
}
}
else if (hResult!=DI_OK )
{
return false;
}

switch (stMouseState.d wOfs)
{
case DIMOFS_X:
nMouseX+=stMous eState.dwData*m _nMouseSpeed;
break;
case DIMOFS_Y:
nMouseY+=stMous eState.dwData*m _nMouseSpeed;
break;
case DIMOFS_Z:
nMouseWheel+=st MouseState.dwDa ta*m_nMouseWhee lSpeed;
break;
}

nBitOffset=0;
for (nCount=DIMOFS_ BUTTON0;nCount< =DIMOFS_BUTTON7 ;nCount++,nBitO ffset++)
{
if (stMouseState.d wOfs==nCount)
{
if (stMouseState.d wData&0x80)
{
nMouseButtons|= nBitMask<<nBitO ffset;
}
else
{
nMouseButtons&= ~(nBitMask<<nBi tOffset);
}
}
}

if (stMouseState.d wSequence==dwSe quenceNumber)
{
oMouseState.Get Buttons()|=nMou seButtons;
oMouseState.Get MouseX()=nMouse X;
oMouseState.Get MouseY()=nMouse Y;
oMouseState.Get WheelPosition() =nMouseWheel;

if (m_cMouseStateC ontainer.empty( ))
{
m_cMouseStateCo ntainer.push_ba ck(oMouseState) ;
}
else
{
m_cMouseStateCo ntainer.back()= oMouseState;
}
}
else
{
oMouseState.Get Buttons()=nMous eButtons;
oMouseState.Get MouseX()=nMouse X;
oMouseState.Get MouseY()=nMouse Y;
oMouseState.Get WheelPosition() =nMouseWheel;
m_cMouseStateCo ntainer.push_ba ck(oMouseState) ;
}
}
while (dwElementsRead >0);

GetCursorPos(&s tPoint);
ScreenToClient( m_hApplicationW indow,&stPoint) ;
oMouseState.Get MouseX()=stPoin t.x;
oMouseState.Get MouseY()=stPoin t.y;

m_oLastMouseSta te=m_oCurrentMo useState;
m_oCurrentMouse State=oMouseSta te;

return true;
}
Nov 17 '05 #1
1 2397
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 "DIDEVICEOBJECT DATA 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
4924
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 write ten words in the text-area, then I move the cursor at the beginning of the second word and activate the event (for example, click on a button): is there a way to know that the cursor is at the beginning of the second word?
1
13174
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 to the desktop 2. Retrieve the desktop cursor position. 3. Take the difference of the forms left and top position. Adjust the desktop cursor position based on this.
1
5101
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 box by 1 if my cursor is in front of the decimal point. If the cursor is after the decimal point, I want to increment the value by 0.1. How do I accomplish this? Is there a way to find out the cusror position in the control relative to decimal...
1
3917
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 TextBox ?? (relative to screen) I mean, i have a textbox that i write some text and i want to know the exact point location of the cursor at any time a character is displayed.
22
4572
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 that difficult, but to find the next line is more difficult. For exemple: if my cursor is on line 200, it has to start searching on line 201, and not on line 1. Anybody has any ideas?
2
5789
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 but when I'm in this event handler I want to find out the cursor position(x,y cordinates) where the double click occurred how is that done? //Tony
1
11991
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 when I mouse over an element (A column in an ASP.NET Gridview, but I suppose that's not important) Everything works great, except I can't seem to position the div where I want given scrolling.
4
4685
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 and write the fields into strings of rows, I want to be able to put field1 in row1/column position1, field2 in row1/column position10,.....etc. I've included the basic code to write a string of text. I understand the process of how to write the...
4
6985
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 just gotta fix it. I need to make some calculations on the measurements and VB6 is my language. Yes, the system mouse will corrupt the measurement, but it's an auditing function and that's acceptable.
0
10031
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11337
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10913
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10536
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9721
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6134
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4770
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3355
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.