[Short] How to make game custom keybind(s) (shortcuts) GUI button(s)

Bad title … however.

Hello.
Since I haven’t seen any small or big tutorial about how to specifically make custom keybind buttons and get it to function. I’ll do it myself, in case someone don’t know how.

This is a very friendly tutorial, it doesn’t eat brain cells, it’s only about to how make the buttons and how to make the button do their small job.

First things first:
We need our gui. StarterGui → Add a ScreenGui, inside the ScreenGui add a TextBox.

Second things second.
Inside the TextBox’s properties, turn off TextEditable property, set it to false. That property means that we are able to write inside the TextBox. Which is not what we want. We only want it to listen to any input key we press when are focusing on it.

So inside the properties, scroll down a bit (if you have a short screen), you should see the TextEditable property.

Set it to false (disable it).

Now you’re good to go.
Good to go to the party, scripting.

So add a local script inside the TextBox. And open it.

-- First of all, we need our friend, UserInputService (UIS). Let´s define it.
local UserInputService = game:GetService("UserInputService")

--Now we need to know whenever you or any player in the game presses any keyboard button or mouse clicks or touches phone screen etc.
--So what we do is:
UserInputService.InputBegan:Connect(function(input)
    -- If we print anything now, whatever you do it will print it.
    Print("Hello World") --Will print this whatever you press.
end)

Now you know a bit about UIS (UserInputService). Let’s get the full code.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	-- Now you may ask, what about the TextBox we created ?
	-- Using UserUnputService, we can access what TextBox we are currently focusing on. We can achieve this by doing:
	local focused_textbox = UserInputService:GetFocusedTextBox() --This will be the TextBox we are focusing on.
	
	-- Now since this is a custom keybind, we are only interested in keyboard buttons. So what we do is this:
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if focused_textbox ~= nil then -- We also want to check that the varibale is not being nil, since we are using UserInputService, you might click stuff without being focused on any TextBox.
			focused_textbox.Text = input.KeyCode.Name -- Now we put the Text of the TextBox to the name of the button we pressed.
			focused_textbox:ReleaseFocus() --When player  presses a key, we want to release the focus of the TextBox. This means that when you press a key, you will unfocus on the TextBox.
		end
	end
	
end)
So this little code will wait for any input, creates a variable for the TextBox you are focusing on,
if you are not focusing on any textbox, the variable will be nil. Then It makes sure the input is a keyboard button, then it check if the variable is not nil to prevent errors
and then it will set the text of the textbox to the key you pressed. And last, it will release your focus (stops your focus on the textbox.)

Result:
ezgif.com-crop

You can just add more Textbox and it will work to all of them. Remember to turn off the TextEditable property though.

My first tutorial. I don’t know why I decided to make it, might be helpful to someone.

Have fun developing folks!

12 Likes

Very helpful, altrough hard-coded keybinds are way superior (in Roblox) than custom ones. Since it’s way easier to setup and don’t require to create a whole GUI

This is so useful, it’s literally what I was trying to make but had no idea where to start. Thanks!