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

InputObject

Show deprecated

InputObject

Show deprecated

An object created when an input begins that describes a particular user input, such as mouse movement, touches, keyboard, and more.

The object’s properties vary according the the type of input. Properties can be used to determine input Enum/UserInputState|state, Enum/UserInputType|type, DataType/Vector3|position, DataType/Vector3|delta, and the Enum/KeyCode that triggered the input (if applicable).

Once created at the beginning of an input, the same object persists and is updated until the input ends. As a result, you can track the object’s changes using the Instance/Changed|Changed event as the user changes the input in question. You can also place these objects into a list of active inputs track and interact with the object after it’s creation by an event such as UserInputService/InputBegan.

#See also

  • Most UserInputService events and functions return an InputObject to describe user input events and states
  • GuiObject events related to user input return an InputObject to describe user input

Properties

Vector3

Delta

A Vector3 describing the Delta between mouse/joystick movements

KeyCode

KeyCode

Contains an Enum that describes the kind of input used

Vector3

Position

Describes a positional value of this input

UserInputState

UserInputState

The state of the user’s input

UserInputType

UserInputType

The type of user input

Inherited from Instance: Show Hide

bool

Archivable

Determines if an Instance can be cloned using /Instance/Clone or saved to file.

string

ClassName

[ReadOnly] [NotReplicated]

A read-only string representing the class this Instance belongs to

int

DataCost

[ReadOnly] [NotReplicated] [Deprecated]

The cost of saving the instance using data persistence.

string

Name

A non-unique identifier of the Instance

Instance

Parent

Determines the hierarchical parent of the Instance

bool

RobloxLocked

If true, the Instance and its descendants cannot be indexed or edited by a Script or LocalScript and will throw an error if it is attempted

bool

archivable

[Hidden] [NotReplicated] [Deprecated]
string

className

[ReadOnly] [NotReplicated] [Deprecated]

Functions

bool

IsModifierKeyDown ( ModifierKey modifierKey )

Inherited from Instance: Show Hide

void

ClearAllChildren ( )

This function destroys all of an Instance's children.

Instance

Clone ( )

Create a deep copy of a Roblox instance and descendants where Archivable = true.

void

Destroy ( )

Sets the Instance/Parent property to nil, locks the Instance/Parent property, disconnects all connections and calls Destroy on all children.

Instance

FindFirstAncestor ( string name )

Returns the first ancestor of the Instance whose Instance/Name is equal to the given name.

Instance

FindFirstAncestorOfClass ( string className )

Returns the first ancestor of the Instance whose Instance/ClassName is equal to the given className.

Instance

FindFirstAncestorWhichIsA ( string className )

Returns the first ancestor of the Instance for whom Instance/IsA returns true for the given className.

Instance

FindFirstChild ( string name , bool recursive )

Returns the first child of the Instance found with the given name.

Instance

FindFirstChildOfClass ( string className )

Returns the first child of the Instance whose Instance/ClassName|ClassName is equal to the given className.

Instance

FindFirstChildWhichIsA ( string className , bool recursive )

Returns the first child of the Instance for whom Instance/IsA returns true for the given className.

Variant

GetAttribute ( string attribute )

RBXScriptSignal

GetAttributeChangedSignal ( string attribute )

Dictionary

GetAttributes ( )

Objects

GetChildren ( )

Returns an array containing all of the Instance's children.

string

GetDebugId ( int scopeLength )

[NotBrowsable]

Returns a coded string of the Instances DebugId used internally by Roblox.

Array

GetDescendants ( )

[CustomLuaState]

Returns an array containing all of the descendants of the instance

string

GetFullName ( )

Returns a string describing the Instance's ancestry.

RBXScriptSignal

GetPropertyChangedSignal ( string property )

Get an event that fires when a given property of an object changes.

bool

IsA ( string className )

[CustomLuaState]

Returns true if an Instance's class matches or inherits from a given class

bool

IsAncestorOf ( Instance descendant )

Returns true if an Instance is an ancestor of the given descendant.

bool

IsDescendantOf ( Instance ancestor )

Returns true if an Instance is a descendant of the given ancestor.

void

Remove ( )

[Deprecated]

Sets the object’s Parent to nil, and does the same for all its descendants.

void

SetAttribute ( string attribute , Variant value )

Instance

WaitForChild ( string childName , double timeOut )

[CustomLuaState] [CanYield]

