エンジンクラス
TextBox
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
概要
プロパティ
方法
CaptureFocus():() |
ReleaseFocus(submitted: boolean):() |
イベント
FocusLost(enterPressed: boolean,inputThatCausedFocusLoss: InputObject):RBXScriptSignal |
コードサンプル
テキストボックスの秘密の単語
-- このコードをテキストボックス内のLocalScriptに配置します
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- 白
local colorWrong = Color3.new(1, 0, 0) -- 赤
local colorCorrect = Color3.new(0, 1, 0) -- 緑
-- textBoxの状態を初期化します
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "秘密の単語は何ですか?"
textBox.BackgroundColor3 = colorNormal
local function onFocused()
textBox.BackgroundColor3 = colorNormal
end
local function onFocusLost(enterPressed, _inputObject)
if enterPressed then
local guess = textBox.Text
if guess == secretWord then
textBox.Text = "アクセス許可されました"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "アクセス拒否されました"
textBox.BackgroundColor3 = colorWrong
end
else
-- プレイヤーはEnterを押さずに編集を停止しました
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)APIリファレンス
プロパティ
CursorPosition
コードサンプル
テキストボックスの選択
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("選択がありません")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('選択されたテキストは:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)FontSize
SelectionStart
コードサンプル
テキストボックスの選択
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("選択がありません")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('選択されたテキストは:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)TextColor
TextColor3
コードサンプル
母音検出器
local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- 母音をチェックします
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- 黒
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- 赤
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)TextWrap
方法
CaptureFocus
TextBox:CaptureFocus():()
戻り値
()
コードサンプル
テキストボックスのフォーカスを取得
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Q then
textBox:CaptureFocus()
end
end)ReleaseFocus
パラメータ
| 既定値: false |
戻り値
()
コードサンプル
テキストボックスのフォーカスを解除
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Escape and textBox.IsFocused then
textBox:ReleaseFocus()
end
end)イベント
Focused
コードサンプル
テキストボックスのフォーカス
local textBox = script.Parent
textBox.Focused:Connect(function()
print("フォーカスされています!")
end)
textBox.FocusLost:Connect(function()
print("フォーカスが失われました!")
end)FocusLost
パラメータ
コードサンプル
テキストボックスのフォーカス
local textBox = script.Parent
textBox.Focused:Connect(function()
print("フォーカスされています!")
end)
textBox.FocusLost:Connect(function()
print("フォーカスが失われました!")
end)フォーカスが外れました
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("プレイヤーがEnterを押しました")
else
print("プレイヤーが", inputThatCausedFocusLost.KeyCode, "を押しました")
end
end
textBox.FocusLost:Connect(onFocusLost)テキストボックス.FocusLost1
local gui = script.Parent
local textBox = gui.TextBox
local function focusLost(enterPressed)
if enterPressed then
print("エンターが押されたためフォーカスが失われました!")
else
print("エンターが押されなかったためフォーカスが失われました")
end
end
textBox.FocusLost:Connect(focusLost)