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

UserInputState

InputObject

UserInputState

Contains a Enum/UserInputState enum that describes the state of the user’s input and indicates how a user’s input is currently interacting with your game.

Enums

Name Value Description

Begin

0

When an InputObject starts to interact with the game. For example, a mouse button down, or a key down, or when a touch begins touching the screen.

Change

1

When an InputObjecthas already begun interacting with the game, and part of it’s state is changing. For example, a mouse position move, or a thumbstick moving, or when a touch begins to move across the screen.

End

2

When an InputObject finishes interacting with the game. For example, a mouse button up, or a key up, or when a touch stops touching the screen.

Cancel

3

A special circumstance state that indicates this input is now being used for some other function. For example, binding two actions to the same input will cause a function to fire with Cancel.

None

4

A state that should never be seen in a game, essentially just marks the end of the enum.

See also

  • InputObject/Delta
  • InputObject/KeyCode
  • InputObject/Position
  • InputObject/UserInputType


Code Samples


Create a Custom CameraScript

By default, Roblox relies on a LocalScript, described [here][1], to control the user’s camera. However, this script can be overridden with a custom CameraScript. The example below demonstrates how to create a custom script to control the user’s camera using many of the UserInputService events.

The script is broken into two parts:

  1. Mobile camera events, which rely on touch events
  2. Non-mobile camera events, which rely on keyboard input and tracking the user’s movement

First, the camera script needs utility functions to setup the camera and set its Camera/CameraType|CameraType to Scriptable so that the script can control the camera. It also needs a function to update the camera when it moves, rotates, and zooms.

Using touch events allows us to track user input as they interact with the touchscreen on their mobile device. These events allow us to handle camera movement, rotation, and zoom.

The second half of the code sample adds camera support for players on desktop devices. When input begans, the function Input() checks that the state of the input is Enum/UserInputState|Enum.UserInputState.Begin to ignore all keypress inputs other than when the user first presses a key down. When the user presses I and O the camera zooms in and out. When the presses down and moves their left mouse button, the script Enum/MouseBehavior|locks the player’s mouse by changing the UserInputService/MouseBehavior property. The camera rotates according to the mouse’s UserInputService/GetMouseDelta|change in screen position. When the player moves their character, the camera moves with them.

All of the parts discussed above are combined and shown in the code sample below.


Binding Supported Gamepad KeyCodes

This example gets a list of navigation gamepads and a list of their supported Enum/KeyCodes. Then, it iterates through the supported KeyCode list and binds the ButtonX and X keys to functions if they are supported by a gamepad using the ContextActionService/BindAction|ContextActionService.

Check out this article to learn more about adding support for gamepad input into your game.