UserInputServiceクラス

クラスサービス(game:GetService("UserInputService")で取り出す)Instance.newでは作れない日本語解説あり

ひとことで言うと

キーボード・マウス・タッチ・ゲームパッドの入力を調べるサービスです(LocalScriptで使います)。

公式の説明(英語):UserInputService is primarily used to detect the input types available on a user's device, as well as detect input events.

使用例

キーボードのEキーを押したことを調べる
LocalScript置く場所:StarterPlayerScripts
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return
	end
	if input.KeyCode == Enum.KeyCode.E then
		print("Eキーが押された")
	end
end)

スクリプトは、Luauの型チェック(luau-lsp)で確認しています。

クラスの関係

受け継いでいるクラス:Object → Instance → UserInputService

プロパティ(15)

名前と書き方説明
AccelerometerEnabled
UserInputService.AccelerometerEnabled: boolean
Describes whether the user's device has an accelerometer.
GamepadEnabled
UserInputService.GamepadEnabled: boolean
Describes whether the user's device has an available gamepad.
GyroscopeEnabled
UserInputService.GyroscopeEnabled: boolean
Describes whether the user's device has a gyroscope.
KeyboardEnabled
UserInputService.KeyboardEnabled: boolean
Describes whether the user's device has a keyboard available.
MouseBehavior
UserInputService.MouseBehavior: MouseBehavior
Determines whether the user's mouse can be moved freely or is locked.
MouseDeltaSensitivity
UserInputService.MouseDeltaSensitivity: float
Scales the delta (change) output of the user's Mouse.
MouseEnabled
UserInputService.MouseEnabled: boolean
Describes whether the user's device has a mouse available.
MouseIcon
UserInputService.MouseIcon: ContentId
The content ID of the image for the user's mouse icon.
MouseIconContent
UserInputService.MouseIconContent: Content
The content ID of the image for the user's mouse icon. Only supports asset URIs.
MouseIconEnabled
UserInputService.MouseIconEnabled: boolean
Determines whether the mouse icon is visible.
OnScreenKeyboardPosition
UserInputService.OnScreenKeyboardPosition: Vector2
Determines the position of the on-screen keyboard.
OnScreenKeyboardSize
UserInputService.OnScreenKeyboardSize: Vector2
Determines the size of the on-screen keyboard.
OnScreenKeyboardVisible
UserInputService.OnScreenKeyboardVisible: boolean
Describes whether an on-screen keyboard is currently visible on the user's screen.
PreferredInput
UserInputService.PreferredInput: PreferredInput
Queries the primary input type a player is using, based on anticipated user behavior.
TouchEnabled
UserInputService.TouchEnabled: boolean
タッチ操作ができる端末かどうか。Describes whether the user's device has a touch screen available.

メソッド(24)