Returns the child of the Instance with the given name. If the child does not exist, it will yield the current thread until it does.

Objects

children ( )

[Deprecated]

Returns an array of the object’s children.

Instance

clone ( )

[Deprecated]
void

destroy ( )

[Deprecated]
Instance

findFirstChild ( string name , bool recursive )

[Deprecated]
Objects

getChildren ( )

[Deprecated]
bool

isA ( string className )

[Deprecated] [CustomLuaState]
bool

isDescendantOf ( Instance ancestor )

[Deprecated]
void

remove ( )

[Deprecated]

Events

Inherited from Instance: Show Hide

RBXScriptSignal

AncestryChanged ( Instance child , Instance parent )

Fires when the Instance/Parent property of the object or one of its ancestors is changed.

RBXScriptSignal

AttributeChanged ( string attribute )

RBXScriptSignal

Changed ( string property )

Fired immediately after a property of an object changes.

RBXScriptSignal

ChildAdded ( Instance child )

Fires when an object is parented to this Instance.

RBXScriptSignal

ChildRemoved ( Instance child )

Fires when a child is removed from this Instance.

RBXScriptSignal

DescendantAdded ( Instance descendant )

Fires when a descendant is added to the Instance

RBXScriptSignal

DescendantRemoving ( Instance descendant )

Fires immediately before a descendant of the Instance is removed.

RBXScriptSignal

childAdded ( Instance child )

[Deprecated]

Code Samples


Handling InputEnded

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

-- In order to use the InputChanged event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")
 
-- A sample function providing multiple usage cases for various types of user input
UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local keyPressed = input.KeyCode
		print("A key has been released! Key:",input.KeyCode)
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("The left mouse button has been released at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
		print("The right mouse button has been released at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.Touch then
		print("A touchscreen input has been released at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
		print("A button has been released on a gamepad! Button:",input.KeyCode)
	end
	
	if gameProcessed then
		print("\tThe game engine internally observed this input!")
	else
		print("\tThe game engine did not internally observe this input!")
	end
end)

Handling InputBegan

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

-- In order to use the InputBegan event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")

-- A sample function providing multiple usage cases for various types of user input
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	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

	if gameProcessed then
		print("\tThe game engine internally observed this input!")
	else
		print("\tThe game engine did not internally observe this input!")
	end
end)

Handling InputChanged

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

-- In order to use the InputChanged event, the UserInputService service must be used
local userInputService = game:GetService("UserInputService")

-- Prints the current input position and the change (delta) in position
local function printMovement(input)
    print("\tPosition:",input.Position)
    print("\tMovement Delta:",input.Delta)
end
 
-- A sample function providing multiple usage cases for various types of user input
local function InputChanged(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
   	 print("The mouse has been moved!")
   	 printMovement(input)
    elseif input.UserInputType == Enum.UserInputType.MouseWheel then
   	 print("The mouse wheel has been scrolled!")
   	 print("\tWheel Movement:",input.Position.Z)
    elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
   	 if input.KeyCode == Enum.KeyCode.Thumbstick1 then
   		 print("The left thumbstick has been moved!")
   		 printMovement(input)
   	 elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
   		 print("The right thumbstick has been moved!")
   		 printMovement(input)
   	 elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
   		 print("The pressure being applied to the left trigger has changed!")
   		 print("\tPressure:",input.Position.Z)
   	 elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
   		 print("The pressure being applied to the right trigger has changed!")
   		 print("\tPressure:",input.Position.Z)
   	 end
    elseif input.UserInputType == Enum.UserInputType.Touch then
   	 print("The user's finger is moving on the screen!")
   	 printMovement(input)
    elseif input.UserInputType == Enum.UserInputType.Gyro then
   	 local rotInput,rotCFrame = UserInputService:GetDeviceRotation()
   	 local rotX,rotY,rotZ = rotCFrame:toEulerAnglesXYZ()
   	 local rot = Vector3.new(math.deg(rotX),math.deg(rotY),math.deg(rotZ))
   	 print("The rotation of the user's mobile device has been changed!")
   	 print("\tPosition",rotCFrame.p)
   	 print("\tRotation:",rot)
    elseif input.UserInputType == Enum.UserInputType.Accelerometer then
   	 print("The acceleration of the user's mobile device has been changed!")
   	 printMovement(input)
    end    
end

userInputService.InputChanged:Connect(InputChanged)