Jump to content

prevent mouse from exiting screen (javascript or nw.js)


marcgfx
 Share

Recommended Posts

I am using NW.js for my game Devader (http://falkenbrew.com) and if the mouse leaves the window/application it's possible for the player to lose focus on the game, what can be very annoying.

I can't come up with a solution. I need the cursor to be shown, but I don't want the cursor to exit the screen.

A possible solution might be to use pointer lock (http://www.smartjava.org/content/html5-use-pointer-lock-api-restrict-mouse-movement-element) but it will be awkward to handle and it won't be possible to use the GUI I have so far. So ... maybe I could hack it in, but I really don't like the sound of it.

Has anyone dealt with this problem before?

Link to comment
Share on other sites

It would be mega mega annoying for users playing in a window to have their mouse effectively taken away from them, the pointer lock hack works because you still need to ask for permission from a user to hijack their mouse but a different solution would be to enforce fullscreen playing, that way you do nothing, the user simply cant make their mouse exit the screen as the game fills it.

Link to comment
Share on other sites

1 hour ago, mattstyles said:

... a different solution would be to enforce fullscreen playing, that way you do nothing, the user simply cant make their mouse exit the screen as the game fills it.

 

Unless the user has multi-monitor, then he still can click in the other screen and lose focus. I believe that there is no perfect solution yet, but what I do is keep a flag knowing if the game is in fullscreen or not. If not, then when the user clicks on the screen for any reason, then I switch to fullscreen and lock mouse cursor. If I need a pointer inside the game, then I use my own in-game pointer, not the default pointer which could leave the game screen. If he presses ESC leaving fullscreen, ok, he gets his pointer back, but to continue playing he must click back (and then I switch to fullscreen and lock cursor again).

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...