How do i check if mouse cursor is off-screen?

  1. WindowFocusReleased event will not work because it fires when the window loses if focus, not when mouse leaves the window.
  2. making a Frame that fills the screen and using MouseLeave event won’t also work because roblox just stops tracking the mouse position once the cursor leaves the window, and sustain its last position.

You can’t do it directly (that is, there is no API for this) because of OS limitations, IIRC.

According to this thread asking the same question, you can use UserInputService:GetMouseDelta() to track the user’s cursor’s position from frame to frame and detect if it’s being clipped to a corner or edge of the viewport.

getmousedelta only works in first person or locked mouse

its probably impossible any other way

--LocalScript

local uis = game:GetService("UserInputService")
local run = game:GetService("RunService")
local cam = workspace.CurrentCamera

local lastPos = Vector2.zero
local stillCount = 0

local function checkMouse()
	local pos = uis:GetMouseLocation()
	if pos == lastPos then
		stillCount += 1
	else
		stillCount = 0
		lastPos = pos
	end
	if stillCount > 10 then
		print("off-screen")
	end
end

run.RenderStepped:Connect(checkMouse)

(programmers can do anything they want to do)

Here’s a little snippet of code from my tab glitching anticheat that does what you want reliably:

local UserInputService = game:GetService("UserInputService")

local mouseOffScreen = false
UserInputService.InputBegan:Connect(function(iO)
	if iO.UserInputType == Enum.UserInputType.MouseMovement then
		mouseOffScreen = false
	end
end)
UserInputService.InputEnded:Connect(function(iO)
	if iO.UserInputType == Enum.UserInputType.MouseMovement then
		mouseOffScreen = true
	end
end)
1 Like

And some other programmer will always come along and do it better. :rofl:

1 Like

Bit confused here after reading replies. Do you want to check if mouse is not moving, or when mouse is not inside a specific UIComponent?

They want to check if the mouse cursor is outside of the client window. Detecting when the MouseMovement InputObject starts or ends is the best way to accomplish this.

Indeed. Thanks for clarifying.