i wat a hotkey script that i put in a text box or something and when the player clicks on it they need to press a key and it displays the key and un focuses them on the text box.
i cant get one to work properly i get it to make the player stop focusing on the text box.
i look allover the dev hub and cant find nothing.
need one for a game I’m making and cant get it to work.
that is all i need. in summary stomping the player clicks i detect that click the player types a character it detects that and makes the player not be focused on the text box can.
Check for focus, wait for a key event, release focus.
local UserInputService = game:GetService("UserInputService")
local textBox = path.to.textBox
local inputObject
local selectedKey
textBox.Focused:Connect(function()
repeat
inputObject = UserInputService.InputBegan:Wait()
-- Do checks
if inputObject.UserInputType ~= Enum.UserInputType.Keyboard then
inputObject = nil
end
until inputObject
selectedKey = inputObject.KeyCode
textBox:ReleaseFocus()
-- now you can do whatever you want with the key (selectedKey)
end)
At the end of the script, selectedKey is a variable that contains one of these Enum items: KeyCode
thanks it works i did modify it a tad to dow it runs wait(0.001) with is like one fframe to unfous giving the text box time to display the key and got rid of the slted key cuz i can just use the text string.
ok now im havig a issue using the text as a keycode
local parent = script.Parent
local uis = game.UserInputService
uis.InputBegan:Connect(function(inp, GPE)
local tbt = parent.TextBox.Text
if GPE then return end
if inp.KeyCode == Enum.KeyCode.(tbt) then
parent.TextBox.Text = "You pressed the " .. tbt .. " key!"
end
end)
I wouldn’t recommend trying to reference an Enum item through a string due to capitalization issues, though you can use curly brackets and capitalize the letter if you really want to reference it that way.
Enum.KeyCode.(tbt) should be Enum.KeyCode[tbt].
Do note that this conditional statement would always be true unless you end up changing the value of tbt to something else.