名前と書き方説明
CreateVirtualInput
UserInputService:CreateVirtualInput(target: Instance): Object
Creates a VirtualInput object for simulating mouse, keyboard, and pointer input.
GamepadSupports
UserInputService:GamepadSupports(gamepadNum: UserInputType, gamepadKeyCode: KeyCode): boolean
Returns whether the given UserInputType gamepad supports a button corresponding with the given KeyCode.
GetConnectedGamepads
UserInputService:GetConnectedGamepads(): Array
Returns an array of UserInputType gamepads currently connected.
GetDeviceAcceleration
UserInputService:GetDeviceAcceleration(): InputObject
Returns an InputObject that describes the device's current acceleration.
GetDeviceGravity
UserInputService:GetDeviceGravity(): InputObject
Returns an InputObject describing the device's current gravity vector.
GetDeviceRotation
UserInputService:GetDeviceRotation(): Tuple
Returns an InputObject and a CFrame describing the device's current rotation vector.
GetFocusedTextBox
UserInputService:GetFocusedTextBox(): TextBox
Returns the TextBox the client is currently focused on.
GetGamepadConnected
UserInputService:GetGamepadConnected(gamepadNum: UserInputType): boolean
Returns whether a gamepad with the given UserInputType is connected.
GetGamepadState
UserInputService:GetGamepadState(gamepadNum: UserInputType): List<InputObject>
Returns an array of Class.InputObject|InputObjects for all available inputs on the given gamepad, representing each input's last input state.
GetImageForKeyCode
UserInputService:GetImageForKeyCode(keyCode: KeyCode): ContentId
Returns an image for the requested KeyCode.
GetKeysPressed
UserInputService:GetKeysPressed(): List<InputObject>
Returns an array of Class.InputObject|InputObjects associated with the Enum.KeyCode|keys currently being pressed down.
GetLastInputType
UserInputService:GetLastInputType(): UserInputType
Returns the UserInputType associated with the user's most recent input.
GetMouseButtonsPressed
UserInputService:GetMouseButtonsPressed(): List<InputObject>
Returns an array of Class.InputObject|InputObjects associated with the mouse buttons currently being held down.
GetMouseDelta
UserInputService:GetMouseDelta(): Vector2
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.
GetMouseLocation
UserInputService:GetMouseLocation(): Vector2
Returns the current screen location of the player's Mouse relative to the top-left corner of the screen.
GetNavigationGamepads
UserInputService:GetNavigationGamepads(): Array
Returns an array of gamepads connected and enabled for GuiObject navigation in descending order of priority.
GetStringForKeyCode
UserInputService:GetStringForKeyCode(keyCode: KeyCode, format: KeyCodeStringFormat): string
Returns a string representing a key the user should press in order to input a given KeyCode, optionally in an abbreviated format.
GetSupportedGamepadKeyCodes
UserInputService:GetSupportedGamepadKeyCodes(gamepadNum: UserInputType): Array
Returns an array of Enum.KeyCode|KeyCodes that the gamepad associated with the given UserInputType supports.
IsGamepadButtonDown
UserInputService:IsGamepadButtonDown(gamepadNum: UserInputType, gamepadKeyCode: KeyCode): boolean
Determines whether a particular button is pressed on a gamepad.
IsKeyDown
UserInputService:IsKeyDown(keyCode: KeyCode): boolean
指定したキーが押されているかを調べます。Returns whether the given Enum.KeyCode|key is currently held down.
IsMouseButtonPressed
UserInputService:IsMouseButtonPressed(mouseButton: UserInputType): boolean
Returns whether the given mouse button is currently held down.
IsNavigationGamepad
UserInputService:IsNavigationGamepad(gamepadEnum: UserInputType): boolean
Returns true if the specified gamepad is allowed to control navigation and selection Class.GuiObject|GuiObjects.
RecenterUserHeadCFrame
UserInputService:RecenterUserHeadCFrame(): ()
Recenters the CFrame of the VR headset to the current orientation of the headset worn by the user.
SetNavigationGamepad
UserInputService:SetNavigationGamepad(gamepadEnum: UserInputType, enabled: boolean): ()
Sets whether or not the specified gamepad can move the GuiObject navigator.

イベント(26)

