エンジンクラス
UserInputService
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
概要
プロパティ
MouseIcon:ContentId |
方法
GamepadSupports(gamepadNum: Enum.UserInputType,gamepadKeyCode: Enum.KeyCode):boolean |
GetGamepadConnected(gamepadNum: Enum.UserInputType):boolean |
GetGamepadState(gamepadNum: Enum.UserInputType):{InputObject} |
GetImageForKeyCode(keyCode: Enum.KeyCode):ContentId |
GetStringForKeyCode(keyCode: Enum.KeyCode,format: Enum.KeyCodeStringFormat):string |
GetSupportedGamepadKeyCodes(gamepadNum: Enum.UserInputType):{any} |
GetUserCFrame(type: Enum.UserCFrame):CFrame |
IsGamepadButtonDown(gamepadNum: Enum.UserInputType,gamepadKeyCode: Enum.KeyCode):boolean |
IsKeyDown(keyCode: Enum.KeyCode):boolean |
IsMouseButtonPressed(mouseButton: Enum.UserInputType):boolean |
IsNavigationGamepad(gamepadEnum: Enum.UserInputType):boolean |
SetNavigationGamepad(gamepadEnum: Enum.UserInputType,enabled: boolean):() |
イベント
APIリファレンス
プロパティ
AccelerometerEnabled
コードサンプル
加速度センサーを使用してボールを移動させる
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)
endModalEnabled
MouseIcon
UserInputService.MouseIcon:ContentId
コードサンプル
カスタムマウスアイコン
local UserInputService = game:GetService("UserInputService")
-- カーソルを以前に設定したものに戻すには、変数に保存する必要があります
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- 現在保存されていない場合のみ保存されたカーソルを更新します
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- 保存されたカーソルがある場合のみマウスカーソルを復元します
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- 同じカーソルを二度復元しないようにします(他のスクリプトを上書きする可能性があります)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.com/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()MouseIconContent
コードサンプル
カスタムマウスアイコン
local UserInputService = game:GetService("UserInputService")
-- カーソルを以前に設定したものに戻すには、変数に保存する必要があります
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- 現在保存されていない場合のみ保存されたカーソルを更新します
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- 保存されたカーソルがある場合のみマウスカーソルを復元します
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- 同じカーソルを二度復元しないようにします(他のスクリプトを上書きする可能性があります)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.com/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()PreferredInput
コードサンプル
優先入力検出
local UserInputService = game:GetService("UserInputService")
local function preferredInputChanged()
local preferredInput = UserInputService.PreferredInput
if preferredInput == Enum.PreferredInput.Touch then
-- プレイヤーは他の入力タイプが利用できない/接続されていないタッチ対応デバイスを使用しています
print("タッチ")
elseif preferredInput == Enum.PreferredInput.Gamepad then
-- プレイヤーが接続したか、最近ゲームパッドと対話しました
print("ゲームパッド")
elseif preferredInput == Enum.PreferredInput.KeyboardAndMouse then
-- プレイヤーが接続したか、最近キーボードまたはマウスと対話しました
print("キーボードとマウス")
end
end
preferredInputChanged()
UserInputService:GetPropertyChangedSignal("PreferredInput"):Connect(function()
preferredInputChanged()
end)UserHeadCFrame
VREnabled
方法
GamepadSupports
UserInputService:GamepadSupports(
パラメータ
戻り値
GetDeviceAcceleration
戻り値
コードサンプル
出力デバイスの加速度
local UserInputService = game:GetService("UserInputService")
if UserInputService.AccelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
warn("デバイスに有効な加速度計がないため、デバイスの加速度を取得できません!")
endGetDeviceGravity
戻り値
コードサンプル
ジャイロスコープでオブジェクトを動かす
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
-- ジャイロスコープが変化を検出した際にイベントをバインドする
UserInputService.DeviceGravityChanged:Connect(function(accel)
-- ジャイロスコープデータに基づいてバブルを世界で移動する
bubble.Position = Vector3.new(-8 * accel.Position.X, 1.8, -8 * accel.Position.Z)
end)
endGetDeviceRotation
GetGamepadConnected
パラメータ
戻り値
GetGamepadState
パラメータ
戻り値
GetImageForKeyCode
パラメータ
戻り値
ContentId
コードサンプル
UserInputService - キーコードに対する画像を取得
local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImageGetLastInputType
コードサンプル
最後の入力タイプを検出
local UserInputService = game:GetService("UserInputService")
if UserInputService:GetLastInputType() == Enum.UserInputType.Keyboard then
print("最新の入力はキーボードでした!")
endGetMouseButtonsPressed
戻り値
コードサンプル
押されたマウスボタンを確認する
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
-- 押されたマウスボタンの配列を返す
local buttonsPressed = UserInputService:GetMouseButtonsPressed()
local m1, m2 = false, false
for _, button in buttonsPressed do
if button.UserInputType == Enum.UserInputType.MouseButton1 then
print("MouseButton1 が押されました!")
m1 = true
end
if button.UserInputType == Enum.UserInputType.MouseButton2 then
print("MouseButton2 が押されました!")
m2 = true
end
if m1 and m2 then
print("両方のマウスボタンが押されました!")
end
end
end)GetMouseDelta
戻り値
コードサンプル
マウスデルタの取得
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("マウスが移動しました", delta, "最後のステップから")
end
end
RunService:BindToRenderStep("MeasureMouseMovement", Enum.RenderPriority.Input.Value, onRenderStep)GetStringForKeyCode
パラメータ
| 既定値: "Default" |
戻り値
GetSupportedGamepadKeyCodes
パラメータ
戻り値
GetUserCFrame
IsGamepadButtonDown
UserInputService:IsGamepadButtonDown(
パラメータ
戻り値
IsKeyDown
IsMouseButtonPressed
パラメータ
戻り値
パラメータ
戻り値
RecenterUserHeadCFrame
UserInputService:RecenterUserHeadCFrame():()
戻り値
()
パラメータ
戻り値
()
イベント
DeviceAccelerationChanged
パラメータ
コードサンプル
加速度センサを使用してプレイヤーを制御
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)
endDeviceGravityChanged
パラメータ
コードサンプル
加速度センサーを使用してボールを移動させる
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)
endDeviceRotationChanged
パラメータ
GamepadConnected
パラメータ
GamepadDisconnected
パラメータ
InputBegan
パラメータ
InputChanged
パラメータ
InputEnded
パラメータ
JumpRequest
コードサンプル
デフォルトのジャンプを無効にする
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
-- カスタムジャンプリクエストを処理する
print("ジャンプが要求されました!")
-- クールダウン後にデバウンス変数をリセットする
task.wait(COOLDOWN_TIME)
processJumpRequest = false
end
end
UserInputService.JumpRequest:Connect(jumpRequest)LastInputTypeChanged
パラメータ
PointerAction
TextBoxFocused
パラメータ
コードサンプル
フォーカス時のテキストボックスの修正
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)TextBoxFocusReleased
パラメータ
コードサンプル
フォーカス時のテキストボックスの修正
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
UserInputService.TouchDrag(
パラメータ
TouchEnded
パラメータ
TouchLongPress
UserInputService.TouchLongPress(
パラメータ
TouchMoved
パラメータ
TouchPan
UserInputService.TouchPan(
touchPositions:{any}, totalTranslation:Vector2, velocity:Vector2, state:Enum.UserInputState, gameProcessedEvent:boolean
パラメータ
TouchPinch
UserInputService.TouchPinch(
touchPositions:{any}, scale:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
パラメータ
TouchRotate
UserInputService.TouchRotate(
touchPositions:{any}, rotation:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
パラメータ
TouchStarted
パラメータ
TouchSwipe
UserInputService.TouchSwipe(
パラメータ
TouchTap
TouchTapInWorld
UserCFrameChanged
WindowFocusReleased
コードサンプル
ウィンドウフォーカス解除スクリプト (LocalScript)
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)ウィンドウフォーカスアウェイスクリプト (スクリプト)
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)