Engine Class
TextBox
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
คุณสมบัติ
วิธีการ
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
ตัวอย่างโค้ด
การเลือก TextBox
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
ตัวอย่างโค้ด
การเลือก TextBox
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():()
ส่งค่ากลับ
()
ตัวอย่างโค้ด
จับโฟกัส TextBox
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 |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปล่อยโฟกัสจาก TextBox
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)TextBox.FocusLost1
local gui = script.Parent
local textBox = gui.TextBox
local function focusLost(enterPressed)
if enterPressed then
print("โฟกัสถูกสูญเสียเพราะกด Enter!")
else
print("โฟกัสถูกสูญเสียโดยไม่มีการกด Enter")
end
end
textBox.FocusLost:Connect(focusLost)