Switch to full style
For C/C++ coders discussions and solutions
Post a reply

Mouse position in C++

Sun May 25, 2008 5:17 pm

This C/C++ show the position of mouse (X,Y) at the screen :
cpp code
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
HANDLE hIn;
HANDLE hOut;
COORD MouseWhere = {30, 20};
COORD DClickWhere = {30, 20};





bool Continue = TRUE;
DWORD EventCount;
int LoopCount = 0;
int KeyEvents = 0;
INPUT_RECORD InRec;
DWORD NumRead;

hIn = GetStdHandle(STD_INPUT_HANDLE);
hOut = GetStdHandle(STD_OUTPUT_HANDLE);

cout << "Mouse is at : " << endl;
cout << "Double Click at : " << endl;




while (Continue)
{



Sleep(10); // To slow it down!!

GetNumberOfConsoleInputEvents(hIn,&EventCount);
while (EventCount > 0)
{
ReadConsoleInput(hIn,&InRec,1,&NumRead);

if (InRec.EventType == KEY_EVENT)
{
if (InRec.Event.KeyEvent.uChar.AsciiChar == 'x')
{


cout << "Exiting..." << endl;
Continue = FALSE;
}
}
else if (InRec.EventType == MOUSE_EVENT)
{

if (InRec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
{
SetConsoleCursorPosition(hOut,
MouseWhere);
cout << InRec.Event.MouseEvent.dwMousePosition.X << "," <<
InRec.Event.MouseEvent.dwMousePosition.Y << " " << flush;
}
else if (InRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
{
SetConsoleCursorPosition(hOut,
DClickWhere);
cout << InRec.Event.MouseEvent.dwMousePosition.X << "," <<
InRec.Event.MouseEvent.dwMousePosition.Y << " " << flush;
}
}

GetNumberOfConsoleInputEvents(hIn,&EventCount);
}
}

return 0;
}


I hope it is helpfull



Re: Mouse position in C++

Tue Apr 17, 2012 5:12 am

Thank you so much!!! It is highly useful for my project. CHEERS!!

Post a reply
  Related Posts  to : Mouse position in C++
 Get Mouse Position using JQuery     -  
 Change background position when mouse over     -  
 X and Y background position     -  
 setting div background position     -  
 ORACLE/ SQL DBA this is a CTH position. No H1B’s     -  
 static vs fixed div position     -  
 right bottom background image position     -  
 Set position of background image in pixels     -  
 Get the position of HTML element using JQuery     -  
 Slide automatically-specific speed to a determined position     -  

Topic Tags

C++ Inputs