C++ Ncurses , how to check what the current mouse position is - c++

How Do I Check What The Mouse Position is when I move the mouse in Ncurses. I have tried searching but couldnt find any answer . I have seen the use of getmouse() , but getting the mouse co-ordinates from that function requires enabling an event first. I want to get the position of the mouse without clicking the mouse.

You get the cursor position by calling GetCursorPos.
POINT p;
if (GetCursorPos(&p))
{
//cursor position now in p.x and p.y
}
This returns the cursor position relative to screen coordinates. Call ScreenToClient to map to window coordinates.
if (ScreenToClient(hwnd, &p))
{
//p.x and p.y are now relative to hwnd's client area
}

Related

DirectX 9 Get Cursor Position [duplicate]

I want to get the current mouse position of the window, and assign it to 2 variables x and y (co-ordinates relative to the window, not to the screen as a whole).
I'm using Win32 and C++.
And a quick bonus question: how would you go about hiding the cursor/unhiding it?
You get the cursor position by calling GetCursorPos.
POINT p;
if (GetCursorPos(&p))
{
//cursor position now in p.x and p.y
}
This returns the cursor position relative to screen coordinates. Call ScreenToClient to map to window coordinates.
if (ScreenToClient(hwnd, &p))
{
//p.x and p.y are now relative to hwnd's client area
}
You hide and show the cursor with ShowCursor.
ShowCursor(FALSE);//hides the cursor
ShowCursor(TRUE);//shows it again
You must ensure that every call to hide the cursor is matched by one that shows it again.
GetCursorPos() will return to you the x/y if you pass in a pointer to a POINT structure.
Hiding the cursor can be done with ShowCursor().

How to determine which way the mouse is being dragged (Win32, C++)

I am currently working on a project which requires me to know when the mouse is being dragged to the left, or to the right.
What I would like to do with this information, is drag an object in 3d space (with OpenGL/Win32), left, or right, depending on which way the mouse was dragged.
The problem is, I have no idea how to determine which way the mouse is being dragged.
I tried finding the previous x coordinate, and subtracting it from the current x coordinate, but I do not know how to determine the previous x coordinate!
Can someone please help me?
(P.S. I already know that the current x mouse location is found with LOWORD(lParam), but what I do not know is how to determine the previous x mouse location. Thanks for your time.)
You could do it yourself by storing the current mouse position as the 'last' mouse position, and then after two mouse position updates, subtract.
onMouseMove(mouseX, mouseY):
if previousX and previousY are set:
deltaX = previousX - mouseX
deltaY = previousY - mouseY
actOnDrag(deltaX, deltaY)
previousX = mouseX
previousY = mouseY
You could set previousX and previousY to some crazy sentinel value (e.g. -1) to determine whether they have ever been set.
You could use the DragDetect function.
BOOL DragDetect(HWND hwnd, POINT pt);
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-dragdetect

convert window coordinates to 3D world coordinates with glut function glutMouseFunc()

I am trying to get the 3D coordinates of a mouse click C++/OpengGL with the glut function glutMouseFunc(). So I created a function like this:
void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouse_x=x;
mouse_y=y;
}
}
The function gets the window coordinates of the click of the mouse and i use it with the glut function glutMouseFunc() like this:
glutMouseFunc(mouse);
My question is how would I modify the coordinates given by the mouse function so I could use them in a 3D world. My exact purpose would be the following: to be able to see if I have clicked on a 3D shape drawn in the world.
[EDIT] Would it be easier to transform the coordinates of the 3D object to 2D window coordinates and then compare it to the coordinates of the mouse click?
Mouse click does not correspond to a point in 3d space, but to a ray.
In any case, you use gluUnProject.
If you know scene "depth" under cursor, then you can get 3d position of a click - by passing depth via winZ parameter.
If you don't know depth, pass 0.0 in winZ parameter, to get start of the ray, and 1.0 to get the "end". You'll have to calculate yourself if this ray hits anything.

Relative mouse position of SDL_Surface

In my application I need to return the relative mouse position from an SDL_Surface, the problem is the mouse position that gets returned is relative to the SDL window and not the SDL_Surface. I guess my question is what is the easiest / most effective way of doing this. Any questions just ask. Thanks.
EDIT: Sorry I should have explained better, I have SDL_Surface* Surf_Display; on Surf_display there is an Image say its 1000 x 1000, So in order to see the image on a 600 x 600 window I have a camera that I can move around ( really its the surface that moves not the camera ) for instance to look right of the image I move the surface -1 left if that makes sense. So my problem is when I click my mouse on a part of the surface(image) my mouse returns the position that the mouse compared to where the cursor is in the window, what i'm wanting is so that it returns the position of the cursor compared to where it is on the surface(image)
I hope that better explains the situation. Thanks again
Just add(or subtract, depending on how you look at it) the offset to the mouse coordinates. So you're drawing the surface something like this:
SDL_Rect dest_rect = { -camera.x, -camera.y };
SDL_BlitSurface(image_surface, NULL, screen_surface, &dest_rect);
I don't know if you're using event based mouse handling, or if you're using SDL_GetMouseState, but either way, you would simply add camera.x and camera.y to the mouse position, for example:
int x, y;
SDL_GetMouseState(&x, &y);
x += camera.x;
y += camera.y;

Get current cursor position

I want to get the current mouse position of the window, and assign it to 2 variables x and y (co-ordinates relative to the window, not to the screen as a whole).
I'm using Win32 and C++.
And a quick bonus question: how would you go about hiding the cursor/unhiding it?
You get the cursor position by calling GetCursorPos.
POINT p;
if (GetCursorPos(&p))
{
//cursor position now in p.x and p.y
}
This returns the cursor position relative to screen coordinates. Call ScreenToClient to map to window coordinates.
if (ScreenToClient(hwnd, &p))
{
//p.x and p.y are now relative to hwnd's client area
}
You hide and show the cursor with ShowCursor.
ShowCursor(FALSE);//hides the cursor
ShowCursor(TRUE);//shows it again
You must ensure that every call to hide the cursor is matched by one that shows it again.
GetCursorPos() will return to you the x/y if you pass in a pointer to a POINT structure.
Hiding the cursor can be done with ShowCursor().