UserInputService

非推奨を表示

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

作成できません
サービス
複製されていません

UserInputService は、ユーザーのデバイスに利用可能なさまざまな種類の入力を検出し、キャプチャするサービスです。

このサービスの主な目的は、ゲームパッド、タッチスクリーン、キーボードなどの利用可能な入力の多様性に対応するためです。 LocalScript は、デバイスに応じて異なるアクションを実行し、最終ユーザーに最高のエクスペリエンスを提供できるようにします。

このサービスの一部として、ユーザーがGUI、ツール、その他のゲームインスタンスとインタラクトするときにユーザーの入力を検出する機能が含まれます。ユーザーの入力を検出するには、サービスは UserInputService.TouchStarted 、または

このサービスはクライアント側のみにのみ機能し、 LocalScript または ModuleScript によって必要になる LocalScript のみを使用している場合にのみ機能します。ユーザー入力サービスはクライアント側のみにのみユーザーの入力を検出でき、 2>Class.LocalScript</

また、ContextActionService を参照してください。これは、複数のユーザーの入力に機能をバインドできるサービスです。

コードサンプル

UserInputService

-- We must get the UserInputService before we can use it
local UserInputService = game:GetService("UserInputService")
-- A sample function providing one usage of InputBegan
local function onInputBegan(input, _gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed!")
end
end
UserInputService.InputBegan:Connect(onInputBegan)

概要

プロパティ

  • 読み取り専用
    複製されていません
    並列読み取り

    ユーザーのデバイスに加速度センサーがあるかどうかを記述します。

  • 読み取り専用
    複製されていません
    並列読み取り

    ユーザーが使用しているデバイスにゲームパッドが使用可能かどうかを記述します。

  • 読み取り専用
    複製されていません
    並列読み取り

    ユーザーのデバイスにギロスコープがあるかどうかを記述します。

  • 読み取り専用
    複製されていません
    並列読み取り

    ユーザーのデバイスにキーボードが含まれているかどうかを記述します。

  • ユーザーのマウスが自由に移動できるか、ロックされているかを決定します。

  • 複製されていません
    並列読み取り

    ユーザーの Mouse のデルタ (変更) 出力をスケールします。

  • 読み取り専用
    複製されていません
    並列読み取り

    ユーザーのデバイスにマウスが含まれているかどうかを記述します。

  • MouseIcon:ContentId
    並列読み取り

    ユーザーマウスアイコンとして使用される画像のコンテンツID。

  • 並列読み取り

    Class.Mouse アイコンが表示されるかどうかを決定します。

  • 読み取り専用
    複製されていません
    並列読み取り

    画面キーボードの位置を決定します。

  • 読み取り専用
    複製されていません
    並列読み取り

    画面キーボードのサイズを決定します。

  • 読み取り専用
    複製されていません
    並列読み取り

    現在、ユーザーの画面に表示されているオンスクリーンキーボードが表示されているかどうかを記述します。

  • 読み取り専用
    複製されていません
    並列読み取り

    ユーザーの現在のデバイスにタッチスクリーンが含まれているかどうかを記述します。

  • 読み取り専用
    複製されていません
    並列読み取り

    ユーザーがバーチャルリアリティヘッドセットを使用しているかどうかを示します。

方法

イベント

プロパティ

AccelerometerEnabled

読み取り専用
複製されていません
並列読み取り

このプロパティは、ユーザーのデバイスに加速度センサーがあるかどうかを記述します

加速度センサーは、加速度 (速度の変化) を測定するために見つけられるコンポーネントです。

たとえば、次のコードスナップでは、ユーザーのデバイスが加速度センサーを持っているかどうかをチェックする方法を示しています。


local UserInputService = game:GetService("UserInputService")
local accelerometerEnabled = UserInputService.AccelerometerEnabled
if accelerometerEnabled then
print("Accelerometer enabled!")
else
print("Accelerometer not enabled!")
end

デバイスに加速度センサーが有効になっている場合は、UserInputService:GetDeviceAcceleration() 機能またはデバイスの加速度センサー変更を追跡することで、現在の加速度を取得できます。

Class.UserInputService はクライアント側のみであるため、このプロパティは LocalScript でのみ使用できます。

コードサンプル

Move a Ball using the 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
Create a Gyroscopic Camera

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local camera = workspace.CurrentCamera
local currentRotation = camera.CFrame -- CFrame.new(Vector3.new(0,0,0), Vector3.new(0,0,0))
local lastInputFrame = nil
local upsideDown = false
task.wait()
local orientationSet = false
local function GravityChanged(gravity)
if not orientationSet then
upsideDown = (gravity.Position.X < -0.5 or gravity.Position.Z > 0.5)
orientationSet = true
end
end
local function RotationChanged(_rotation, rotCFrame)
if orientationSet then
if not lastInputFrame then
lastInputFrame = rotCFrame
end
local delta = rotCFrame * lastInputFrame:inverse()
local x, y, z = delta:ToEulerAnglesXYZ()
if upsideDown then
delta = CFrame.Angles(-x, y, z)
else
delta = CFrame.Angles(x, -y, z)
end
currentRotation = currentRotation * delta
lastInputFrame = rotCFrame
end
end
local function HideCharacter()
for _, limb in pairs(character:GetChildren()) do
if limb:IsA("Part") then
limb.Transparency = 1
end
end
end
if UserInputService.GyroscopeEnabled then
UserInputService.DeviceGravityChanged:Connect(GravityChanged)
UserInputService.DeviceRotationChanged:Connect(RotationChanged)
HideCharacter()
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, function()
camera.CFrame = CFrame.new(head.Position - Vector3.new(0, 8, 10)) * currentRotation
camera.Focus = CFrame.new(currentRotation * Vector3.new(0, 0, -10))
end)
end

GamepadEnabled

読み取り専用
複製されていません
並列読み取り

このプロパティは、ユーザーが使用しているデバイスが利用可能なゲームパッドを持っているかどうかを記述します。ゲームパッドが利用可能である場合は、UserInputService:GetConnectedGamepads() を使用して、接続されたゲームパッドのリストを取得できます。

Class.UserInputService はクライアント側のみであるため、このプロパティは LocalScript でのみ使用できます。

参照してください:

コードサンプル

How to Set the Active Gamepad for Input

local UserInputService = game:GetService("UserInputService")
local function getActiveGamepad()
local activeGamepad = nil
local connectedGamepads = UserInputService:GetConnectedGamepads()
if #connectedGamepads > 0 then
for _, gamepad in connectedGamepads do
if activeGamepad == nil or gamepad.Value < activeGamepad.Value then
activeGamepad = gamepad
end
end
end
if activeGamepad == nil then -- Nothing is connected; set up for "Gamepad1"
activeGamepad = Enum.UserInputType.Gamepad1
end
return activeGamepad
end
if UserInputService.GamepadEnabled then
local activeGamepad = getActiveGamepad()
print(activeGamepad)
end

GyroscopeEnabled

