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

InputBegan

This event fires when a user begins interacting with the GuiObject via a Human-Computer Interface device (Mouse button down, touch begin, keyboard button down, etc).

The UserInputService has a similarly named event that is not restricted to a specific UI element: UserInputService/InputBegan.

This event will always fire regardless of game state.

See also

  • GuiObject/InputEnded
  • GuiObject/InputChanged

Parameters

Name Type Default Description

An InputObject, which contains useful data for querying user input such as theEnum/UserInputType|type of input, Enum/UserInputState|state of input, and Enum/Position|screen coordinates of the input


Code Samples


Tracking the Beginning of Input on a GuiObject

The following example demonstrates one of many usage examples of handling user input from InputBegan depending on its type.

In order for this to work as expected, it must be placed in a LocalScript and a child of gui.

-- In order to use the InputBegan event, you must specify the GuiObject
local gui = script.Parent

-- A sample function providing multiple usage cases for various types of user input
local function inputBegan(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local keyPressed = input.KeyCode
		print("A key is being pushed down! Key:",input.KeyCode)
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("The left mouse button has been pressed down at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
		print("The right mouse button has been pressed down at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.Touch then
		print("A touchscreen input has started at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
		print("A button is being pressed on a gamepad! Button:",input.KeyCode)
	end
end

gui.InputBegan:Connect(inputBegan)