Jump to content

Trapping the mouse to the current window


Einlander
 Share

Recommended Posts

As a user of a multi-monitor setup, I was amazed at the number of AAA games that simply do not take into account the possibility that their game may be played on one monitor of a multi-monitor system.

 

After rage quitting a gaming session because I kept dropping back to the desktop because my mouse wandered outside the game window, I decided to write some brain-dead simple lua code to trap the mouse in the game without interfering with any code.

 

-- trap mouse start
if (self.trapmouse == true) then
if (self.window:GetMousePosition().x<0) then
self.window:SetMousePosition(0,self.window:GetMousePosition().y)
end
if (self.window:GetMousePosition().x>self.context:GetWidth()) then
self.window:SetMousePosition(self.context:GetWidth(),self.window:GetMousePosition().y)
end
if (self.window:GetMousePosition().y<0) then
self.window:SetMousePosition(self.window:GetMousePosition().x,0)
end
if (self.window:GetMousePosition().y>self.context:GetHeight()) then
self.window:SetMousePosition(self.window:GetMousePosition().x,self.context:GetHeight())
end
end
-- trap mouse end

 

I placed this in app.lua before the render, but it can be placed anywhere in your code as long as it is placed AFTER any mouse handling routines.

 

The code is inabled by setting self.trapmouse to true.

  • Upvote 1
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.

 Share

×
×
  • Create New...