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.