読み取り専用
複製されていません
並列読み取り

このプロパティは、ユーザーのデバイスにジャイロスコープがあるかどうかを記述します。

ジロスコープは、オリエンテーションと回転速度を検出するほとんどのモバイルデバイスにあるコンポーネントです。

ユーザーのデバイスにジャイロスコープがある場合は、UserInputService:GetDeviceRotation() 機能と UserInputService.DeviceRotationChanged イベントを使用して、ゲームに組み込むことができます。


local UserInputService = game:GetService("UserInputService")
local gyroIsEnabled = UserInputService.GyroscopeEnabled
if gyroIsEnabled then
print("Gyroscope is enabled!")
else
print("Gyroscope is not enabled!")
end

Class.UserInputService はクライアント側のみであるため、このプロパティは LocalScript でのみ使用できます。

コードサンプル

Create a Gyroscopic Camera

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local camera = workspace.CurrentCamera
local currentRotation = camera.CFrame -- CFrame.new(Vector3.new(0,0,0), Vector3.new(0,0,0))
local lastInputFrame = nil
local upsideDown = false
task.wait()
local orientationSet = false
local function GravityChanged(gravity)
if not orientationSet then
upsideDown = (gravity.Position.X < -0.5 or gravity.Position.Z > 0.5)
orientationSet = true
end
end
local function RotationChanged(_rotation, rotCFrame)
if orientationSet then
if not lastInputFrame then
lastInputFrame = rotCFrame
end
local delta = rotCFrame * lastInputFrame:inverse()
local x, y, z = delta:ToEulerAnglesXYZ()
if upsideDown then
delta = CFrame.Angles(-x, y, z)
else
delta = CFrame.Angles(x, -y, z)
end
currentRotation = currentRotation * delta
lastInputFrame = rotCFrame
end
end
local function HideCharacter()
for _, limb in pairs(character:GetChildren()) do
if limb:IsA("Part") then
limb.Transparency = 1
end
end
end
if UserInputService.GyroscopeEnabled then
UserInputService.DeviceGravityChanged:Connect(GravityChanged)
UserInputService.DeviceRotationChanged:Connect(RotationChanged)
HideCharacter()
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, function()
camera.CFrame = CFrame.new(head.Position - Vector3.new(0, 8, 10)) * currentRotation
camera.Focus = CFrame.new(currentRotation * Vector3.new(0, 0, -10))
end)
end

KeyboardEnabled

読み取り専用
複製されていません
並列読み取り

このプロパティは、ユーザーのデバイスにキーボードが利用可能かどうかを記述します。このプロパティは、ユーザーのデバイスにキーボードが利用可能な場合、または利用可能でない場合に true です。

ユーザーが利用可能なキーボードを持っているかどうかを判断するのに使用できます - これは、UserInputService:IsKeyDown() またはUserInputService:GetKeysPressed() をチェックするために使用することが重要です。

Class.UserInputService はクライアント側のみであるため、このプロパティは LocalScript でのみ使用できます。

コードサンプル

Check if Keyboard is Enabled

local UserInputService = game:GetService("UserInputService")
if UserInputService.KeyboardEnabled then
print("The user's device has an available keyboard!")
else
print("The user's device does not have an available keyboard!")
end

MouseBehavior

並列読み取り

このプロパティは、ユーザーのマウスの動作を Enum.MouseBehavior 枚に基づいて設定します。デフォルトの値は、Enum.MouseBehavior.Default です。

3つの値に設定できます:

  1. デフォルト : マウスはユーザーの画面を自由に移動します。
  2. ロックセンター : マウスはロックされており、ユーザーの画面の中心から移動できません。
  3. LockCurrentPosition : マウスはロックされており、移動できません。その時点のユーザーの画面上の現在の位置にロックされています。

このプロパティの値は、イベントトラッキングマウスの動きの感度に影響しません。たとえば、 GetMouseDelta は、マウスがロックされているかどうかにかかわらず、ピクセル単位の Vector2 の画面位置を返します。その結果

このプロパティは、GuiButtonModal が有効になっている場合、またはプレイヤーの右マウスボタンが下にある場合、GuiButton.Visible をオーバーライドします。

注意、マウスがロックされている場合、UserInputService.InputChanged は、プレイヤーがマウスを動かすときにまだ発動し、Class.UserInputService.InputLoaded によってマウスが移動した方向にパスします。さらに、プレイヤーがゲームから追放された場合、マウスは強制的にアンロックされます。

Class.UserInputService はクライアント側のみであるため、このプロパティは LocalScript でのみ使用できます。

コードサンプル

