function MouseLockController.OnInputBegan(X,P,N)if N then return end if P.UserInputType==Enum.UserInputType.Keyboard then for N,d in pairs(X.boundKeys)do if d==P.KeyCode then X:OnMouseLockToggled()return end end end end function MouseLockController.DoMouseLockSwitch(d,P,N,X)if N==Enum.UserInputState.Begin then d:OnMouseLockToggled()return Enum.ContextActionResult.Sink end return Enum.ContextActionResult.Pass end function MouseLockController.BindContextActions(P)ContextActionService:BindActionAtPriority(CONTEXT_ACTION_NAME,function(N,X,d)P:DoMouseLockSwitch(N,X,d)end,false,MOUSELOCK_ACTION_PRIORITY,unpack(P.boundKeys))end function MouseLockController.UnbindContextActions(P)ContextActionService:UnbindAction(CONTEXT_ACTION_NAME)end function MouseLockController.IsMouseLocked(P)return P.enabled and P.isMouseLocked end function MouseLockController.EnableMouseLock(N,P)if P~=N.enabled then N.enabled=P if N.enabled then if FFlagPlayerScriptsBindAtPriority then N:BindContextActions()else if N.inputBeganConn then N.inputBeganConn:Disconnect()end N.inputBeganConn=UserInputService.InputBegan:Connect(function(P,X)N:OnInputBegan(P,X)end)end else if Mouse.Icon~=""then Mouse.Icon=""end if FFlagPlayerScriptsBindAtPriority then N:UnbindContextActions()else if N.inputBeganConn then N.inputBeganConn:Disconnect()end N.inputBeganConn=nil end if N.isMouseLocked then N.mouseLockToggledEvent:Fire()end N.isMouseLocked=false end end end return MouseLockController
-- Remove with FFlagPlayerScriptsBindAtPriority
function MouseLockController:OnInputBegan(input, processed)
	if processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.Keyboard then
		for _, keyCode in pairs(self.boundKeys) do
			if keyCode == input.KeyCode then
				self:OnMouseLockToggled()
				return
			end
		end
	end
end

function MouseLockController:DoMouseLockSwitch(name, state, input)
	if state == Enum.UserInputState.Begin then
		self:OnMouseLockToggled()
		return Enum.ContextActionResult.Sink
	end
	return Enum.ContextActionResult.Pass
end

function MouseLockController:BindContextActions()
	ContextActionService:BindActionAtPriority(CONTEXT_ACTION_NAME, function(name, state, input)
		self:DoMouseLockSwitch(name, state, input)
	end, false, MOUSELOCK_ACTION_PRIORITY, unpack(self.boundKeys))
end

function MouseLockController:UnbindContextActions()
	ContextActionService:UnbindAction(CONTEXT_ACTION_NAME)
end

function MouseLockController:IsMouseLocked()
	return self.enabled and self.isMouseLocked
end

function MouseLockController:EnableMouseLock(enable)
	if enable ~= self.enabled then
		self.enabled = enable

		if self.enabled then
			-- Enabling the mode
			if FFlagPlayerScriptsBindAtPriority then
				self:BindContextActions()
			else
				if self.inputBeganConn then
					self.inputBeganConn:Disconnect()
				end
				self.inputBeganConn = UserInputService.InputBegan:Connect(function(input, processed)
					self:OnInputBegan(input, processed)
				end)
			end
		else
			-- Disabling
			-- Restore mouse cursor
			if Mouse.Icon ~= "" then
				Mouse.Icon = ""
			end

			if FFlagPlayerScriptsBindAtPriority then
				self:UnbindContextActions()
			else
				if self.inputBeganConn then
					self.inputBeganConn:Disconnect()
				end
				self.inputBeganConn = nil
			end

			-- If the mode is disabled while being used, fire the event to toggle it off
			if self.isMouseLocked then
				self.mouseLockToggledEvent:Fire()
			end

			self.isMouseLocked = false
		end
	end
end

return MouseLockController
