User Input Service
概要
プロパティ
MouseIcon:ContentId |
方法
CreateVirtualInput(target: Instance):Object |
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リファレンス
プロパティ
Accelerometer Enabled
コードサンプル
加速度センサーを使用してボールを移動させる
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)
endModal Enabled
Mouse Icon
User Input Service. Mouse Icon: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()Mouse Icon Content
コードサンプル
カスタムマウスアイコン
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()Preferred Input
コードサンプル
優先入力検出
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)User Head C Frame
VR Enabled
方法
Create Virtual Input
Gamepad Supports
User Input Service: Gamepad Supports(
パラメータ
戻り値
Get Device Acceleration
戻り値
コードサンプル
出力デバイスの加速度
local UserInputService = game:GetService("UserInputService")
if UserInputService.AccelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
warn("デバイスに有効な加速度計がないため、デバイスの加速度を取得できません!")
endGet Device Gravity
戻り値
コードサンプル
ジャイロスコープでオブジェクトを移動させる
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)
endGet Device Rotation
Get Gamepad Connected
パラメータ
戻り値
Get Gamepad State
パラメータ
戻り値
Get Image For Key Code
パラメータ
戻り値
ContentId
コードサンプル
UserInputService - キーコードの画像を取得
local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImageGet Last Input Type
コードサンプル
最後の入力タイプを検出
local UserInputService = game:GetService("UserInputService")
if UserInputService:GetLastInputType() == Enum.UserInputType.Keyboard then
print("最近の入力はキーボードでした!")
endGet Mouse Buttons Pressed
戻り値
コードサンプル
押されたマウスボタンを確認する
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)Get Mouse Delta
戻り値
コードサンプル
マウスのデルタを取得
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)Get String For Key Code
パラメータ
| 既定値: Default |
戻り値
Get Supported Gamepad Key Codes
パラメータ
戻り値
Get User C Frame
Is Gamepad Button Down
User Input Service: Is Gamepad Button Down(
パラメータ
戻り値
Is Key Down
Is Mouse Button Pressed
パラメータ
戻り値
パラメータ
戻り値
Recenter User Head C Frame
User Input Service: Recenter User Head C Frame():()
戻り値
()
パラメータ
戻り値
()
イベント
Device Acceleration Changed
パラメータ
コードサンプル
加速度センサーを使用してプレイヤーを制御する
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)
endDevice Gravity Changed
パラメータ
コードサンプル
加速度センサーを使用してボールを移動させる
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)
endDevice Rotation Changed
パラメータ
Gamepad Connected
パラメータ
Gamepad Disconnected
パラメータ
Input Began
パラメータ
Input Changed
パラメータ
Input Ended
パラメータ
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
-- カスタムジャンプリクエストを処理します
print("ジャンプがリクエストされました!")
-- クールダウン後にデバウンス変数をリセットします
task.wait(COOLDOWN_TIME)
processJumpRequest = false
end
end
UserInputService.JumpRequest:Connect(jumpRequest)Last Input Type Changed
パラメータ
Pointer Action
Text Box Focused
パラメータ
コードサンプル
フォーカス時のテキストボックスの修正
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)Text Box Focus Released
パラメータ
コードサンプル
フォーカス時のテキストボックスの修正
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)Touch Drag
User Input Service. Touch Drag(
パラメータ
Touch Ended
パラメータ
Touch Long Press
User Input Service. Touch Long Press(
パラメータ
Touch Moved
パラメータ
Touch Pan
User Input Service. Touch Pan(
touch Positions:{any}, total Translation:Vector2, velocity:Vector2, state:Enum.UserInputState, game Processed Event:boolean
パラメータ
Touch Pinch
User Input Service. Touch Pinch(
touch Positions:{any}, scale:number, velocity:number, state:Enum.UserInputState, game Processed Event:boolean
パラメータ
Touch Rotate
User Input Service. Touch Rotate(
touch Positions:{any}, rotation:number, velocity:number, state:Enum.UserInputState, game Processed Event:boolean
パラメータ
Touch Started
パラメータ
Touch Swipe
User Input Service. Touch Swipe(
パラメータ
Touch Tap
Touch Tap In World
User C Frame Changed
Window Focus Released
コードサンプル
ウィンドウフォーカス解除スクリプト (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)