Mouse cursor leaving game window

I can’t seem to solve the issue of mouse cursor leaving the game window. I would say it’s just my inexperience except all the Epic projects do it too! For example Vehicle Game, Strategy Game, Particle Effects, Features Tour 2014, Content Examples, First Person Template, Third Person Template.

This really feels like a “it’s just me” thing though, because if not it seems like it would be an enormous problem!

Simple Example:

  • Create new First Person Template blueprint project

  • Run as Standalone game

  • Without clicking, move the mouse one direction and the mouse cursor appears coming out of the window. until the game window loses focus the character will continue to rotate view.

  • if you depress a mouse button then move the mouse, the cursor is never visible and everything seems fine until you release the mouse button, then the cursor appears outside the window (assuming it ends up there obviously). So it was just hidden while captured, but is free to wander around your screen.

I’ve tried to use “Set Input Mode Game Only” and “Set Input Mode Game and UI” in the PlayerController and Character blueprints. I tried it off begin play and key press events.

Hi Dubh, I don’t think this is really a problem because most games are run in fullscreen. But then again, I may not being able to understand your problem in the first place. Could you explain it better? Also keep in mind that this is the UE viewport, to see if the problem is really there you should package your game and try it out.

Hi, I think you’re looking for the Set Input Mode Game and UI node.
In this node you can enable the Lock Mouse to Viewport.

Here is the documentation on this:

I’ve tried to use “Set Input Mode Game Only” and “Set Input Mode Game and UI” in the PlayerController and Character blueprints. I tried it off begin play and key press events.

Try to set this in the GameMode bp.

Calling SetInputMode(s) from BeginPlay in GameMode event graph causes the mouse to stay put (sometimes)! Seems like a strange requirement to not have in the docs!

There are still oddities, but hopefully I can iron those out - at least it’s kind of working! I’m hoping after I figure out UI/PlayerController interaction I can get it to do what I want.

First Person Template: “Set Input Mode Game Only” works perfectly

Third Person Template: “Set Input Mode Game and UI” combined with Show Mouse Cursor = true fails to contain the mouse cursor when not captured (e.g. uncaptured mouse doesn’t stop at edge, captured mouse stays in exact same spot after it reappears)

Empty Template: “Set Input Mode Game and UI” combined with Show Mouse Cursor = true contains the mouse cursor inside window, but the fly camera is now completely nonfunctional. WASD + mouse do nothing.

only a problem if you want to run a game in windowed mode! :slight_smile:

I never use PIE, I just run in “Standalone Game”. My understanding is that “Standalone Game” is mostly like running a packaged game.

As the description on the button says, it only creates it’s own process. It’s not like packaging for the final game :slight_smile:

Good to know - thanks!

Thanks to Klawd3 I ended up doing a little more digging and I think I’ve accidently discovered the true culprit.

If the editor is visible behind the game it will often (but not always) start allowing the mouse to leave the window.

Tested using “Standalone Game” as well as a packaged game, and with this mouse issue they react identically.

  • If editor is minimized, the mouse is locked to window. Everything seems to behave.
  • if the editor is visible behind the game it can allow the mouse to escape. Sometimes the game will propertly lock the mouse to window, but if you simply alt+tab back and forth from editor to game a couple of times it will eventually break.
1 Like

You probably just saved me hours of frustration! Trying it as a packaged game I accidently stumbled across the reason the mouse misbehaves (answer below)

I’m glad. Keep up the good work!

So what was the culprit? I’m having the same issues.

Run this at tick in your controller. and you will never miss a single click. it is a common problem with right and botom edge of wiondow. even on a build fullscreen game. Gods of internet knows only why. hope that helps.

Yeah, I had the same issue and found that this kind of workaround was the only way to solve the problem. I have no idea how this isn’t a bigger issue?? Maybe it only occurs in certain environments? Or does every UE4 developer just hack their way around it themselves without posting on AnswerHub?

Anyway, I posted a similar C++ solution as a response to the following question for the C++ people around here. Hope it helps!

Yay, Epic moved the answer hub to the forums and your image doesn’t exist anymore. Could you update the solution?

Not sure if its built into my keyboard or universal fix but works for me: while in game screen hold window key, tap P, release both. It should lock cursor to that game at least until u hit escape. Some setups may need to do it each time you return to that game window, others may fix the issue so it wont happen again. esc lets u leave a window if locked to it. not sure how to perm unlock the win/P combo. FYI On razers they also have a built in key that lets u lock out win box so it will not pop up to yank u off game if u bump the win key. (F10 i think) so if its on win lock you may have to unlock to try this fix, then relock win