PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

GetMouseButtonsPressed

This function returns an array of InputObject|InputObjects corresponding to the mouse buttons currently being pressed down.

Mouse buttons that are tracked by this function include:

Name Description
MouseButton1 The left mouse button.
MouseButton2 The right mouse button.
MouseButton3 The middle mouse button.

If the user is not pressing any mouse button down when the function is called, it will return an empty array.

As UserInputService is client-side only, this function can only be used in a LocalScript.

Returns

Return Type Summary

An array of InputObject|InputObjects corresponding to the mouse buttons currently being currently held down


Code Samples


Check which MouseButtons are Pressed

This example checks if the user pressed MouseButton1, MouseButton2, or both mouse buttons on UserInputService/InputBegan|InputBegan.

The example can be extended to behave differently depending on which mouse buttons are pressed.

local UserInputService = game:GetService("UserInputService")

-- InputBegan is a UserInputService event that fires when the player
-- begins interacting via a Human-User input device
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
-- Returns an array of the pressed MouseButtons
local buttons = UserInputService:GetMouseButtonsPressed()

	local m1Pressed, m2Pressed = false, false
	for _, button in pairs (buttons) do
		-- Check if MouseButton1 is pressed
		if (button.UserInputType.Name == "MouseButton1") then
			print("MouseButton1 is pressed")
			m1Pressed = true
		end

		-- Check if MouseButton2 is pressed
		if (button.UserInputType.Name == "MouseButton2") then
			print("MouseButton2 is pressed")
			m2Pressed = true
		end
		
		-- Check if both MouseButton1 and MouseButton2 are pressed
		if (m1Pressed and m2Pressed) then
			print("Both mouse buttons are pressed")
		end
	end
end)