名前と書き方説明
DeviceAccelerationChanged
UserInputService.DeviceAccelerationChanged(acceleration: InputObject)
Fires when a user moves a device that has an accelerometer.
DeviceGravityChanged
UserInputService.DeviceGravityChanged(gravity: InputObject)
Fires when the force of gravity changes on a device that has an enabled accelerometer.
DeviceRotationChanged
UserInputService.DeviceRotationChanged(rotation: InputObject, cframe: CFrame)
Fires when a user rotates a device that has a gyroscope.
GamepadConnected
UserInputService.GamepadConnected(gamepadNum: UserInputType)
Fires when a gamepad is connected to the client.
GamepadDisconnected
UserInputService.GamepadDisconnected(gamepadNum: UserInputType)
Fires when a gamepad is disconnected from the client.
InputBegan
UserInputService.InputBegan(input: InputObject, gameProcessedEvent: boolean)
キーを押す・クリックするなど、入力が始まったときに発生するイベントです。Fires when a user begins interacting with an input device such as a mouse or gamepad.
InputChanged
UserInputService.InputChanged(input: InputObject, gameProcessedEvent: boolean)
Fires when a user changes how they're interacting with an input device such as a mouse or gamepad.
InputEnded
UserInputService.InputEnded(input: InputObject, gameProcessedEvent: boolean)
入力が終わった(キーを離した)ときに発生するイベントです。Fires when a user stops interacting with an input device such as a mouse or gamepad.
JumpRequest
UserInputService.JumpRequest()
Fires whenever the client makes a request for their character to jump.
LastInputTypeChanged
UserInputService.LastInputTypeChanged(lastInputType: UserInputType)
Fires whenever the client's UserInputType is changed.
PointerAction
UserInputService.PointerAction(wheel: float, pan: Vector2, pinch: float, gameProcessedEvent: boolean)
Fires when the user performs a specific pointer action.
TextBoxFocused
UserInputService.TextBoxFocused(textboxFocused: TextBox)
Fires when the client focuses on a TextBox.
TextBoxFocusReleased
UserInputService.TextBoxFocusReleased(textboxReleased: TextBox)
Fires when the client loses focus on a TextBox.
TouchDrag
UserInputService.TouchDrag(dragDirection: SwipeDirection, numberOfTouches: int, gameProcessedEvent: boolean)
Fires when the user drags on the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device.
TouchEnded
UserInputService.TouchEnded(touch: InputObject, gameProcessedEvent: boolean)
Fires when a user releases their finger from the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device.
TouchLongPress
UserInputService.TouchLongPress(touchPositions: Array, state: UserInputState, gameProcessedEvent: boolean)
Fires when a user holds at least one finger for a short amount of time on the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device.
TouchMoved
UserInputService.TouchMoved(touch: InputObject, gameProcessedEvent: boolean)
Fires when a user moves their finger on the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device.
TouchPan
UserInputService.TouchPan(touchPositions: Array, totalTranslation: Vector2, velocity: Vector2, state: UserInputState, gameProcessedEvent: boolean)
Fires when the user drags at least one finger on the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device.
TouchPinch
UserInputService.TouchPinch(touchPositions: Array, scale: float, velocity: float, state: UserInputState, gameProcessedEvent: boolean)
Fires when a user performs a pinch gesture on the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device.
TouchRotate
UserInputService.TouchRotate(touchPositions: Array, rotation: float, velocity: float, state: UserInputState, gameProcessedEvent: boolean)
Fires when a user rotates two fingers on the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device.
TouchStarted
UserInputService.TouchStarted(touch: InputObject, gameProcessedEvent: boolean)
Fires when a user places their finger on the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device.
TouchSwipe
UserInputService.TouchSwipe(swipeDirection: SwipeDirection, numberOfTouches: int, gameProcessedEvent: boolean)
Fires on a Class.UserInputService.TouchEnabled|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.
TouchTap
UserInputService.TouchTap(touchPositions: Array, gameProcessedEvent: boolean)
Fires when a user taps their finger on the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device.
TouchTapInWorld
UserInputService.TouchTapInWorld(position: Vector2, processedByUI: boolean)
Fires when a user taps their finger on the screen of a Class.UserInputService.TouchEnabled|TouchEnabled device and the tap location is in the 3D world.
WindowFocused
UserInputService.WindowFocused()
Fires when the window of the Roblox client gains focus on the user's screen.
WindowFocusReleased
UserInputService.WindowFocusReleased()
Fires when the window of the Roblox client loses focus on the user's screen.

受け継いでいるメンバー

Instance から受け継いでいるもの(46)

Archivable・Capabilities・IsInSandbox・Name・Parent・PredictionMode・Sandboxed・AddTag・ClearAllChildren・Clone・Destroy・FindFirstAncestor・FindFirstAncestorOfClass・FindFirstAncestorWhichIsA・FindFirstChild・FindFirstChildOfClass・FindFirstChildWhichIsA・FindFirstDescendant・GetActor・GetAttribute・GetAttributeChangedSignal・GetAttributes・GetChildren・GetDebugId・GetDescendants・GetFullName・GetStyled・GetStyledPropertyChangedSignal・GetTags・HasTag・IsAncestorOf・IsDescendantOf・IsPropertyModified・QueryDescendants・RemoveTag・ResetPropertyToDefault・SetAttribute・WaitForChild・AncestryChanged・AttributeChanged・ChildAdded・ChildRemoved・DescendantAdded・DescendantRemoving・Destroying・StyledPropertiesChanged

Object から受け継いでいるもの(4)

ClassName・GetPropertyChangedSignal・IsA・Changed

UserInputServiceを使う記事

英語の説明とメンバーの一覧は、Roblox Creator Documentation(UserInputService)をもとにしています(CC BY 4.0)。日本語の解説は、このサイトで独自に書いたものです。

執筆:鈴木孝昌官公庁のWeb・システム開発を担当する現役プロジェクトマネージャー。沖縄マイクラ部 クロスウェーブで、ロブロックス(Roblox)・マインクラフト(マイクラ)・Scratchなどのプログラミングを指導。マイクラカップ沖縄代表チームを指導(第6回・第7回、第7回TBS賞)。
沖縄マイクラ部で、子どもたちにロブロックス(Roblox)やマインクラフト(マイクラ)のプログラミングを教えています。プロフィールと編集方針
Instagramで教室の様子を見る
noteで記事を読む