Hey devforum, Is there a way to lock player mouse while holding a key?

Hello Devforum, as the title implies I want to make it so while the player holds a key I made an inspect item system;

as you see when fully moving my mouse left it will eventually run out of screen space I want it so I can somehow try to lock the mouse or find a way to make the mouse not run out of space is this possible?

1 Like

Try something like this:

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local keyPressed = false


UserInputService.InputBegan:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		keyPressed = true
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
		repeat RunService.RenderStepped:Wait() until not keyPressed
		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	end
end)

UserInputService.InputEnded:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		keyPressed = false
	end
end)
3 Likes

There’s no need for the repeat wait which is inefficient, just lock their camera on input began and unlock it on input ended with no extra bool

UserInputService.InputBegan:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
	end
end)

UserInputService.InputEnded:Connect(function(input,gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	end
end)
3 Likes

@steven4547466
I am confused on what this script is accomplishing? I want to attempt to freeze the player mouse. If its possible in anyway I just need it to stay in the same spot while the player is holding down space to rotate the object.

1 Like

I think that is what the scripts do. Lock the player’s mouse. Do you still need help with this?

They didn’t work for me but maybe I am confused about their implementation.
Here is my code with the code provided above;

			local function Item_Rotate(input, gpe)
				
				local function inspect_Rotate()
					runService.RenderStepped:Connect(function()--Runs every frame to rotate the object
						if not inspectingRotationCheck then
							if rotCheck then--A debounce I forgot what it does
								playerGui.FakeMouse.Enabled = false --Sets mouse invisible to make it look nice
								local CurrentMousePos = Vector2.new(mouse.X,mouse.Y)--Gets the mouse position
								local change = (CurrentMousePos - LastMousePos) / 6.9--Gets the change

								tempSelectedModel:SetPrimaryPartCFrame(tempSelectedModel:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(change.X), 0) * CFrame.Angles(math.rad(change.Y), 0, 0))--Runs some math to rotate the object

								LastMousePos = CurrentMousePos--Sets the lastMousePos to the current so it can keep rotating

								tempSelectedModel.PrimaryPart.Orientation = Vector3.new(0, 0, 0)

								return LastMousePos
							elseif not rotCheck then
								playerGui.FakeMouse.Enabled = true--Makes the mouse visible again
							end
						else
							playerGui.FakeMouse.Enabled = true
						end
					end)
				end
		

				local keyPressed = false


				userInputService.InputBegan:Connect(function(input,gameProcessed)
					if input.UserInputType == Enum.UserInputType.MouseButton1 then
						userInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
					end
				end)

				userInputService.InputEnded:Connect(function(input,gameProcessed)
					if input.UserInputType == Enum.UserInputType.MouseButton1 then
						userInputService.MouseBehavior = Enum.MouseBehavior.Default
					end
				end)
			end



userInputService.InputBegan:Connect(Item_Rotate)

I feel like I dont understand the implementation of their code to freeze the mouse.
My goal is to have when the user holds space and moves their mouse it will rotate the item I want to then freeze the mouse while they are holding space and moving their mouse.

Their code uses UserInputService to check for any starting and ending inputs and locks the mouse down when it’s pressed down, however if you want to make it lock the mouse when the SPACE key is pressed then you need to change

if input.UserInputType == Enum.UserInputType.MouseButton1 then

to

if input.KeyCode == Enum.KeyCode.Space then

in both segments

If you are still confused on how it works, you can take a look at the UserInputService API and its events

Im sorry I understood this it this works although it wont work for the other parts of my script so I can not use it but I understand how this works now thank you!

Paste this as localscript in StarterPlayerScrpits. This should work:

--Copy and paste into starter player scripts
local UIS = game:GetService("UserInputService")

--Camera module services
local cameraModule = script.Parent:WaitForChild("PlayerModule"):WaitForChild("CameraModule")

local CameraUtils = require(cameraModule:WaitForChild("CameraUtils"))
local BaseCamera = require(cameraModule:WaitForChild("BaseCamera"))
local CameraUI = require(cameraModule:WaitForChild("CameraUI"))
local CameraInput = require(cameraModule:WaitForChild("CameraInput"))
local CameraToggleStateController = require(cameraModule:WaitForChild("CameraToggleStateController"))

local UserGameSettings = UserSettings():GetService("UserGameSettings")

local MouseLocked = true

local mouseBehavior = Enum.MouseBehavior.LockCenter
UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end --exit function if they are typing

	if input.KeyCode == Enum.KeyCode.Q then
		MouseLocked = not MouseLocked --turn mouselocked to true if false, false if true

		if MouseLocked then --if true then lock mouse
			mouseBehavior = Enum.MouseBehavior.LockCenter
			UIS.MouseBehavior = Enum.MouseBehavior.LockCenter

		else
			mouseBehavior = Enum.MouseBehavior.Default
			UIS.MouseBehavior = Enum.MouseBehavior.Default
		end
	end
end)

local debounce = false

function BaseCamera:UpdateMouseBehavior()
	local blockToggleDueToClickToMove = UserGameSettings.ComputerMovementMode == Enum.ComputerMovementMode.ClickToMove

	if self.isCameraToggle and blockToggleDueToClickToMove == false then
		CameraUI.setCameraModeToastEnabled(true)
		CameraInput.enableCameraToggleInput()
		CameraToggleStateController(self.inFirstPerson)
	else
		CameraUI.setCameraModeToastEnabled(false)
		CameraInput.disableCameraToggleInput()

		-- first time transition to first person mode or mouse-locked third person
		if self.inFirstPerson or self.inMouseLockedMode then
			CameraUtils.setRotationTypeOverride(Enum.RotationType.CameraRelative)
			if not debounce then
				debounce = true
				CameraUtils.setMouseBehaviorOverride(mouseBehavior)
			end
		else
			debounce = false
			CameraUtils.restoreRotationType()
			CameraUtils.restoreMouseBehavior()
		end
	end
end