Get UserInputService from a text-box

Hi, I have been working on a “Super powers” game for some time now. To fire powers the player is required to press a certain Hot-key on there keyboard (Using UserInputService).

I want to make the “certain Hot-key” chosen by the player, to fit the players needs and to make the game easier for the player. I want the player to choose the “certain Hot-key” through a text-box, having the player select the textbox and inserting the letter he/she desires, and then that key will be the new key to fire a certain power.

How would I go about doing this?

Any help is much appreciated. :+1:

2 Likes

You could do this, but without a textbox. You could have a UI with a TextButton on it. Once a player clicks the TextButton, it acts like a TextBox and waits for the user to press a key. When the user presses a key, the TextBoxes text can be updated to that key, and you can set the UserInputService (or ContextActionService) bind/event to the selected key.

*I will try to create a quick demo script to show what I mean.

1 Like

You can make a value or variable that stores whatever keycode the player entered. When the player inputs that keycode, you can run a function

I just made a post similar to yours one hour ago. Hope this helps.

https://devforum.roblox.com/t/text-box-issues/1200515/11

This is my code that i have tried:

wait(1)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")


local Remote = ReplicatedStorage:WaitForChild("SaidRemote")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()

local hotkey = game.Players.LocalPlayer.PlayerGui.UI.Main.Key.Text

local CoolDown = true
local Key = hotkey


UserInputService.InputBegan:Connect(function(Input, IsTyping)
	
	if IsTyping then return end
	
	local KeyPressed = Input.KeyCode
	if KeyPressed ==  Enum.KeyCode[Key] and CoolDown and Character then
		CoolDown = false
		
		Remote:FireServer(Mouse.Hit)
		
		wait(1.5)
		CoolDown = true
	end
end)

Doesnt work though.

I want to only run that function when they press down on the key that is equal to the text-box text.

Ok so I have created a script that does what you want. Here is the script:

local player = game:GetService("Players").LocalPlayer --Player
local UI = script.Parent --The ScreenGui
local F = UI.KeyBinder --The Main Frame
local SB = F.SprintButton --The Button to change the control
local DefaultControl = Enum.KeyCode.LeftShift --The default control. If you want to save their input, then you shouldn't have this, instead you should load their save from a Datastore module like DataStore2

local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")

local function getNameFromKeyCode(KeyCode)
	local sk = tostring(KeyCode)
	local keyCodeName =string.sub(sk, 14)
	print(keyCodeName)
	return keyCodeName
end

local Kn = getNameFromKeyCode(DefaultControl)
SB.Text = Kn

local function actionHandler(actionName, inputState, inputObject)
	if actionName == "Sprint" then
		if inputState == Enum.UserInputState.Begin then
			print("Started Sprinting")
		elseif inputState == Enum.UserInputState.Cancel or inputState == Enum.UserInputState.End then
			print("Stopped Sprinting")
		else
			print("Something Happened When Sprinting")
		end
	end
end

local function buttonPressed()
	print("ButtonPressed")
	local input = UIS.InputBegan:Wait()
	print(tostring(input.KeyCode))
	if input.KeyCode ~= Enum.KeyCode.Escape then
		CAS:UnbindAction("Sprint")
		CAS:BindAction("Sprint", actionHandler, false, input.KeyCode)
		local KeyName = getNameFromKeyCode(input.KeyCode)
		SB.Text = KeyName
		print("Changed Sprint Keybind to "..KeyName..".")
	end
	print("Done")
end

SB.MouseButton1Click:Connect(buttonPressed)

I have it hooked up to a UI (It’s the LocalScript):
image
The UI:
image

If you set it up like this and test it it’ll work. You shouldn’t just use this, because I know there is other stuff you have going on, but this shows how to do it.

1 Like

Thanks, I’ll test it out :+1: :smile:

Yeah it works! Thanks so much! :+1: :smile:

I’m going to adjust it to my needs.

I have had this problem for a while, I really appreciate the help! :grinning:

You’re welcome! I hope you are able to mold it to your needs!

local function getNameFromKeyCode(KeyCode)

local sk = tostring(KeyCode)

local keyCodeName = string.sub(sk, 14)

print(keyCodeName)

return keyCodeName

end

Could you explain this part of the code please? What exactly is its function and how does it work?

I appreciate all help :grinning: :+1:

I will break down each line.

local function getNameFromKeyCode(KeyCode)

This line is simply the function name. It takes a KeyCode as an input.

local sk = tostring(KeyCode)

This line is setting the variable sk to a string version of the input KeyCode. This works by using the functions tostring(), which for this purpose outputs Enum.KeyCode.[KeyCodeHere].

local keyCodeName = string.sub(sk, 14)

This line sets the variables keyCodeName to the digits 15+ of string sk. This is done using the string.sub() function, which outputs a string starting from one point, and ending at another. For example, I could take string abcdefg and use string.sub() on it like this:

local output = string.sub("abcdefg", 2, 5)

In this example, output is equal to bcde.

print(keyCodeName)

This line simply prints the variable keyCodeName which should be the name of the key.

return keyCodeName

Returns the variable keyCodeName to whatever calls the function.

end

The end statement.

EXAMPLE:

I will quickly show an example of how this would work.

The function takes KeyCode’s as an input. KeyCode’s are expressed through Enums. So the KeyCode for the Left Shift key is Enum.KeyCode.LeftShift. When running the tostring() function on that, it returns that as a string "Enum.KeyCode.LeftShift", so using string.sub() you can get all of the text following “Enum.KeyCode.”, which just leaves the input, in this case it’s “LeftShift”. That is what get’s printed, and returned (and in the whole script, gets eventually displayed on the UI)

An example script using this function:

local function getNameFromKeyCode(KeyCode)
	local sk = tostring(KeyCode)
	local keyCodeName =string.sub(sk, 14)
	return keyCodeName
end

print(getNameFromKeyCode(Enum.KeyCode.B))

The Output:

18:18:03.437 B - Client - LocalScript:7

Sidenote: This is being run on the client, as you can see in the Output, but the function uses nothing that is client-specific, so you could run it on the server if you wanted to (though inputs, and KeyCode, are usually dealt with primarily on the client).

If you have any extra questions feel free to ask them.

Or you can just do Enum.KeyCode.B.Name.

Good thought though!