UserInputService
UserInputService is primarily used to detect the input types available on a user's device, as well as detect input events. It allows you to perform different actions depending on the device and, in turn, provide the best experience for the end user.
As this service is intended for client-side usage only, its properties, methods, and events can only be used in a LocalScript, a ModuleScript required by a LocalScript, or a Script with RunContext set to Enum.RunContext.Client.
Summary
Properties
Describes whether the user's device has an accelerometer.
Describes whether the user's device has an available gamepad.
Describes whether the user's device has a gyroscope.
Describes whether the user's device has a keyboard available.
Determines whether the user's mouse can be moved freely or is locked.
Scales the delta (change) output of the user's Mouse.
Describes whether the user's device has a mouse available.
The content ID of the image for the user's mouse icon.
Determines whether the mouse icon is visible.
Determines the position of the on-screen keyboard.
Determines the size of the on-screen keyboard.
Describes whether an on-screen keyboard is currently visible on the user's screen.
Queries the primary input type a player is using, based on anticipated user behavior.
Describes whether the user's device has a touch screen available.
Indicates whether the user is using a virtual reality headset.
Methods
Returns whether the given Enum.UserInputType gamepad supports a button corresponding with the given Enum.KeyCode.
Returns an array of Enum.UserInputType gamepads currently connected.
Returns an InputObject that describes the device's current acceleration.
Returns an InputObject describing the device's current gravity vector.
Returns an InputObject and a CFrame describing the device's current rotation vector.
Returns the currently TextBox the client is currently focused on.
Returns whether a gamepad with the given Enum.UserInputType is connected.
Returns an array of InputObjects for all available inputs on the given gamepad, representing each input's last input state.
Returns an image for the requested Enum.KeyCode.
Returns an array of InputObjects associated with the keys currently being pressed down.
Returns the Enum.UserInputType associated with the user's most recent input.
Returns an array of InputObjects associated with the mouse buttons currently being held down.
Returns the change, in pixels, of the position of the player's Mouse in the last rendered frame. Only works if the mouse is locked.
Returns the current screen location of the player's Mouse relative to the top-left corner of the screen.
Returns an array of gamepads connected and enabled for GuiObject navigation in descending order of priority.
Returns a string representing a key the user should press in order to input a given Enum.KeyCode.
Returns an array of KeyCodes that the gamepad associated with the given Enum.UserInputType supports.
Determines whether a particular button is pressed on a gamepad.
Returns whether the given key is currently held down.
Returns whether the given mouse button is currently held down.
Returns true if the specified gamepad is allowed to control navigation and selection GuiObjects.
Recenters the CFrame of the VR headset to the current orientation of the headset worn by the user.
Sets whether or not the specified gamepad can move the GuiObject navigator.
Events
Fires when a user moves a device that has an accelerometer.
Fires when the force of gravity changes on a device that has an enabled accelerometer.
Fires when a user rotates a device that has a gyroscope.
Fires when a gamepad is connected to the client.
Fires when a gamepad is disconnected from the client.
Fires when a user begins interacting with an input device such as a mouse or gamepad.
Fires when a user changes how they're interacting with an input device such as a mouse or gamepad.
Fires when a user stops interacting with an input device such as a mouse or gamepad.
Fires whenever the client makes a request for their character to jump.
Fires whenever the client's Enum.UserInputType is changed.
- PointerAction(wheel : number,pan : Vector2,pinch : number,gameProcessedEvent : boolean):RBXScriptSignal
Fires when the user performs a specific pointer action.
Fires when the client loses focus on a TextBox.
Fires when the client focuses on a TextBox.
- TouchDrag(dragDirection : Enum.SwipeDirection,numberOfTouches : number,gameProcessedEvent : boolean):RBXScriptSignal
Fires when the user drags on the screen of a TouchEnabled device.
Fires when a user releases their finger from the screen of a TouchEnabled device.
- TouchLongPress(touchPositions : Array,state : Enum.UserInputState,gameProcessedEvent : boolean):RBXScriptSignal
Fires when a user holds at least one finger for a short amount of time on the screen of a TouchEnabled device.
Fires when a user moves their finger on the screen of a TouchEnabled device.
- TouchPan(touchPositions : Array,totalTranslation : Vector2,velocity : Vector2,state : Enum.UserInputState,gameProcessedEvent : boolean):RBXScriptSignal
Fires when the user drags at least one finger on the screen of a TouchEnabled device.
- TouchPinch(touchPositions : Array,scale : number,velocity : number,state : Enum.UserInputState,gameProcessedEvent : boolean):RBXScriptSignal
Fires when a user performs a pinch gesture on the screen of a TouchEnabled device.
- TouchRotate(touchPositions : Array,rotation : number,velocity : number,state : Enum.UserInputState,gameProcessedEvent : boolean):RBXScriptSignal
Fires when a user rotates two fingers on the screen of a TouchEnabled device.
Fires when a user places their finger on the screen of a TouchEnabled device.
- TouchSwipe(swipeDirection : Enum.SwipeDirection,numberOfTouches : number,gameProcessedEvent : boolean):RBXScriptSignal
Fires on a TouchEnabled device when a user places their finger(s) down on the screen, pans across the screen, and lifts their finger(s) off with a certain speed of movement.
Fires when a user taps their finger on the screen of a TouchEnabled device.
Fires when a user taps their finger on the screen of a TouchEnabled device and the tap location is in the 3D world.
Fires when the window of the Roblox client loses focus on the user's screen.
Fires when the window of the Roblox client gains focus on the user's screen.
Properties
AccelerometerEnabled
This property describes whether the user's device has an accelerometer, a component found in most mobile devices that measures acceleration (change in speed).
If the device has an enabled accelerometer, you can get its current acceleration by using the GetDeviceAcceleration() method or track when the device's acceleration changes through the DeviceAccelerationChanged event.
Code Samples
This code adds a force on a part so that it falls in the direction of actual gravity relative to the user's device. In order for this example to work as expected, it must be placed in a LocalScript and the user's device must have an accelerometer.
local Workspace = game:GetService("Workspace")
local UserInputService = game:GetService("UserInputService")
local ball = script.Parent:WaitForChild("Ball")
local mass = ball:GetMass()
local gravityForce = ball:WaitForChild("GravityForce")
local function moveBall(gravity)
gravityForce.Force = gravity.Position * Workspace.Gravity * mass
end
if UserInputService.AccelerometerEnabled then
UserInputService.DeviceGravityChanged:Connect(moveBall)
end
GamepadEnabled
This property describes whether the user's device has an available gamepad. If true, you can use gamepad‑related methods such as GetConnectedGamepads().
For seamless cross-platform compatibility on mixed-input devices, see PreferredInput which more accurately reflects which input (mouse/keyboard, touch, gamepad, etc.) the player is likely using as the primary input.
GyroscopeEnabled
This property describes whether the user's device has a gyroscope, a component found in most mobile devices that detects orientation and rotational speed.
If the device has a gyroscope, you can incorporate it into your experience using the GetDeviceRotation() method or track when the device's rotation changes through the DeviceRotationChanged event.
KeyboardEnabled
This property describes whether the user's device has a keyboard available. If true, you can use key‑related methods such as IsKeyDown() or GetKeysPressed().
For seamless cross-platform compatibility on mixed-input devices, see PreferredInput which more accurately reflects which input (mouse/keyboard, touch, gamepad, etc.) the player is likely using as the primary input.
MouseBehavior
This property sets how the user's mouse behaves based on the Enum.MouseBehavior enum. It can be set to three values:
- Enum.MouseBehavior.Default — The mouse moves freely around the user's screen.
- Enum.MouseBehavior.LockCenter — The mouse is locked and cannot move from the center of the user's screen.
- Enum.MouseBehavior.LockCurrentPosition — The mouse is locked and cannot move from its current position on the user's screen at the time of locking.
The value of this property does not affect the sensitivity of events tracking mouse movement. For example, GetMouseDelta returns the same Vector2 screen position in pixels regardless of whether the mouse is locked or able to move freely around the user's screen. As a result, default scripts like those controlling the camera are not impacted by this property.
This property is overridden if a GuiButton with Modal enabled is Visible unless the player's right mouse button is down.
Note that if the mouse is locked, InputChanged will still fire when the player moves the mouse and will pass in the delta that the mouse attempted to move by. Additionally, if the player is kicked from the experience, the mouse will be forcefully unlocked.
MouseDeltaSensitivity
This property determines the sensitivity of the user's Mouse. It can be used to adjust the sensitivity of events tracking mouse movement, such as GetMouseDelta().
This property does not affect the movement of the mouse icon, nor the camera sensitivity that the user has selected for their client.
This property has a maximum value of 10 and a minimum value of 0. When sensitivity is 0, events that track the mouse's movement will still fire but all parameters and properties indicating the change in mouse position will return 0.
MouseEnabled
This property describes whether the user's device has a mouse available. If true, you can use mouse‑related methods such as GetMouseLocation().
For seamless cross-platform compatibility on mixed-input devices, see PreferredInput which more accurately reflects which input (mouse/keyboard, touch, gamepad, etc.) the player is likely using as the primary input.
MouseIcon
This property determines the content ID of the image for the user's mouse icon. If blank, a default arrow pointer is used. While the cursor hovers over certain UI objects such as an ImageButton, TextButton, TextBox, or ProximityPrompt, this image will be overridden and temporarily ignored.
To hide the cursor entirely, do not use a transparent image; instead, set MouseIconEnabled to false.
Code Samples
This example changes the user mouse icon to look like a dragon image.
local UserInputService = game:GetService("UserInputService")
-- In order to restore the cursor to what it was set to previously, it will need to be saved to a variable
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- Only update the saved cursor if it's not currently saved
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- Only restore the mouse cursor if there's a saved cursor to restore
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- Don't restore the same cursor twice (might overwrite another script)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.com/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()
OnScreenKeyboardPosition
This property describes the position of the on-screen keyboard in pixels. The keyboard's position is Vector2.new(0, 0) when it is not visible.
See also OnScreenKeyboardVisible and OnScreenKeyboardSize.
OnScreenKeyboardSize
This property describes the size of the on-screen keyboard in pixels. The keyboard's size is Vector2.new(0, 0) when it is not visible.
See also OnScreenKeyboardVisible and OnScreenKeyboardPosition.
OnScreenKeyboardVisible
This property describes whether an on-screen keyboard is currently visible on the user's screen.
See also OnScreenKeyboardSize and OnScreenKeyboardPosition.
PreferredInput
This read-only property lets you query the primary input type a player is likely using, based on anticipated user behavior, to ensure UI elements like on‑screen buttons and menus work elegantly across devices. For example, a touch‑enabled device assumes touch is the default input and that touch buttons may appear for actions, but if a player connects an additional bluetooth keyboard/mouse or gamepad, you can assume they want to switch to that as the primary input type and possibly use touch as a backup input for on‑screen UI.
The behavior of PreferredInput is summarized in the following table. Its value changes based on the value of legacy UserInputService properties such as KeyboardEnabled, GamepadEnabled, and TouchEnabled, as well as the player's most recent interaction with a connected gamepad or keyboard/mouse.
KeyboardEnabled; GamepadEnabled; TouchEnabled | Most Recent Interaction | PreferredInput | Example |
---|---|---|---|
false; false; true | (don't care) | Touch | ⒜ |
true; false; (don't care) | (don't care) | KeyboardAndMouse | ⒝ |
false; true; (don't care) | (don't care) | Gamepad | ⒞ |
true; true; (don't care) | Keyboard or Mouse | KeyboardAndMouse | ⒟ |
true; true; (don't care) | Gamepad | Gamepad | ⒠ |
Code Samples
The following LocalScript is a template for initially detecting the preferred input and responding to changes during gameplay.
local UserInputService = game:GetService("UserInputService")
local function preferredInputChanged()
local preferredInput = UserInputService.PreferredInput
if preferredInput == Enum.PreferredInput.Touch then
-- Player is on touch-enabled device with no other input types available/connected
print("Touch")
elseif preferredInput == Enum.PreferredInput.Gamepad then
-- Player has connected or most recently interacted with a gamepad
print("Gamepad")
elseif preferredInput == Enum.PreferredInput.KeyboardAndMouse then
-- Player has connected or most recently interacted with a keyboard or mouse
print("KeyboardAndMouse")
end
end
preferredInputChanged()
UserInputService:GetPropertyChangedSignal("PreferredInput"):Connect(function()
preferredInputChanged()
end)
TouchEnabled
This property describes whether the user's device has a touch screen available. If true, you can use touch‑related events such as TouchStarted and TouchMoved.
For seamless cross-platform compatibility on mixed-input devices, see PreferredInput which more accurately reflects which input (mouse/keyboard, touch, gamepad, etc.) the player is likely using as the primary input.
Methods
GamepadSupports
This method returns whether the given Enum.UserInputType gamepad supports a button corresponding with the given Enum.KeyCode.
Parameters
The Enum.UserInputType of the gamepad.
The Enum.KeyCode of the button in question.
Returns
Whether the given gamepad supports a button corresponding with the given Enum.KeyCode.
GetConnectedGamepads
This method returns an array of Enum.UserInputType gamepads currently connected. If no gamepads are connected, the array will be empty.
Alternatively to detecting all gamepads, the PreferredInput property can be used to more accurately reflect which input (mouse/keyboard, touch, gamepad, etc.) the player is likely using as the primary input.
Returns
An array of UserInputTypes corresponding with the gamepads connected to the user's device.
GetDeviceAcceleration
This method returns an InputObject that describes the device's current acceleration. For this to function, the user's device must have an enabled accelerometer as queried through the AccelerometerEnabled property.
To track when the device's acceleration changes, use the DeviceAccelerationChanged event.
Returns
Code Samples
This example checks if a user's device has an enabled accelerometer and, if true, it outputs the current acceleration.
local UserInputService = game:GetService("UserInputService")
if UserInputService.AccelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
warn("Cannot get device acceleration because device does not have an enabled accelerometer!")
end
GetDeviceGravity
This method returns an InputObject describing the device's current gravity vector. The vector is determined by the device's orientation relative to the real-world force of gravity. For example:
- Vector3.new(0, 0, -9.18) if the device is perfectly upright (portrait)
- Vector3.new(9.81, 0, 0) if the left side of the device is pointing down
- Vector3.new(0, -9.81, 0) if the back of the device is pointing down
Gravity is only tracked for devices with an enabled gyroscope as queried through GyroscopeEnabled.
To track when the device's gravity changes, use the DeviceGravityChanged event.
Returns
Code Samples
This example implements a level where the bubble will move along the X and Z axes depending on the device's current gryoscope position.
local UserInputService = game:GetService("UserInputService")
local bubble = script.Parent:WaitForChild("Bubble")
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = CFrame.new(0, 20, 0) * CFrame.Angles(-math.pi / 2, 0, 0)
if UserInputService.GyroscopeEnabled then
-- Bind event to when gyroscope detects change
UserInputService.DeviceGravityChanged:Connect(function(accel)
-- Move the bubble in the world based on the gyroscope data
bubble.Position = Vector3.new(-8 * accel.Position.X, 1.8, -8 * accel.Position.Z)
end)
end
GetDeviceRotation
This method returns an InputObject and a CFrame describing the device's current rotation vector.
Device rotation is only tracked for devices with an enabled gyroscope as queried through GyroscopeEnabled.
Returns
Code Samples
This example checks if a user's device has an enabled gyroscope and, if true, it outputs the device's current CFrame.
local UserInputService = game:GetService("UserInputService")
if UserInputService.GyroscopeEnabled then
local _, cf = UserInputService:GetDeviceRotation()
print(cf)
else
warn("Cannot get device rotation because device does not have an enabled gyroscope!")
end
GetFocusedTextBox
This method returns the TextBox the client is currently focused on. A TextBox can be manually selected by the user, or selection can be forced using the TextBox:CaptureFocus() method. If no TextBox is selected, this method will return nil.
Returns
GetGamepadConnected
This method returns whether a gamepad with the given Enum.UserInputType is connected.
Alternatively to detecting a specific gamepad by Enum.UserInputType, the PreferredInput property can be used to more accurately reflect which input (mouse/keyboard, touch, gamepad, etc.) the player is likely using as the primary input.
Parameters
The Enum.UserInputType of the gamepad in question.
Returns
Whether a gamepad associated with Enum.UserInputType is connected.
GetGamepadState
This method returns an array of InputObjects for all available inputs on the given Enum.UserInputType gamepad, representing each input's last input state.
Parameters
The Enum.UserInputType corresponding with the gamepad in question.
Returns
An array of InputObjects representing the current state of all available inputs for the given gamepad.
GetImageForKeyCode
This method takes the requested Enum.KeyCode and returns the associated image for the currently connected gamepad device (limited to Xbox, PlayStation, and Windows). This means that if the connected controller is an Xbox One controller, the user sees Xbox assets. Similarly, if the connected device is a PlayStation controller, the user sees PlayStation assets. If you want to use custom assets, see GetStringForKeyCode().
Parameters
The Enum.KeyCode for which to fetch the associated image.
Returns
The returned image asset ID.
Code Samples
This API returns the requested image for the given Enum.KeyCode.
local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImage
GetKeysPressed
This method returns an array of InputObjects associated with the keys currently being pressed down. The array can be iterated through to determine which keys are currently being pressed, using the InputObject.KeyCode names or values.
To check if a specific key is being pressed, use IsKeyDown().
Returns
An array of InputObjects associated with the keys currently being pressed.
GetLastInputType
This method returns the Enum.UserInputType associated with the user's most recent input. For example, if the user's previous input had been pressing the A key, the returned Enum.UserInputType value would be Keyboard.
For seamless cross-platform compatibility on mixed-input devices, see PreferredInput which more accurately reflects which input (mouse/keyboard, touch, gamepad, etc.) the player is likely using as the primary input. GetLastInputType() remains for advanced workflows or control schemes that rely on detecting and responding to the player's specific most recent Enum.UserInputType.
Returns
The Enum.UserInputType associated with the user's most recent input.
Code Samples
This example gets the last Enum.UserInputType and outputs if it was keyboard input.
local UserInputService = game:GetService("UserInputService")
if UserInputService:GetLastInputType() == Enum.UserInputType.Keyboard then
print("Most recent input was keyboard!")
end
GetMouseButtonsPressed
This method returns an array of InputObjects associated with the mouse buttons currently being held down. The array can be iterated through to determine which buttons are currently being held, using the InputObject.KeyCode names or values.
Mouse buttons that are tracked by this method include MouseButton1 (left), MouseButton2 (right), and MouseButton3 (middle).
If the user is not pressing any mouse button down when the method is called, it will return an empty array.
Returns
An array of InputObjects corresponding to the mouse buttons currently being currently held down.
Code Samples
This example checks if the user pressed MouseButton1 (left), MouseButton2 (right), or both mouse buttons. This can be extended to behave differently depending on which mouse buttons are pressed, including MouseButton3 (middle).
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
-- Return an array of the pressed mouse buttons
local buttonsPressed = UserInputService:GetMouseButtonsPressed()
local m1, m2 = false, false
for _, button in buttonsPressed do
if button.UserInputType == Enum.UserInputType.MouseButton1 then
print("MouseButton1 pressed!")
m1 = true
end
if button.UserInputType == Enum.UserInputType.MouseButton2 then
print("MouseButton2 pressed!")
m2 = true
end
if m1 and m2 then
print("Both mouse buttons pressed!")
end
end
end)
GetMouseDelta
This method returns the change, in pixels, of the position of the player's Mouse in the last rendered frame, only if the mouse has been locked using the MouseBehavior property; otherwise the returned Vector2 values will be 0.
The sensitivity of the mouse, determined in the client's settings and MouseDeltaSensitivity, will influence the result.
Returns
Change in movement of the mouse.
Code Samples
The following example measures any mouse movement in pixels from the last render step to the current render step. If the user has set their camera sensitivity to be higher or lower than 1 in the experience's menu, it will affect the returned value.
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
local function onRenderStep()
local delta = UserInputService:GetMouseDelta()
if delta ~= Vector2.new(0, 0) then
print("The mouse has moved", delta, "since the last step")
end
end
RunService:BindToRenderStep("MeasureMouseMovement", Enum.RenderPriority.Input.Value, onRenderStep)
GetMouseLocation
This method returns a Vector2 representing the current screen location of the player's Mouse in pixels relative to the top‑left corner. This does not account for the Enum.ScreenInsets; to get the top‑left and bottom‑right insets, call GuiService:GetGuiInset().
If the location of the mouse pointer is offscreen or the player's device does not have a mouse, the returned value will be undetermined.
Returns
GetNavigationGamepads
This method returns an array of gamepads that are connected and enabled for GuiObject navigation, but does not influence navigation controls. This list is in descending order of priority, meaning it can be iterated over to determine which gamepad should have navigation control.
See also SetNavigationGamepad(), IsNavigationGamepad(), and GetConnectedGamepads().
Returns
An array of UserInputTypes that can be used for navigation, in descending order of priority.
GetStringForKeyCode
This method returns a string representing a key the user should press in order to input a given Enum.KeyCode, keeping in mind their keyboard layout. For key codes that require some modifier to be held, this method returns the key to be pressed in addition to the modifier. See the examples below for further explanation.
When using Roblox with a non‑QWERTY keyboard layout, key codes are mapped to equivalent QWERTY positions. For example, pressing A on an AZERTY keyboard results in Enum.KeyCode.Q, potentially leading to mismatched information on experience UI elements. This method solves the issue by providing the actual key to be pressed while using non‑QWERTY keyboard layouts.
KeyCode | QWERTY Return | AZERTY Return |
---|---|---|
Enum.KeyCode.Q | Q | A |
Enum.KeyCode.W | W | Z |
Enum.KeyCode.Equals | = | = |
Enum.KeyCode.At | 2 because @ is typed with Shift2 | É |
Gamepad Usage
GetStringForKeyCode() returns the string mapping for the Enum.KeyCode for the most recently connected gamepad. If the connected controller is not supported, the method returns the default string conversion for the requested key code.
The following example shows how you can map custom assets for ButtonA:
local UserInputService = game:GetService("UserInputService")local imageLabel = script.Parentlocal key = Enum.KeyCode.ButtonAlocal mappings = {ButtonA = "rbxasset://BUTTON_A_ASSET", -- Replace with the desired ButtonA assetButtonCross = "rbxasset://BUTTON_CROSS_ASSET" -- Replace with the desired ButtonCross asset}local mappedKey = UserInputService:GetStringForKeyCode(key)local image = mappings[mappedKey]imageLabel.Image = image
Gamepad Mappings
The directional pad key codes do not have any differences based on device. Enum.KeyCode.ButtonSelect has slightly different behavior in some cases. Use both PlayStation mappings to ensure users see the correct buttons.
KeyCode | PlayStation Return Value | Xbox Return Value |
---|---|---|
Enum.KeyCode.ButtonA | ButtonCross | ButtonA |
Enum.KeyCode.ButtonB | ButtonCircle | ButtonB |
Enum.KeyCode.ButtonX | ButtonSquare | ButtonX |
Enum.KeyCode.ButtonY | ButtonTriangle | ButtonY |
Enum.KeyCode.ButtonL1 | ButtonL1 | ButtonLB |
Enum.KeyCode.ButtonL2 | ButtonL2 | ButtonLT |
Enum.KeyCode.ButtonL3 | ButtonL3 | ButtonLS |
Enum.KeyCode.ButtonR1 | ButtonR1 | ButtonRB |
Enum.KeyCode.ButtonR2 | ButtonR2 | ButtonRT |
Enum.KeyCode.ButtonR3 | ButtonR3 | ButtonRS |
Enum.KeyCode.ButtonStart | ButtonOptions | ButtonStart |
Enum.KeyCode.ButtonSelect | ButtonTouchpad and ButtonShare | ButtonSelect |
Legacy System Images
When using a Enum.KeyCode that may be better represented as an image, such as for an ImageLabel in a user interface, you can use the following legacy icons. However, it's recommended that you use GetImageForKeyCode() as a more modern, cross‑platform method to retrieve Xbox and PlayStation controller icons.
KeyCode | Asset ID |
---|---|
Enum.KeyCode.ButtonX | rbxasset://textures/ui/Controls/xboxX.png |
Enum.KeyCode.ButtonY | rbxasset://textures/ui/Controls/xboxY.png |
Enum.KeyCode.ButtonA | rbxasset://textures/ui/Controls/xboxA.png |
Enum.KeyCode.ButtonB | rbxasset://textures/ui/Controls/xboxB.png |
Enum.KeyCode.DPadLeft | rbxasset://textures/ui/Controls/dpadLeft.png |
Enum.KeyCode.DPadRight | rbxasset://textures/ui/Controls/dpadRight.png |
Enum.KeyCode.DPadUp | rbxasset://textures/ui/Controls/dpadUp.png |
Enum.KeyCode.DPadDown | rbxasset://textures/ui/Controls/dpadDown.png |
Enum.KeyCode.ButtonSelect | rbxasset://textures/ui/Controls/xboxView.png |
Enum.KeyCode.ButtonStart | rbxasset://textures/ui/Controls/xboxmenu.png |
Enum.KeyCode.ButtonL1 | rbxasset://textures/ui/Controls/xboxLB.png |
Enum.KeyCode.ButtonR1 | rbxasset://textures/ui/Controls/xboxRB.png |
Enum.KeyCode.ButtonL2 | rbxasset://textures/ui/Controls/xboxLT.png |
Enum.KeyCode.ButtonR2 | rbxasset://textures/ui/Controls/xboxRT.png |
Enum.KeyCode.ButtonL3 | rbxasset://textures/ui/Controls/xboxLS.png |
Enum.KeyCode.ButtonR3 | rbxasset://textures/ui/Controls/xboxRS.png |
Enum.KeyCode.Thumbstick1 | rbxasset://textures/ui/Controls/xboxLSDirectional.png |
Enum.KeyCode.Thumbstick2 | rbxasset://textures/ui/Controls/xboxRSDirectional.png |
Enum.KeyCode.Backspace | rbxasset://textures/ui/Controls/backspace.png |
Enum.KeyCode.Return | rbxasset://textures/ui/Controls/return.png |
Enum.KeyCode.LeftShift | rbxasset://textures/ui/Controls/shift.png |
Enum.KeyCode.RightShift | rbxasset://textures/ui/Controls/shift.png |
Enum.KeyCode.Tab | rbxasset://textures/ui/Controls/tab.png |
Enum.KeyCode.Quote | rbxasset://textures/ui/Controls/apostrophe.png |
Enum.KeyCode.Comma | rbxasset://textures/ui/Controls/comma.png |
Enum.KeyCode.Backquote | rbxasset://textures/ui/Controls/graveaccent.png |
Enum.KeyCode.Period | rbxasset://textures/ui/Controls/period.png |
Enum.KeyCode.Space | rbxasset://textures/ui/Controls/spacebar.png |
Parameters
Returns
GetSupportedGamepadKeyCodes
This method returns an array of KeyCodes that the gamepad associated with the given Enum.UserInputType supports. If called on a non‑connected gamepad, returns an empty array.
To determine if a specific Enum.KeyCode is supported, use GamepadSupports().
Parameters
The Enum.UserInputType of the gamepad.
Returns
IsGamepadButtonDown
This method returns true if a particular button is pressed on a gamepad, otherwise returns false.
See also InputBinding as a way to hook gamepad and other input interactions to InputActions.
Parameters
The Enum.UserInputType of the given gamepad.
The Enum.KeyCode of the specified gamepad button.
Returns
Whether the specified button on the given gamepad is pressed is pressed.
IsKeyDown
This method returns true if a particular key is pressed on a keyboard, otherwise returns false.
See also InputBinding as a way to hook key and other input interactions to InputActions.
Parameters
The Enum.KeyCode of the key.
Returns
Whether the specified key is being held down.
IsMouseButtonPressed
This method returns true if a particular mouse button is pressed, otherwise returns false.
See also InputBinding as a way to hook mouse button and other input interactions to InputActions.
Parameters
The Enum.UserInputType of the mouse button.
Returns
Whether the given mouse button is currently held down.
IsNavigationGamepad
This method returns true if the specified gamepad is allowed to control navigation and selection GuiObjects.
Use SetNavigationGamepad() to set a navigation gamepad, or GetNavigationGamepads() to get a list of all navigation gamepads.
Parameters
The Enum.UserInputType of the specified gamepad.
Returns
Whether the specified gamepad is a navigation gamepad.
RecenterUserHeadCFrame
This method recenters the CFrame of the VR headset to the current orientation of the headset worn by the user. This means that the headset's current orientation is set to CFrame.new().
This method behaves identically to the VRService method RecenterUserHeadCFrame().
Returns
SetNavigationGamepad
This method sets whether the specified gamepad can move the GuiObject navigator.
Use IsNavigationGamepad() to check if a specified gamepad is a set to be a navigation gamepad, or GetNavigationGamepads() to retrieve a list of all navigation gamepads.
Parameters
The Enum.UserInputType of the specified gamepad.
Whether the specified gamepad can move the GUI navigator.
Returns
Events
DeviceAccelerationChanged
This event fires when a user moves a device that has an accelerometer, a component found in most mobile devices that measures acceleration (change in speed). To determine whether a user's device has an accelerometer enabled, use AccelerometerEnabled.
This event can be used along with GetDeviceAcceleration() to determine the current movement of a user's device.
Parameters
An InputObject, with a UserInputType of Accelerometer and Position that shows the force of gravity on each local device axis.
Code Samples
This example uses the accelerometer to move the player character when a mobile device is accelerated. The character will move along the axis that the device was moved.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local SENSITIVITY = 0.2
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local ready = true
local function changeAcceleration(acceleration)
if ready then
ready = false
local accel = acceleration.Position
if accel.Y >= SENSITIVITY then
humanoid.Jump = true
end
if accel.Z <= -SENSITIVITY then
humanoid:Move(Vector3.new(-1, 0, 0))
end
if accel.Z >= SENSITIVITY then
humanoid:Move(Vector3.new(1, 0, 0))
end
if accel.X <= -SENSITIVITY then
humanoid:Move(Vector3.new(0, 0, 1))
end
if accel.X >= SENSITIVITY then
humanoid:Move(Vector3.new(0, 0, -1))
end
task.wait(1)
ready = true
end
end
if UserInputService.AccelerometerEnabled then
UserInputService.DeviceAccelerationChanged:Connect(changeAcceleration)
end
DeviceGravityChanged
This event fires when the device's gravity Vector3 changes on a device that has an accelerometer. To determine whether a user's device has an accelerometer enabled, use AccelerometerEnabled.
A device's gravity vector represent the force of gravity on each of the device's X, Y, and Z axes. While gravity never changes, the force it exerts on each axis changes when the device rotates and changes orientation. The force value exerted on each axis is a unit vector ranging from -1 to 1.
If the device has an enabled accelerometer, you can use the GetDeviceGravity() method to get the current force of gravity on the user's device.
Parameters
An InputObject with a Position property that shows the force of gravity on each local device axis. This position can be used as a direction to determine the direction of gravity relative to the device.
Code Samples
This code adds a force on a part so that it falls in the direction of actual gravity relative to the user's device. In order for this example to work as expected, it must be placed in a LocalScript and the user's device must have an accelerometer.
local Workspace = game:GetService("Workspace")
local UserInputService = game:GetService("UserInputService")
local ball = script.Parent:WaitForChild("Ball")
local mass = ball:GetMass()
local gravityForce = ball:WaitForChild("GravityForce")
local function moveBall(gravity)
gravityForce.Force = gravity.Position * Workspace.Gravity * mass
end
if UserInputService.AccelerometerEnabled then
UserInputService.DeviceGravityChanged:Connect(moveBall)
end
DeviceRotationChanged
This event fires when a user rotates a device that has a gyroscope, a component found in most mobile devices that detects orientation and rotational speed. To check if a user's device has an enabled gyroscope, use GyroscopeEnabled.
To query the current device rotation, use the GetDeviceRotation() method.
Note that this event only fires when the Roblox client window is in focus. Inputs will not be captured when the window is minimized.
Parameters
An InputObject providing info about the device's rotation. Position represents the new rotation a Vector3 positional value and Delta represents the change in rotation in a Vector3 positional value.
GamepadConnected
This event fires when a gamepad is connected to the client. You can also use GetConnectedGamepads() to find the correct gamepad to use.
Alternatively, you can detect value changes to the PreferredInput property which more accurately reflects which input (mouse/keyboard, touch, gamepad, etc.) the player is likely using as the primary input.
See also GamepadDisconnected.
Parameters
The Enum.UserInputType of the connected gamepad.
GamepadDisconnected
This event fires when a gamepad is disconnected from the client.
Alternatively, you can detect value changes to the PreferredInput property which more accurately reflects which input (mouse/keyboard, touch, gamepad, etc.) the player is likely using as the primary input.
See also GamepadConnected.
Parameters
TheEnum.UserInputType of the disconnected gamepad.
InputBegan
This event fires when a user begins interacting with an input device such as a mouse or gamepad, such as when they first interact with a gamepad button, although it does not capture mouse wheel movements. Can be used along with InputChanged and InputEnded to track when user input begins, changes, and ends.
See also InputBinding as a way to hook input device interactions to InputActions.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An InputObject instance containing information about the user's input.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
InputChanged
This event fires when a user changes how they're interacting with an input device such as a mouse or gamepad. Can be used along with InputBegan and InputEnded to track when user input begins, changes, and ends.
See also InputBinding as a way to hook input device interactions to InputActions.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An InputObject instance containing information about the user's input.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true. To ignore events that are automatically handled by Roblox like scrolling in a ScrollingFrame, check that gameProcessedEvent is false.
InputEnded
This event fires when a user stops interacting with an input device such as a mouse or gamepad, such as when they release a gamepad button. Can be used along with InputBegan and InputChanged to track when user input begins, changes, and ends.
See also InputBinding as a way to hook input device interactions to InputActions.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An InputObject instance containing information about the user input.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
JumpRequest
This event fires when there is a jump request from the client, for example when the client presses the spacebar or jump button on mobile. Default behavior is to set the player's Humanoid.Jump property to true which makes the player's character jump.
Since this event fires multiple times for a single jump request, using a debounce is recommended.
Code Samples
This code sample disables default jumping for the player's character by setting the Enum.HumanoidStateType.Jumping state to false and connects the JumpRequest event for handling custom behavior upon jump request.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
local processJumpRequest = false
local COOLDOWN_TIME = 0.5
local function jumpRequest()
if processJumpRequest == false then
processJumpRequest = true
-- Process custom jump request
print("Jump requested!")
-- Reset debounce variable after cooldown
task.wait(COOLDOWN_TIME)
processJumpRequest = false
end
end
UserInputService.JumpRequest:Connect(jumpRequest)
LastInputTypeChanged
This event fires whenever the client's Enum.UserInputType is changed.
To get the value of the last input type, regardless of whether it has changed, use the GetLastInputType() method.
Parameters
A Enum.UserInputType indicating the last input type.
PointerAction
This event fires when the user performs a specific pointer action (wheel, pan, pitch).
Parameters
Indicates whether the engine internally observed this input and acted on it.
TextBoxFocusReleased
This event fires when the client loses focus on a TextBox, typically when a user stops text entry by pressing Enter or clicking/touching elsewhere on the screen. Can be used alongside TextBoxFocused to track when a TextBox gains focus.
See also GetFocusedTextBox(), TextBox.Focused, and TextBox.FocusLost.
Parameters
Code Samples
This example adjusts the GuiObject.Transparency of TextBox objects when they gain/lose focus.
local UserInputService = game:GetService("UserInputService")
local function textBoxFocused(textBox)
textBox.BackgroundTransparency = 0
end
local function textBoxFocusReleased(textBox)
textBox.BackgroundTransparency = 0.5
end
UserInputService.TextBoxFocused:Connect(textBoxFocused)
UserInputService.TextBoxFocusReleased:Connect(textBoxFocusReleased)
TextBoxFocused
This event fires when the client gains focus on a TextBox, typically when a user clicks/taps it to begin inputting text. Also fires if the TextBox is focused using TextBox:CaptureFocus(). Can be used alongside TextBoxFocusReleased to track when a TextBox loses focus.
See also GetFocusedTextBox(), TextBox.Focused, and TextBox.FocusLost.
Parameters
Code Samples
This example adjusts the GuiObject.Transparency of TextBox objects when they gain/lose focus.
local UserInputService = game:GetService("UserInputService")
local function textBoxFocused(textBox)
textBox.BackgroundTransparency = 0
end
local function textBoxFocusReleased(textBox)
textBox.BackgroundTransparency = 0.5
end
UserInputService.TextBoxFocused:Connect(textBoxFocused)
UserInputService.TextBoxFocusReleased:Connect(textBoxFocusReleased)
TouchDrag
This event fires when the user drags on the screen of a TouchEnabled device.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
Currently only supports one touch for a value of 1.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchEnded
This event fires when a user releases their finger from the screen of a TouchEnabled device. Can be paired with TouchStarted to determine when a user starts and stops touching the screen.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An InputObject instance containing information about the user's input. This is the same object throughout the lifetime of the touch, so comparing InputObjects when they are touch objects is valid to determine if it's the same finger.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchLongPress
This event fires when a user holds at least one finger for a short amount of time on the screen of a TouchEnabled device.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An array of Vector2 objects indicating the position of the fingers involved in the gesture.
The Enum.UserInputState of the gesture.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchMoved
This event fires when a user moves their finger on the screen of a TouchEnabled device, useful for tracking whether a user is moving their finger on the screen and where they're moving it. Can be paired with TouchStarted and TouchEnded to determine when a user starts touching the screen, how their finger moves while touching it, and when the they stop touching the screen.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An InputObject instance containing information about the user's input. Note that its Position is a Vector3 but only includes X and Y coordinates (Z is always 0).
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchPan
This event fires when the user drags at least one finger on the screen of a TouchEnabled device.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An array of Vector2s indicating the positions of the touches involved in the gesture.
The size of the pan gesture from start to end, in pixels.
The speed of the pan gesture in pixels per second.
The Enum.UserInputState of the gesture.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchPinch
This event fires when a user performs a pinch gesture on the screen of a TouchEnabled device.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An array of Vector2s indicating the screen position, in pixels, of the fingers involved in the pinch gesture.
The magnitude of the pinch from start to finish (in pixels) divided by the starting pinch positions.
The speed of the pinch gesture in pixels per second.
The Enum.UserInputState of the gesture.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchRotate
This event fires when a user rotates two fingers on the screen of a TouchEnabled device.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An array of Vector2s indicating the positions of the fingers involved in the gesture.
The number of degree the gesture has rotated since the start of the gesture.
The change in rotation (in degrees) divided by the duration of the change (in seconds).
The Enum.UserInputState of the gesture.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchStarted
This event fires when a user places their finger on the screen of a TouchEnabled device. Can be paired with TouchEnded to determine when a user starts and stops touching the screen.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An InputObject instance, which contains information about the user's input. This is the same object throughout the lifetime of the touch, so comparing InputObjects when they are touch objects is valid to determine if it's the same finger.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchSwipe
This event fires on a TouchEnabled device when a user places their finger(s) down on the screen, pans across the screen, and lifts their finger(s) off with a certain speed of movement.
For more precise tracking of touch input movement, use TouchMoved.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An Enum.SwipeDirection indicating the direction the user swiped.
Number of touches involved in the swipe gesture.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchTap
This event fires when a user taps their finger on the screen of a TouchEnabled device, regardless of whether the user taps in the 3D world or on a GuiObject element. If you're looking for an event that only fires when the user taps in the 3D world, use TouchTapInWorld.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
An array of Vector2 objects indicating the position of the fingers involved in the tap gesture.
Indicates whether the engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent will be true.
TouchTapInWorld
This event fires when a user taps their finger on the screen of a TouchEnabled device and the tap location is in the 3D world rather than on a GuiObject element.
Note that this event only fires when the Roblox client window is in focus. It will not fire when the window is minimized.
Parameters
Whether the user tapped a UI element.
WindowFocusReleased
This event fires when the window of the Roblox client loses focus, typically when it is minimized by the user. Can be used alongside WindowFocused to track when the client gains focus on a user's screen.
Code Samples
The purpose of this code sample is to fire a server-side RemoteEvent to indicate when the player is "away" based on their client window being minimized. When mimimized, the server creates a ForceField around the player's character to shield them from damage.
The code below should be placed in a LocalScript within StarterPlayerScripts with the intention of running it in conjunction with the server-side Script that follows it.
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local awayEvent = ReplicatedStorage:WaitForChild("AwayEvent")
local function focusGained()
awayEvent:FireServer(false)
end
local function focusReleased()
awayEvent:FireServer(true)
end
UserInputService.WindowFocused:Connect(focusGained)
UserInputService.WindowFocusReleased:Connect(focusReleased)
The following Script, placed within ServerScriptService, completes the RemoteEvent connection triggered by the LocalScript above.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local awayEvent = Instance.new("RemoteEvent")
awayEvent.Name = "AwayEvent"
awayEvent.Parent = ReplicatedStorage
local function manageForceField(player, away)
if away then
local forceField = Instance.new("ForceField")
forceField.Parent = player.Character
else
local forceField = player.Character:FindFirstChildOfClass("ForceField")
if forceField then
forceField:Destroy()
end
end
end
awayEvent.OnServerEvent:Connect(manageForceField)
WindowFocused
This event fires when the window of the Roblox client gains focus, typically when it is maximized or actively opened by the user. Can be used alongside WindowFocusReleased to track when the client loses focus on a user's screen.