Sign in to follow this  
kumikana

SDL and cursor position outside the window

Recommended Posts

I'm using SDL and I'm trying to find a way to read the mouse position when cursor is outside the application window. I've tried using SDL_MOUSEMOTION -event but it only returns the position when mouse cursor is inside the application window. As does the SDL_GetMouseState -function. I want the application to run in a windowed mode (one solution, of course, would be to run the application in fullscreen) and I don't want to capture the cursor inside the window (other solution I've used is to enable the SDL_WM_GrabInput). I've tried searching the SDL -documentation for a solution, but I haven't found one. I'm not sure but I think this is something that's not possible with SDL only. I think I have to override the SDL and do it using WinAPI, but I don't any clue how to do this, so all help and tips are highly appreciated.

Share this post


Link to post
Share on other sites
What do you mean you don't want to capture the cursor inside the application window? Do you mean you want the mouse input to only be meaningful when it's outside? What is your main goal?

I don't know my WinAPI all that well, but it might be impossible to do that by design. Having access to the cursor while another app is in focus might constitute a security hole. Then again, this is windows ;-).

If I misread, and you just don't want the cursor to show up while hovering over your app window, you can just do SDL_ShowCursor(0) (and it'll continue showing up when outside your app).

Share this post


Link to post
Share on other sites
Sorry, for the messy description. I mean that when the there's a focus on the application window, I want to keep track of the mouse cursor even if it goes outside the window. When mouse cursor goes outside the window, SDL keeps giving me the last position where the mouse cursor was inside the window. I think this is possible to with WinAPI or at least I hope it is.

Share this post


Link to post
Share on other sites
Quote:
Original post by kumikana
When mouse cursor goes outside the window, SDL keeps giving me the last position where the mouse cursor was inside the window.

Create 2 variables and save the values every logic loop?

Share this post


Link to post
Share on other sites
Hidden
This is from the SDL documentation: SDL_GetAppState

There's a variable called SDL_APPMOUSEFOCUS which is particularly interesting. I have no idea how to use it though, but I'm sure you can figure it out. :-)

Share this post


Link to post
Quote:
Original post by Kada2k6
This is from the SDL documentation: SDL_GetAppState

There's a variable called SDL_APPMOUSEFOCUS which is particularly interesting. I have no idea how to use it though, but I'm sure you can figure it out. :-)


I don't think that is what you are looking for (it will only inform you if your program has the mouse focus).

Does SDL_GetMouseState() report the correct position? It doesn't mention it in the documentation, but you might find it does anyway...

Share this post


Link to post
Share on other sites
Hidden
Unfortunately, SDL_GetMouseState() only works for cursor positions inside the SDL application window. That's why I suggest he should use SDL_GetAppState() to check where the cursor is. If it's outside the window - ignore any functions that are related to the mouse cursor (like for example scrolling the map if this was an RTS game).

Share this post


Link to post
Thanks everybody for your response.

I'm making an action game were the character follows the mouse cursor. If the game is runing in a windowed mode, the controls lag or stop completely if the mouse cursor moves outside the window. This is really frustrating as a player, because the character stops responding to your controls.

This is the reason why I'm particularly interested in finding a way to track the mouse even if it's outside the application window.

Share this post


Link to post
Share on other sites
I think the solution you are after might include the SDL_GetRelativeMouseState() function. And track the X and Y mouse coordinates each frame.

something like:


...
static int Mouse_X = -99999;
static int Mouse_Y = -99999;
static Uint8 Buttons;

if (Mouse_X == -99999)
{
Buttons = SDL_GetMouseState(&Mouse_X, &Mouse_Y);
}
else
{
int mx, my;
Buttons = SDL_GetRelativeMouseState(&mx, &my);
Mouse_X += mx;
Mouse_Y += my;
}
...




SDL_GetMouseState() sets the x and y mouse coordinates to that of the current mouse cursor. (You need the initial cursor position)

SDL_GetRelativeMouseState() sets the x and y values in relation to the last cursor posistion. In other words, if you move the mouse one pixel to the right but not up or down, it would set mx to 1 and my to 0.

But I am not completely sure. I has been a long time since I have personally used either function. I am not sure if even SDL_GetRelativeMouseState() continues to update if the visible mouse cursor leaves the screen.

It would be worth a shot.

Share this post


Link to post
Share on other sites
Thanks for the suggestion. I tested it but unfortunately it doesn't work. The SDL_GetRelativeMouseState -function also stops updating if you move your mouse cursor out of the window.

I'm pretty certain that the only way to do is by breaking the SDL -api and doing with WinAPI.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this