Keep mouse in window

I’m trying to create a cool 2.5D shooter that looks like the original mario kart on snes, but I have a problem with the mouse moving out of window constantly. How do I set the mouse to stay inside window, like just track which direction the mouse moves and stick the mouse to the center of the window like in other apps?

I am quite certain that this isn’t in Corona (for the time being at least) and any games that require such a feature are impossible to create on Corona until such a feature is implemented.

I’m not sure how difficult it would be to implement and I don’t expect anyone to implement this feature any time soon.

Are you building this for desktop/laptop or mobile? For mobile, you can’t go outside the window anyway, so there is no reason to need this feature. If you’re building for window’s I could see where you might want this, but I wonder how much it would annoy people if they couldn’t react with the other apps they have running (click into a backgrounded web browser, email app, etc.), I would think that if you were doing this, you could lock the game into either full screen or full window mode and then the mouse couldn’t escape there. I do know that when using a VM to run something like Linux on your host OS, they can be set to trap the mouse within the apps window until you hit a special key to release the key.

I’m not sure how you could do this in the Corona simulator. There might be a way to do this at the native level.

Rob

Yes, I am creating a win32 game (Maybe mac too)

I have worked with unity before and they had a call to lock the mouse: https://docs.unity3d.com/ScriptReference/Cursor-lockState.html

And I have three monitors, playing full screen doesnt work because the mouse jumps from screen to screen when I move left or right. My game requires screen panning so that if I move the mouse to the edge of the screen, the map/level will rotate, but I cant do that since the mouse just leaves the screen.

So, full screen works if there is only one screen but if there are more then the mouse doesnt stay in window and the game is unplayable. How to fix?

Also, it would be great to let people play windowed if they want, but if the mouse escapes the window, then that is impossible.

It’s like every FPS or RTS game on computer. The mouse wont show when the window is active so that player can move their mouse freely while staying in game

Like I already said, Corona doesn’t support that. Corona apps aren’t really in a “proper” fullscreen for desktop, but in a maximised borderless windowed mode.

Corona  cannot be used to create any games for PC/Mac/Linux that rely on the user interacting with the borders of the screen. If the player has only a single monitor, then they won’t be able to move their mouse off screen, but when it comes to gamers, a lot of people have two or more monitors these days. Now, since Corona apps are just maximised borderless windows, the mouse cursor will always escape to said alternative displays (which is really the same as just moving a mouse out of a smaller window).

If you know C++, you could see about creating such a feature to https://docs.coronalabs.com/api/library/native/setProperty.html where you can toggle things like mouse cursor visibility. If you don’t know C++ or don’t have the time to do that, then your game will have that problem until someone else fixes it for you.

I haven’t really thought this through in any detail but might it be possible to hide the real mouse cursor and display your own by tracking its position? Then when the real mouse leaves the bounds of the window then yours just stays where it is, and you can handle scrolling the screen.

That won’t really work. The cleanest (and most commonly used) fix to this issue is the same that Unity seems to be using, i.e. locking the cursor in the centre of the window and turning it invisible. Then you just track where the mouse would move (if it weren’t locked) and you can move your custom cursor.

If you can move your mouse outside of the window (and click on things outside), then you can accidentally click on things on other screens or on the desktop, and you don’t have the ability to, for instance, move a game map by moving the map to one side of the window since the mouse just moves out of the window (or it’d keep scrolling until you return to the screen, which would make the controls exceedingly difficult).

I really dont know any c++ and i wouldnt want to learn just for this? This is horrible and i cant work on my game without this feature!!

Is there a way to request features or to pay someone to create them? Otherwise I need to go back to unity which I dont want

up

It might not be too bad on Windows, if this does what it sounds like. Maybe I’ll take a look tomorrow. Lerg’s library might be a better final home for it, though. 

Yes! This is exactly what I need! I believe this is something that a lot of people need but dont know yet. It is kinda must for Pc/desktop games.

Well, I have something. Current project attached, plus a DLL. See here for some instructions about “installing” that. (For the “YOUR PATH TO HERE”, you can use %appdata% in the search bar of Windows Explorer.)

In Lua do

local cc = require("plugin.clipcursor") cc.ConfineCursor{ left = 15, right = -15, top = 5, bottom = -5 } -- hack, should just be cc.ConfineCursor()

It needs a more intelligent way of finding the right window. Now it seems to pick up the shadow around the simulator, which doesn’t take input, so you can click out around the edges. Thus the artficial narrowing in the above snippet. Might be possible to just iterate all child windows and check some flag, merging the valid areas.

There’s a __gc hook to disable it when you close.

Alt+Tab will break it. Some looking around suggest message hooks might help here.

Some other comments online say clip cursors have global side effects (there’s a reference count involved), so if Corona crashes the clip region might affect other programs.

No idea what the Mac equivalent of this would be.

Amazing! I’ll get to testing this once I get home later tonight! Thank you for taking the time to do this!

Lotta families to visit during x-mas. I followed your instructions and it correctly clipped it for the simulator, which was cool! Are the included win32 and shared files the source code?

I couldnt build my win32 app with it and it just crashed. I dont know if this needs to be in simulator, but it needs to be in actual win32 build because otherwise the mouse always escapes to other monitors even from full screen. In Unity this is done by locking the mouse to the center of the window and listening to where it would move. Then a ”fake mouse” moves instead.

This was really cool still! Thanks for taking the time! Do you know how a more permanent solution could be added to all win32 and or mac built games?

Hi. Yes, that’s the (current) source included. (It’s unfortunately getting rather difficult to find Visual Studio 2013 . As far as I can tell Microsoft’s own links are down / redirect to newer versions. I’m actually trying to update the source to use 2017 but there are some pieces that use different build tools and I haven’t mustered the will to do all these.  :))

I think the problem you’re seeing is just because the plugin isn’t actually integrated into Corona’s build process (since it’s just a free binary, not on the marketplace). Try putting it into the folder that gets built.