I need a way to make a good hotkey maker

  1. 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.

  2. i cant get one to work properly i get it to make the player stop focusing on the text box.

  3. 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.

2 Likes

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

its giving a error at line 3 with the “path”

Well you have to change that part so that it points to the path of your TextBox. It is just a placeholder.

ok i put script.parent cuz i put it in the text box and now its giving attepetd to index nil with keyboard at line 12

Yeah I think I forgot something.

Change UserInputType.Keyboard to Enum.UserInputType.Keyboard on Line 12.

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.

thanks.

1 Like

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.

1 Like

to fix the caplisation and did [{tbt}] and that makes it a table so should i do

[tostring({tbt})]

No. [tbt:upper()] should do it.

You don’t need to make tbt into a table itself because it is to be used as a key for the table Enum.KeyCode.

1 Like

oh, i didn’t know that :upper existed lol

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.