Create a Binoculars Script

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)
local mouse = player:GetMouse()
local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties = {
FieldOfView = nil,
_CFrame = nil,
MouseBehavior = nil,
MouseDeltaSensitivity = nil,
}
local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0
-- Reset camera back to CFrame and FieldOfView before zoom
local function ResetCamera()
target = nil
camera.CameraType = Enum.CameraType.Custom
camera.CFrame = originalProperties._CFrame
camera.FieldOfView = originalProperties.FieldOfView
UserInputService.MouseBehavior = originalProperties.MouseBehavior
UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end
local function ZoomCamera()
-- Allow camera to be changed by script
camera.CameraType = Enum.CameraType.Scriptable
-- Store camera properties before zoom
originalProperties._CFrame = camera.CFrame
originalProperties.FieldOfView = camera.FieldOfView
originalProperties.MouseBehavior = UserInputService.MouseBehavior
originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity
-- Zoom camera
target = mouse.Hit.Position
local eyesight = head.Position
camera.CFrame = CFrame.new(eyesight, target)
camera.Focus = CFrame.new(target)
camera.FieldOfView = 10
-- Lock and slow down mouse
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseDeltaSensitivity = 1
-- Reset zoom angles
AngleX, TargetAngleX = 0, 0
AngleY, TargetAngleY = 0, 0
end
-- Toggle camera zoom/unzoom
local function MouseClick()
if zoomed then
-- Unzoom camera
ResetCamera()
else
-- Zoom in camera
ZoomCamera()
end
zoomed = not zoomed
end
local function MouseMoved(input)
if zoomed then
local sensitivity = 0.6 -- anything higher would make looking up and down harder; recommend anything between 0~1
local smoothness = 0.05 -- recommend anything between 0~1
local delta = Vector2.new(input.Delta.x / sensitivity, input.Delta.y / sensitivity) * smoothness
local X = TargetAngleX - delta.y
local Y = TargetAngleY - delta.x
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y
AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
AngleY = AngleY + (TargetAngleY - AngleY) * 0.15
camera.CFrame = CFrame.new(head.Position, target)
* CFrame.Angles(0, math.rad(AngleY), 0)
* CFrame.Angles(math.rad(AngleX), 0, 0)
end
end
local function InputBegan(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
MouseClick()
end
end
local function InputChanged(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseMovement then
MouseMoved(input)
end
end
if UserInputService.MouseEnabled then
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputChanged:Connect(InputChanged)
end
Create a Custom CameraScript

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local torso = character:WaitForChild("HumanoidRootPart")
local playerPosition = torso.Position
local default_CameraPosition = torso.Position
local default_CameraRotation = Vector2.new(0, math.rad(-60))
local default_CameraZoom = 15
local cameraPosition = default_CameraPosition
local cameraRotation = default_CameraRotation
local cameraZoom = default_CameraZoom
local cameraZoomBounds = nil -- {10,200}
local cameraRotateSpeed = 10
local cameraMouseRotateSpeed = 0.25
local cameraTouchRotateSpeed = 10
local function SetCameraMode()
camera.CameraType = "Scriptable"
camera.FieldOfView = 80
camera.CameraSubject = nil
end
local function UpdateCamera()
SetCameraMode()
local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0) * CFrame.Angles(cameraRotation.Y, 0, 0)
camera.CFrame = cameraRotationCFrame + cameraPosition + cameraRotationCFrame * Vector3.new(0, 0, cameraZoom)
camera.Focus = camera.CFrame - Vector3.new(0, camera.CFrame.p.Y, 0)
end
local lastTouchTranslation = nil
local function TouchMove(_touchPositions, totalTranslation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = totalTranslation - lastTouchTranslation
cameraPosition = cameraPosition + Vector3.new(difference.X, 0, difference.Y)
UpdateCamera()
end
lastTouchTranslation = totalTranslation
end
local lastTouchRotation = nil
local function TouchRotate(_touchPositions, rotation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = rotation - lastTouchRotation
cameraRotation = cameraRotation
+ Vector2.new(-difference, 0) * math.rad(cameraTouchRotateSpeed * cameraRotateSpeed)
UpdateCamera()
end
lastTouchRotation = rotation
end
local lastTouchScale = nil
local function TouchZoom(_touchPositions, scale, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = scale - lastTouchScale
cameraZoom = cameraZoom * (1 + difference)
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
lastTouchScale = scale
end
local function Input(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then
if inputObject.UserInputState == Enum.UserInputState.Begin then
-- (I) Zoom In
if inputObject.KeyCode == Enum.KeyCode.I then
cameraZoom = cameraZoom - 15
elseif inputObject.KeyCode == Enum.KeyCode.O then
cameraZoom = cameraZoom + 15
end
-- (O) Zoom Out
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
end
local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
if pressed then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
local rotation = UserInputService:GetMouseDelta()
cameraRotation = cameraRotation + rotation * math.rad(cameraMouseRotateSpeed)
else
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
end
local function PlayerChanged()
local movement = torso.Position - playerPosition
cameraPosition = cameraPosition + movement
playerPosition = torso.Position
UpdateCamera()
end
-- Determine whether the user is on a mobile device
if UserInputService.TouchEnabled then
-- The user is on a mobile device, use Touch events
UserInputService.TouchPan:Connect(TouchMove)
UserInputService.TouchRotate:Connect(TouchRotate)
UserInputService.TouchPinch:Connect(TouchZoom)
else
-- The user is not on a mobile device use Input events
UserInputService.InputBegan:Connect(Input)
UserInputService.InputChanged:Connect(Input)
UserInputService.InputEnded:Connect(Input)
-- Camera controlled by player movement
task.wait(2)
RunService:BindToRenderStep("PlayerChanged", Enum.RenderPriority.Camera.Value - 1, PlayerChanged)
end

MouseDeltaSensitivity

複製されていません
並列読み取り

このプロパティは、ユーザーの Mouse の感度を決定します。

感度は、物理マウスの移動をゲーム内のマウス移動にどの程度に変換するかを決定します。これは、GetMouseDelta 、マウスの移動などの感度を調整するために使用されることがあります。

このプロパティは、マウスアイコンの移動に影響しません。また、カメラ感度 設定は、クライアントの 設定 タブの マウスモーション トラッキングを調整することもありません。

このプロパティには最大 10 の値と最小 0 の値があります。低い値は低感度に対応し、高い値は高感度に対応します。

感度が 0 の場合、マウスの動きを追跡するイベントは引き続き発生しますが、変更マウスポジションを表すすべてのパラメータとプロパティは Vector2.new() または Datatype. Vector3|Vector

コードサンプル

Create a Binoculars Script

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)
local mouse = player:GetMouse()
local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties = {
FieldOfView = nil,
_CFrame = nil,
MouseBehavior = nil,
MouseDeltaSensitivity = nil,
}
local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0
-- Reset camera back to CFrame and FieldOfView before zoom
local function ResetCamera()
target = nil
camera.CameraType = Enum.CameraType.Custom
camera.CFrame = originalProperties._CFrame
camera.FieldOfView = originalProperties.FieldOfView
UserInputService.MouseBehavior = originalProperties.MouseBehavior
UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end
local function ZoomCamera()
-- Allow camera to be changed by script
camera.CameraType = Enum.CameraType.Scriptable
-- Store camera properties before zoom
originalProperties._CFrame = camera.CFrame
originalProperties.FieldOfView = camera.FieldOfView
originalProperties.MouseBehavior = UserInputService.MouseBehavior
originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity
-- Zoom camera
target = mouse.Hit.Position
local eyesight = head.Position
camera.CFrame = CFrame.new(eyesight, target)
camera.Focus = CFrame.new(target)
camera.FieldOfView = 10
-- Lock and slow down mouse
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseDeltaSensitivity = 1
-- Reset zoom angles
AngleX, TargetAngleX = 0, 0
AngleY, TargetAngleY = 0, 0
end
-- Toggle camera zoom/unzoom
local function MouseClick()
if zoomed then
-- Unzoom camera
ResetCamera()
else
-- Zoom in camera
ZoomCamera()
end
zoomed = not zoomed
end
local function MouseMoved(input)
if zoomed then
local sensitivity = 0.6 -- anything higher would make looking up and down harder; recommend anything between 0~1
local smoothness = 0.05 -- recommend anything between 0~1
local delta = Vector2.new(input.Delta.x / sensitivity, input.Delta.y / sensitivity) * smoothness
local X = TargetAngleX - delta.y
local Y = TargetAngleY - delta.x
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y
AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
AngleY = AngleY + (TargetAngleY - AngleY) * 0.15
camera.CFrame = CFrame.new(head.Position, target)
* CFrame.Angles(0, math.rad(AngleY), 0)
* CFrame.Angles(math.rad(AngleX), 0, 0)
end
end
local function InputBegan(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
MouseClick()
end
end
local function InputChanged(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseMovement then
MouseMoved(input)
end
end
if UserInputService.MouseEnabled then
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputChanged:Connect(InputChanged)
end

MouseEnabled

読み取り専用
複製されていません
並列読み取り

このプロパティは、ユーザーのデバイスにマウスが利用可能かどうかを記述します。このプロパティは、ユーザーのデバイスにマウスが利用可能な場合、または利用可能でない場合に true です。


local UserInputService = game:GetService("UserInputService")
if UserInputService.MouseEnabled then
print("The user's device has an available mouse!")
else
print("The user's device does not have an available mouse!")
end

Class.UserInputService マウス機能を使用する前に、これを確認することは重要です。UserInputService:GetMouseLocation() などの。

Class.UserInputService はクライアント側のみであるため、このプロパティは LocalScript でのみ使用できます。

参照してください:

コードサンプル

Create a Binoculars Script

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)
local mouse = player:GetMouse()
local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties = {
FieldOfView = nil,
_CFrame = nil,
MouseBehavior = nil,
MouseDeltaSensitivity = nil,
}
local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0
-- Reset camera back to CFrame and FieldOfView before zoom
local function ResetCamera()
target = nil
camera.CameraType = Enum.CameraType.Custom
camera.CFrame = originalProperties._CFrame
camera.FieldOfView = originalProperties.FieldOfView
UserInputService.MouseBehavior = originalProperties.MouseBehavior
UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end
local function ZoomCamera()
-- Allow camera to be changed by script
camera.CameraType = Enum.CameraType.Scriptable
-- Store camera properties before zoom
originalProperties._CFrame = camera.CFrame
originalProperties.FieldOfView = camera.FieldOfView
originalProperties.MouseBehavior = UserInputService.MouseBehavior
originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity
-- Zoom camera
target = mouse.Hit.Position
local eyesight = head.Position
camera.CFrame = CFrame.new(eyesight, target)
camera.Focus = CFrame.new(target)
camera.FieldOfView = 10
-- Lock and slow down mouse
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseDeltaSensitivity = 1
-- Reset zoom angles
AngleX, TargetAngleX = 0, 0
AngleY, TargetAngleY = 0, 0
end
-- Toggle camera zoom/unzoom
local function MouseClick()
if zoomed then
-- Unzoom camera
ResetCamera()
else
-- Zoom in camera
ZoomCamera()
end
zoomed = not zoomed
end
local function MouseMoved(input)
if zoomed then
local sensitivity = 0.6 -- anything higher would make looking up and down harder; recommend anything between 0~1
local smoothness = 0.05 -- recommend anything between 0~1
local delta = Vector2.new(input.Delta.x / sensitivity, input.Delta.y / sensitivity) * smoothness
local X = TargetAngleX - delta.y
local Y = TargetAngleY - delta.x
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y
AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
AngleY = AngleY + (TargetAngleY - AngleY) * 0.15
camera.CFrame = CFrame.new(head.Position, target)
* CFrame.Angles(0, math.rad(AngleY), 0)
* CFrame.Angles(math.rad(AngleX), 0, 0)
end
end
local function InputBegan(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
MouseClick()
end
end
local function InputChanged(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseMovement then
MouseMoved(input)
end
end
if UserInputService.MouseEnabled then
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputChanged:Connect(InputChanged)
end

MouseIcon

ContentId
並列読み取り

マウスアイコン プロパティは、ポインターとして使用する画像を決定します。空の場合は、デフォルトの矢印が使用されます。 Class.ImageButton 、 ImageButtonTextButton 、または 2>Class.ProximityPrompt2> の

カーソルを完全に非表示にするには、 透明な画像を使用しない で、UserInputService.MouseIconEnabled を偽に設定します。代わりに、Class.UserInputService.MouseIconEnabled を偽に設定します。

コードサンプル

UserInputService.MouseIcon

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()
print(UserInputService.MouseIcon)

MouseIconEnabled

並列読み取り

このプロパティは、Mouse アイコンが表示されるかどうかを決定します。true マウスのアイコンが表示されるとき、false そうではありません。

たとえば、以下のコードスナップは、マウスのアイコンを非表示にします。


local UserInputService = game:GetService("UserInputService")
UserInputService.MouseIconEnabled = false

Class.UserInputService はクライアント側のみであるため、このプロパティは LocalScript でのみ使用できます。

コードサンプル

Hide Mouse During Keyboard Input

local UserInputService = game:GetService("UserInputService")
local mouseInput = {
Enum.UserInputType.MouseButton1,
Enum.UserInputType.MouseButton2,
Enum.UserInputType.MouseButton3,
Enum.UserInputType.MouseMovement,
Enum.UserInputType.MouseWheel,
}
local keyboard = Enum.UserInputType.Keyboard
local function toggleMouse(lastInputType)
if lastInputType == keyboard then
UserInputService.MouseIconEnabled = false
return
end
for _, mouse in pairs(mouseInput) do
if lastInputType == mouse then
UserInputService.MouseIconEnabled = true
return
end
end
end
UserInputService.LastInputTypeChanged:Connect(toggleMouse)

OnScreenKeyboardPosition

読み取り専用
複製されていません
並列読み取り

このプロパティは、画面上のキーボードの位置をピクセルで記述します。キーボードの位置は、Vector2.new(0, 0) で非表示になります。

Class.UserInputService はクライアント側のみですので、このプロパティは LocalScript または Script に 1>Class.BaseScript.RunContext|RunContext1> を設定している場合にのみ使用できます。

また、OnScreenKeyboardVisibleOnScreenKeyboardSize を参照してください。

コードサンプル

UserInputService.OnScreenKeyboardPosition

local UserInputService = game:GetService("UserInputService")
print(UserInputService.OnScreenKeyboardPosition)

OnScreenKeyboardSize

読み取り専用
複製されていません
並列読み取り

このプロパティは、画面キーボードのサイズをピクセルで表示します。キーボードのサイズは、Vector2.new(0, 0) で表示されるときにはありません。

Class.UserInputService はクライアント側のみですので、このプロパティは LocalScript または Script に 1>Class.BaseScript.RunContext|RunContext1> を設定している場合にのみ使用できます。

また、OnScreenKeyboardVisibleOnScreenKeyboardPosition を参照してください。

OnScreenKeyboardVisible

読み取り専用
複製されていません
並列読み取り

このプロパティは、ユーザーの画面に現在表示されているオンスクリーンキーボードが表示されているかどうかを記述します。

Class.UserInputService はクライアント側のみですので、このプロパティは LocalScript または Script に 1>Class.BaseScript.RunContext|RunContext1> を設定している場合にのみ使用できます。

また、OnScreenKeyboardSizeOnScreenKeyboardPosition を参照してください。

TouchEnabled

読み取り専用
複製されていません
並列読み取り

このプロパティは、ユーザーの現在のデバイスにタッチスクリーンが利用可能かどうかを記述します。

このプロパティは、ユーザーのデバイスにタッチ画面があるかどうかを判断するために使用され、その結果によりタッチイベントが発動するかどうかを決定します。TouchEnabled が true の場合、UserInputService.TouchStarted およびUserInputService.TouchEnded などのユーザー入力サービス

以下のコードスナップでは、ユーザーのデバイスがタッチスクリーンを持っているかどうかを印刷します。


local UserInputService = game:GetService("UserInputService")
if UserInputService.TouchEnabled then
print("The user's device has a touchscreen!")
else
print("The user's device does not have a touchscreen!")
end

参照してください:

コードサンプル

Create a Custom CameraScript

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local torso = character:WaitForChild("HumanoidRootPart")
local playerPosition = torso.Position
local default_CameraPosition = torso.Position
local default_CameraRotation = Vector2.new(0, math.rad(-60))
local default_CameraZoom = 15
local cameraPosition = default_CameraPosition
local cameraRotation = default_CameraRotation
local cameraZoom = default_CameraZoom
local cameraZoomBounds = nil -- {10,200}
local cameraRotateSpeed = 10
local cameraMouseRotateSpeed = 0.25
local cameraTouchRotateSpeed = 10
local function SetCameraMode()
camera.CameraType = "Scriptable"
camera.FieldOfView = 80
camera.CameraSubject = nil
end
local function UpdateCamera()
SetCameraMode()
local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0) * CFrame.Angles(cameraRotation.Y, 0, 0)
camera.CFrame = cameraRotationCFrame + cameraPosition + cameraRotationCFrame * Vector3.new(0, 0, cameraZoom)
camera.Focus = camera.CFrame - Vector3.new(0, camera.CFrame.p.Y, 0)
end
local lastTouchTranslation = nil
local function TouchMove(_touchPositions, totalTranslation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = totalTranslation - lastTouchTranslation
cameraPosition = cameraPosition + Vector3.new(difference.X, 0, difference.Y)
UpdateCamera()
end
lastTouchTranslation = totalTranslation
end
local lastTouchRotation = nil
local function TouchRotate(_touchPositions, rotation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = rotation - lastTouchRotation
cameraRotation = cameraRotation
+ Vector2.new(-difference, 0) * math.rad(cameraTouchRotateSpeed * cameraRotateSpeed)
UpdateCamera()
end
lastTouchRotation = rotation
end
local lastTouchScale = nil
local function TouchZoom(_touchPositions, scale, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = scale - lastTouchScale
cameraZoom = cameraZoom * (1 + difference)
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
lastTouchScale = scale
end
local function Input(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then
if inputObject.UserInputState == Enum.UserInputState.Begin then
-- (I) Zoom In
if inputObject.KeyCode == Enum.KeyCode.I then
cameraZoom = cameraZoom - 15
elseif inputObject.KeyCode == Enum.KeyCode.O then
cameraZoom = cameraZoom + 15
end
-- (O) Zoom Out
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
end
local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
if pressed then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
local rotation = UserInputService:GetMouseDelta()
cameraRotation = cameraRotation + rotation * math.rad(cameraMouseRotateSpeed)
else
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
end
local function PlayerChanged()
local movement = torso.Position - playerPosition
cameraPosition = cameraPosition + movement
playerPosition = torso.Position
UpdateCamera()
end
-- Determine whether the user is on a mobile device
if UserInputService.TouchEnabled then
-- The user is on a mobile device, use Touch events
UserInputService.TouchPan:Connect(TouchMove)
UserInputService.TouchRotate:Connect(TouchRotate)
UserInputService.TouchPinch:Connect(TouchZoom)
else
-- The user is not on a mobile device use Input events
UserInputService.InputBegan:Connect(Input)
UserInputService.InputChanged:Connect(Input)
UserInputService.InputEnded:Connect(Input)
-- Camera controlled by player movement
task.wait(2)
RunService:BindToRenderStep("PlayerChanged", Enum.RenderPriority.Camera.Value - 1, PlayerChanged)
end

VREnabled

読み取り専用
複製されていません
並列読み取り

このプロパティは、ユーザーがバーチャルリアリティ (VR) デバイスを使用しているかどうかを記述します。

VR デバイスが有効になっている場合は、UserInputService:GetUserCFrame() などの機能を使用して、その場所と移動をインタラクトできます。UserInputService.UserCFrameChanged イベントを使用して、VR デバイスの移動にも反応できます。


local UserInputService = game:GetService("UserInputService")
local isUsingVR = UserInputService.VREnabled
if isUsingVR then
print("User is using a VR headset!")
else
print("User is not using a VR headset!")
end

Class.UserInputService はクライアント側のみであるため、このプロパティは LocalScript でのみ使用できます。

参照してください:

コードサンプル

VR Head Tracking

local VRService = game:GetService("VRService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local function TrackHead(inputType, value)
if inputType == Enum.UserCFrame.Head then
head.CFrame = value
end
end
if VRService.VREnabled then
-- Set the initial CFrame
head.CFrame = VRService:GetUserCFrame(Enum.UserCFrame.Head)
-- Track VR headset movement and mirror for character's head
VRService.UserCFrameChanged:Connect(TrackHead)
end

方法

GamepadSupports

この関数は、Enum.UserInputType ゲームパッドが与えられた Enum.KeyCode に対応するボタンをサポートしているかどうかを返します。この関数は、有効なゲームパッドの入力を決定するために使用されます。

どの Enum.UserInputType ゲームパッドに接続されているかを確認するには、UserInputService:GetConnectedGamepads() を使用します。

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。

参照してください:

パラメータ

gamepadNum: Enum.UserInputType

ゲームパッドの Enum.UserInputType

gamepadKeyCode: Enum.KeyCode

ボタンの Enum.KeyCode


戻り値

与えられたゲームパッドが与えられた Enum.KeyCode に対応するボタンをサポートしているかどうか。

コードサンプル

Binding Functions to Gamepad Controls

local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local controller = Enum.UserInputType.Gamepad1
local buttonX = Enum.KeyCode.ButtonX
local function isSupported(gamepad, keycode)
return UserInputService:GamepadSupports(gamepad, keycode)
end
local function action()
print("Action")
end
if isSupported(controller, buttonX) then
ContextActionService:BindAction("sample action", action, false, buttonX)
end

GetConnectedGamepads

この関数は、現在接続されている Enum.UserInputType ゲームパッドのアレイを返します。ゲームパッドが接続されていない場合は、このアレイは空です。また、ゲームパッドが接続されていない場合は、このアレイは空です。たとえゲームパッドが接続されていない場合でも、このイベントはゲームパッド1オブジェクトを返します

たとえば、次のコードスナップは、接続されたゲームパッドを取得し、connectedGamepads という変数に保存します。


local UserInputService = game:GetService("UserInputService")
local connectedGamepads = UserInputService:GetConnectedGamepads()

特定のゲームパッドが接続されているかどうかを確認するには、UserInputService:GetGamepadConnected() を使用します。

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。

参照してください:


戻り値

ユーザーのデバイスに接続されたゲームパッドに対応する UserInputTypes のアレイ。

コードサンプル

How to Set the Active Gamepad for Input

local UserInputService = game:GetService("UserInputService")
local function getActiveGamepad()
local activeGamepad = nil
local connectedGamepads = UserInputService:GetConnectedGamepads()
if #connectedGamepads > 0 then
for _, gamepad in connectedGamepads do
if activeGamepad == nil or gamepad.Value < activeGamepad.Value then
activeGamepad = gamepad
end
end
end
if activeGamepad == nil then -- Nothing is connected; set up for "Gamepad1"
activeGamepad = Enum.UserInputType.Gamepad1
end
return activeGamepad
end
if UserInputService.GamepadEnabled then
local activeGamepad = getActiveGamepad()
print(activeGamepad)
end

GetDeviceAcceleration

GetDeviceAcceleration 機能は、ユーザーのデバイスの現在の加速度を決定します。それは、デバイスの現在の加速度を説明する InputObject を返します。

これが機能するには、ユーザーのデバイスには加速度センサーが有効でなければなりません。ユーザーのデバイスに加速度センサーが有効であるかどうかを確認するには、 UserInputService.AccelerometerEnabled プロパティをチェックできます。

ユーザーのデバイスの加速度が変更される代わりに、UserInputService.DeviceAccelerationChanged イベントを使用することもできます。

ローカルにのみ発行されるため、LocalScript 内のみで使用できます。


戻り値

コードサンプル

Print Device Acceleration

local UserInputService = game:GetService("UserInputService")
local accelerometerEnabled = UserInputService.AccelerometerEnabled
if accelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
print("Cannot get device acceleration because device does not have an enabled accelerometer!")
end

GetDeviceGravity

この関数は、デバイスの現在の重力ベクトルを記述する InputObject を返します。

重力ベクトルは、デバイスの向きが実際の重力の向きに対してどのようになっているかによって決まります。たとえば、デバイスが完全に正しく(ポートレート)なら、重力ベクトルは Datatype.Vector3|Vector3.new(0,

この関数は、ユーザーのデバイスがゲーム内で重力に影響を与えたり、ボールなどのゲームオブジェクトを移動するために使用されるかもしれません。

重力は、モバイルデバイスなど、ジャイロスコープを有効にしたデバイスを使用するプレイヤーにのみ追跡されます。

ユーザーのデバイスにジャイロスコープが有効であるかどうかをチェックするには、UserInputService.GyroscopeEnabled の値をチェックします。デバイスにジャイロスコープが有効である場合は、UserInputService.DeviceGravityChanged イベントを使用して、ユーザーのデバイスの重力の変化を追跡で

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。


戻り値

コードサンプル

Moving Objects with the Gyroscope

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

この関数は、InputObject と、CFrame を返し、デバイスの現在の回転ベクトルを記述します。

これは、InputObject で発動します。入力オブジェクトの Enum.InputType.Gyroscope プロパティは、各ローカルデバイスの軸に対して合計の回転を追跡する En册.InputType|枚列.InputType.Gyroscope です。

デバイスの回転は、gyroscope があるデバイスにのみ追跡できます。

この関数はローカルで発動するため、 LocalScript 内のみで使用できます。


戻り値

2つのプロパティを含むテープル:

  1. デルタプロパティは、最後に発生した回転量を記述します
  2. CFrame は、デバイスのデフォルトの参照フレームに対する当該デバイスの現在の回転量です。

コードサンプル

Print Device Rotation

local UserInputService = game:GetService("UserInputService")
local gyroEnabled = UserInputService:GyroscopeEnabled()
if gyroEnabled then
local _inputObj, cframe = UserInputService:GetDeviceRotation()
print("CFrame: {", cframe, "}")
else
print("Cannot get device rotation because device does not have an enabled gyroscope!")
end

GetFocusedTextBox

この関数は、TextBox クライアントが現在焦点にあるものを返します。 Class.Toolbar:CaptureFocus() 機能を使用して、ユーザーがテキストボックスを手動で選択するか、 Class.Toolbar:CaptureFocus() 機能を使用して選択を強制することができます。1>Class.Toolbar:CaptureFocus1> が選択されていない場合、この関数は

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。

参照してください:


戻り値

コードサンプル

Ignore User Input When a TextBox Is Focused

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")
local jumpKey = Enum.KeyCode.J
local isJumping = false
local function InputBegan(input, _gameProcessedEvent)
local TextBoxFocused = UserInputService:GetFocusedTextBox()
-- Ignore input event if player is focusing on a TextBox
if TextBoxFocused then
return
end
-- Make player jump when user presses jumpKey Key on Keyboard
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == jumpKey then
if not isJumping then
isJumping = true
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
end
local function StateChanged(_oldState, newState)
-- Prevent player from jumping again using jumpKey if already jumping
if newState == Enum.HumanoidStateType.Jumping then
isJumping = true
-- Allow player to jump again after landing
elseif newState == Enum.HumanoidStateType.Landed then
isJumping = false
end
end
UserInputService.InputBegan:Connect(InputBegan)
humanoid.StateChanged:Connect(StateChanged)

GetGamepadConnected

この関数は、енью.UserInputType を与えたゲームパッドがクライアントに接続されているかどうかを返します。

これは、「ゲームパッド1」 などの特定のゲームパッドがクライアントのデバイスに接続されているかどうかをチェックするために使用できます。

接続されたすべてのゲームパッドのリストを取得するには、UserInputService:GetConnectedGamepads() を使用します。

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。

参照してください:

パラメータ

gamepadNum: Enum.UserInputType

ゲームパッドの Enum.UserInputType の内容。


戻り値

ゲームパッドが Enum.UserInputType に関連されているかどうか。

コードサンプル

Check Whether a Gamepad is Connected

local UserInputService = game:GetService("UserInputService")
local isConnected = UserInputService:GetGamepadConnected(Enum.UserInputType.Gamepad1)
if isConnected then
print("Gamepad1 is connected to the client")
else
print("Gamepad1 is not connected to the client")
end

GetGamepadState

この関数は、InputObjects が与えられた Enum.UserInputType ゲームパッドのすべての可用な入力について、それぞれの入力の最後の入力状態を表示するアレイを返します。

接続されたゲームパッドの UserInputTypes を見つけるには、UserInputService:GetConnectedGamepads() を使用します。

この関数はローカルにのみ発動するため、 LocalScript 内でのみ使用できます。

参照してください:

パラメータ

gamepadNum: Enum.UserInputType

ゲームパッドに対応する Enum.UserInputType


戻り値

Class.InputObject|InputObjects アレイ、Class.InputObject|InputObjects が、現在のゲームパッドのすべての入力の状態を表示します。

GetImageForKeyCode

ContentId

このメソッドは、リクエストされた Enum.KeyCode を取得し、現在のゲームパッドデバイスに関連する画像を返します(限定: Xbox、PlayStation および Windows)。これにより、接続されたコントローラーが Xbox のコントローラーである場合、ユーザーは Xbox ア

パラメータ

keyCode: Enum.KeyCode

枚数.KeyCode により、関連する画像を取得する。


戻り値

ContentId

返された画像アセットID。

コードサンプル

UserInputService - Get Image For KeyCode

local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImage

GetKeysPressed

この関数は、現在押されているキーに関連付けられた InputObjects のアレイを返します。

この配列をイテレートして、現在どのキーが押されているかを判断するために、InputObject.KeyCode 値を使用してイテレートできます。

特定のキーが押されているかどうかを確認するには、UserInputService:IsKeyDown() を使用します。

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。


戻り値

現在押されているキーに関連する InputObjects のアレイ。

コードサンプル

Double Jump Key Combo

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")
local actionKey = Enum.KeyCode.LeftShift
local canJump = true
local canDoubleJump = false
local function jumpRequest()
local keysPressed = UserInputService:GetKeysPressed()
for _, key in ipairs(keysPressed) do
if key.KeyCode == actionKey and canJump then
canJump = false
canDoubleJump = true
end
end
end
local function stateChanged(oldState, newState)
-- Double jump during freefall if able to
if oldState == Enum.HumanoidStateType.Jumping and newState == Enum.HumanoidStateType.Freefall and canDoubleJump then
canDoubleJump = false
task.wait(0.2)
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
-- Allow player to jump again after they land
if oldState == Enum.HumanoidStateType.Freefall and newState == Enum.HumanoidStateType.Landed then
canJump = true
end
end
UserInputService.JumpRequest:Connect(jumpRequest)
humanoid.StateChanged:Connect(stateChanged)

GetLastInputType

この関数は、ユーザーの最近の入力に関連付けられた「Enum.UserInputType」を返します。

たとえば、ユーザーの前の入力がスペースバーを押していた場合、Enum.UserInputTypeは、*「キーボード」*に戻ります。

Class.UserInputService.LastInputTypeChangedイベントは、ユーザーが変更する Enum.UserInputType の最後の使用時間を追跡することができます。

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。


戻り値

ユーザーの最新の入力と関連付けられた Enum.UserInputType

コードサンプル

UserInputService:GetLastInputType

local UserInputService = game:GetService("UserInputService")
local lastInput = UserInputService:GetLastInputType()
if lastInput == Enum.UserInputType.Keyboard then
print("Most recent input was via keyboard")
end

GetMouseButtonsPressed

この関数は、現在マウスボタンが押されている InputObjects に対応するアレイを返します。

この関数によって追跡されるマウスボタンには、次のものが含まれます:


<tr>
<td>ボタン1</td>
<td>左のマウスボタン。</td>
</tr>
<tr>
<td>マウスボタン2</td>
<td>右マウスボタン。</td>
</tr>
<tr>
<td>マウスボタン3</td>
<td>中央のマウスボタン。</td>
</tr>
名前説明

ユーザーが関数を呼び出したときにマウスボタンを押していない場合、空の配列を返します。

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。


戻り値

現在、マウスボタンを押し続けるときに対応する InputObjects 配列。

コードサンプル

Check which MouseButtons are Pressed

local UserInputService = game:GetService("UserInputService")
-- InputBegan is a UserInputService event that fires when the player
-- begins interacting via a Human-User input device
UserInputService.InputBegan:Connect(function(_input, _gameProcessedEvent)
-- Returns an array of the pressed MouseButtons
local buttons = UserInputService:GetMouseButtonsPressed()
local m1Pressed, m2Pressed = false, false
for _, button in pairs(buttons) do
if button.UserInputType.Name == "MouseButton1" then
print("MouseButton1 is pressed")
m1Pressed = true
end
if button.UserInputType.Name == "MouseButton2" then
print("MouseButton2 is pressed")
m2Pressed = true
end
if m1Pressed and m2Pressed then
print("Both mouse buttons are pressed")
end
end
end)

GetMouseDelta

この関数は、Mouse の位置を、最後のレンダリングフレームのピクセル単位で変更することで変更を返します。この関数は、Vector2 プロパティを使用してマウスをロックしている場合にのみ機能します。マウ

クライアントの設定により、マウスの感度が UserInputService.MouseDeltaSensitivity に影結果を与えます。

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。


戻り値

マウスの移動に変更を加えます。

コードサンプル

Getting Mouse Delta

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local function OnRenderStep()
local delta = UserInputService:GetMouseDelta()
print("The mouse has moved", delta, "since the last step.")
end
RunService:BindToRenderStep("MeasureMouseMovement", Enum.RenderPriority.Input.Value, OnRenderStep)
Create a Custom CameraScript

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local torso = character:WaitForChild("HumanoidRootPart")
local playerPosition = torso.Position
local default_CameraPosition = torso.Position
local default_CameraRotation = Vector2.new(0, math.rad(-60))
local default_CameraZoom = 15
local cameraPosition = default_CameraPosition
local cameraRotation = default_CameraRotation
local cameraZoom = default_CameraZoom
local cameraZoomBounds = nil -- {10,200}
local cameraRotateSpeed = 10
local cameraMouseRotateSpeed = 0.25
local cameraTouchRotateSpeed = 10
local function SetCameraMode()
camera.CameraType = "Scriptable"
camera.FieldOfView = 80
camera.CameraSubject = nil
end
local function UpdateCamera()
SetCameraMode()
local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0) * CFrame.Angles(cameraRotation.Y, 0, 0)
camera.CFrame = cameraRotationCFrame + cameraPosition + cameraRotationCFrame * Vector3.new(0, 0, cameraZoom)
camera.Focus = camera.CFrame - Vector3.new(0, camera.CFrame.p.Y, 0)
end
local lastTouchTranslation = nil
local function TouchMove(_touchPositions, totalTranslation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = totalTranslation - lastTouchTranslation
cameraPosition = cameraPosition + Vector3.new(difference.X, 0, difference.Y)
UpdateCamera()
end
lastTouchTranslation = totalTranslation
end
local lastTouchRotation = nil
local function TouchRotate(_touchPositions, rotation, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = rotation - lastTouchRotation
cameraRotation = cameraRotation
+ Vector2.new(-difference, 0) * math.rad(cameraTouchRotateSpeed * cameraRotateSpeed)
UpdateCamera()
end
lastTouchRotation = rotation
end
local lastTouchScale = nil
local function TouchZoom(_touchPositions, scale, _velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = scale - lastTouchScale
cameraZoom = cameraZoom * (1 + difference)
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
lastTouchScale = scale
end
local function Input(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then
if inputObject.UserInputState == Enum.UserInputState.Begin then
-- (I) Zoom In
if inputObject.KeyCode == Enum.KeyCode.I then
cameraZoom = cameraZoom - 15
elseif inputObject.KeyCode == Enum.KeyCode.O then
cameraZoom = cameraZoom + 15
end
-- (O) Zoom Out
if cameraZoomBounds ~= nil then
cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
else
cameraZoom = math.max(cameraZoom, 0)
end
UpdateCamera()
end
end
local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
if pressed then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
local rotation = UserInputService:GetMouseDelta()
cameraRotation = cameraRotation + rotation * math.rad(cameraMouseRotateSpeed)
else
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
end
local function PlayerChanged()
local movement = torso.Position - playerPosition
cameraPosition = cameraPosition + movement
playerPosition = torso.Position
UpdateCamera()
end
-- Determine whether the user is on a mobile device
if UserInputService.TouchEnabled then
-- The user is on a mobile device, use Touch events
UserInputService.TouchPan:Connect(TouchMove)
UserInputService.TouchRotate:Connect(TouchRotate)
UserInputService.TouchPinch:Connect(TouchZoom)
else
-- The user is not on a mobile device use Input events
UserInputService.InputBegan:Connect(Input)
UserInputService.InputChanged:Connect(Input)
UserInputService.InputEnded:Connect(Input)
-- Camera controlled by player movement
task.wait(2)
RunService:BindToRenderStep("PlayerChanged", Enum.RenderPriority.Camera.Value - 1, PlayerChanged)
end

GetMouseLocation

この関数は、<a href="/reference/engine/datatypes">データタイプ</a>、<a href="/reference/engine/datatypes/class-Mouse.html">Class.Mouse</a> のピクセルに対するトップ左隅のプレイヤーの Mouse の位置を表示します。これは、GUI のインセットには含まれません。

マウスポインタの場所がオフスクリーンになっているか、プレイヤーのデバイスにマウスがない場合、返される値は未定です。

As UserInputService はクライアント側のみであるため、この関数は LocalScript 内でのみ使用できます。


戻り値

マウスの現在の画面位置をピクセル単位で表示する Vector2

コードサンプル

Move GUI To Mouse Location

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local gui = script.Parent
local screenGui = gui.Parent
screenGui.IgnoreGuiInset = true
local function moveGuiToMouse()
local mouseLocation = UserInputService:GetMouseLocation()
gui.Position = UDim2.fromOffset(mouseLocation.X, mouseLocation.Y)
end
moveGuiToMouse()
RunService:BindToRenderStep("moveGuiToMouse", 1, moveGuiToMouse)

GetNavigationGamepads

この関数は、GUI ナビゲーションに接続されて有効にするためのゲームパッドのアレイを返します。このリストは、優先順位の下で列を返し、どのゲームパッドがナビゲーションを制御するかを判断するために反復可能です。

接続されたゲームパッドがナビゲーションゲームパッドであるかどうかは、ナビゲーションGUIを制御するゲームパッドのみを決定します。これにより、ナビゲーションコントロール操作影響しません。

Class.UserInputService はクライアント側のみであるため、この関数は LocalScript でのみ使用できます。

参照してください:


戻り値

GUI ナビゲーションに使用できる、優先順位の下の Enum.UserInputType|UserInputTypes の配列。

GetStringForKeyCode

GetStringForKeyCode は、ユーザーが入力するために押すべきキーを表示します。Enum.KeyCode を入力するためにキーボードレイアウトを考慮しながら、入力するためにユーザーが押すべきキーを返します。For key codes that require some modifier to be held, this function returns the key to be pressed in addition to the modifier. 以下の例に示すように

Roblox を QWERTY キーボードレイアウトで使用する場合、キーコードは同等の QWERTY ポジションにマップされ


local UserInputService = game:GetService("UserInputService")
local textLabel = script.Parent
local mapKey = Enum.KeyCode.M
textLabel.Text = "Press " .. UserInputService:GetStringForKeyCode(mapKey) .. " to open the map"

QWERTY キーボードの例


<tbody>
<tr>
<td><code>enum.keycode.q</code></td>
<td><code>Q</code></td>
</tr>
<tr>
<td><code>enum.keycode.w</code></td>
<td><code>W</code></td>
</tr>
<tr>
<td><code>enum.keycode.Equals</code></td>
<td><code>=</code></td>
</tr>
<tr>
<td><code>enum.keycode.at</code></td>
<td><code>2</code> 因みに <code>@</code> は Shift で入力されています <kbd>2</kbd></td>
</tr>
</tbody>
キーコード値を返す

AZERTY キーボードの例


<tbody>
<tr>
<td><code>enum.keycode.q</code></td>
<td><code>A</code></td>
</tr>
<tr>
<td><code>enum.keycode.w</code></td>
<td><code>Z</code></td>
</tr>
<tr>
<td><code>enum.keycode.Equals</code></td>
<td><code>=</code></td>
</tr>
<tr>
<td><code>enum.keycode.at</code></td>
<td><code>エ</code></td>
</tr>
</tbody>
キーコード値を返す

ゲームパッド使用

GetStringForKeyCode() は、最近接続されたゲームパッドの Enum.KeyCode にストリングマッピングを返します。接続されていないコントローラーの場合、機能はリクエストされたキーコードのデフォルトのストリング変換を返します。

次の例では、カスタムアセットをマップする方法を示しています: ButtonA :


local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
local mappings = {
ButtonA = "rbxasset://BUTTON_A_ASSET", -- Replace with the desired ButtonA asset
ButtonCross = "rbxasset://BUTTON_CROSS_ASSET" -- Replace with the desired ButtonCross asset
}
local mappedKey = UserInputService:GetStringForKeyCode(key)
local image = mappings[mappedKey]
imageLabel.Image = image

ゲームパッドマップ

方向キーコードにはデバイスに基づく違いはありません。Enum.KeyCode.ButtonSelect には、いくつかの場合、プレイステーションのマッピングを使用してユーザーが正しいボタンを見ることを確認してください。


<tbody>
<tr>
<td><code>enum.keycode.buttona</code></td>
<td><code>ボタンクロス</code></td>
<td><code>ボタンA</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonb</code></td>
<td><code>ボタンサークル</code></td>
<td><code>ボタンB</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonx</code></td>
<td><code>ボタンスクエア</code></td>
<td><code>ボタンX</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttony</code></td>
<td><code>ボタントライアングル</code></td>
<td><code>ボタン Y</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonL1</code></td>
<td><code>ボタンL1</code></td>
<td><code>ボタンLB</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonl2</code></td>
<td><code>ボタンL2</code></td>
<td><code>ボタンLT</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonl3</code></td>
<td><code>ボタンL3</code></td>
<td><code>ボタンLS</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonr1</code></td>
<td><code>ボタンR1</code></td>
<td><code>ボタンRB</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonr2</code></td>
<td><code>ボタンR2</code></td>
<td><code>ボタンRT</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonr3</code></td>
<td><code>ボタンR3</code></td>
<td><code>ボタンRS</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonstart</code></td>
<td><code>ボタンオプションズ</code></td>
<td><code>ボタンスタート</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttonSelect</code></td>
<td><code>ボタンタッチパッド</code> と <code>ボタン共有</code></td>
<td><code>ボタン選択</code></td>
</tr>
</tbody>
キーコードPlayStation のリターン値Xbox リターン値

キーコードのシステム画像

ユーザーインターフェイスの Enum.KeyCode のように、画像としてよく表現される ImageLabel を使用すると、次のレガシーアイコンを使用できます。ただし、GetImageForKeyCode() を使用することを


<tbody>
<tr>
<td><code>enum.keycode.buttonx</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxX.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxX.png</code></td>
</tr>
<tr>
<td><code>enum.keycode.buttony</code></td>
<td>