Classe de moteur
TextBox
*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.
Résumé
Propriétés
Méthodes
CaptureFocus():() |
ReleaseFocus(submitted: boolean):() |
Événements
FocusLost(enterPressed: boolean,inputThatCausedFocusLoss: InputObject):RBXScriptSignal |
Échantillons de code
TexteSecret du TextBox
-- Placez ce code dans un LocalScript à l'intérieur d'un TextBox
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- blanc
local colorWrong = Color3.new(1, 0, 0) -- rouge
local colorCorrect = Color3.new(0, 1, 0) -- vert
-- Initialisez l'état du textBox
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "Quel est le mot secret?"
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 = "ACCÈS AUTORISÉ"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "ACCÈS REFUSÉ"
textBox.BackgroundColor3 = colorWrong
end
else
-- Le joueur a cessé d'éditer sans appuyer sur Entrée
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)Référence API
Propriétés
CursorPosition
Échantillons de code
Sélections de Zone de Texte
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Aucune sélection")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('La sélection est : "', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)FontSize
SelectionStart
Échantillons de code
Sélections de Zone de Texte
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Aucune sélection")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('La sélection est : "', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)TextColor
TextColor3
Échantillons de code
Détecteur de Voyelles
local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- Vérifier les voyelles
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- Noir
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- Rouge
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)TextWrap
Méthodes
CaptureFocus
TextBox:CaptureFocus():()
Retours
()
Échantillons de code
Capturer le focus du 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
Paramètres
| Valeur par défaut : false |
Retours
()
Échantillons de code
Libération du Focus du 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)Événements
Focused
Échantillons de code
Focus de la zone de texte
local textBox = script.Parent
textBox.Focused:Connect(function()
print("En focus !")
end)
textBox.FocusLost:Connect(function()
print("Focus perdu !")
end)FocusLost
Paramètres
Échantillons de code
Focus de la zone de texte
local textBox = script.Parent
textBox.Focused:Connect(function()
print("En focus !")
end)
textBox.FocusLost:Connect(function()
print("Focus perdu !")
end)PerteDeFocus
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("Le joueur a appuyé sur Entrée")
else
print("Le joueur a appuyé sur", 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("Le focus a été perdu parce que la touche entrer a été pressée!")
else
print("Le focus a été perdu sans que la touche entrer ait été pressée")
end
end
textBox.FocusLost:Connect(focusLost)