How would I detect what key someone is pressing?

So you may be thinking that I’m asking about how to do input service, but I’m not well yes and no.

So heres what I need to be able to do. I need it to be that when a GUI button is clicked a script will wait for a key to be pressed then send that information the the sever. (PS this is for a custom keybind game.)
Here is the current game that I have. However it goes off of text inputs from the player.

1 Like

Here is how I would detect when a user presses a key on the keyboard and convert the input to a string. I recommend reading the developer hub page on KeyCodes as this will give you a better understanding on how I wrote this code.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.Keyboard then -- check if input was from keyboard
		local value = inputObject.KeyCode.Value -- the value of the enum
		if value > 96 and value < 123 then -- letters are between 97 and 122
			print(string.char(value):upper()) -- convert the enum to a string and print it
		end
	end
end)
8 Likes

So is there a way I can only take A: The first key pressed. and B: Only get start the userinput after they press a GUI button?

First, connect a function to the mouse clicked event for the GUI button by using the following line in a local script.

yourGUIbutton.MouseButton1Click:Connect(function()
    -- do stuff
end)

Then you will need to connect a function to the UserInputService.InputChanged event. This looks like this;

game:GetService(“UserInputService”).InputChanged:Connect(function(input, gameProcessed) -- you can learn more about these parameters on the link I shared above
    -- do stuff
end)

From here; you only want to check for inputs that are from the keyboard. You can do that by using an if statement on the Input parameter’s UserInputType value.

Then, if the if statement confirms that the input is from a keyboard, you want to send the input objects key code to the server using a remote event.

Edit: You need to disconnect the InputChanged event after the user sends a key to the server by using :Disconnect(). To do that you have to set the connection as variable, like this;

local connection
local InputChanged = function(input, gameProccessed)
    -- do stuff, once key is sent add the below line
    connection:Disconnect()
end
connection = game:GetService(“UserInputService”).InputChanged:Connect(InputChanged)

By editing @RetributionVI‘s answer to include my code, you can create what you are looking for.

You can do this by utilising GuiButton.MouseButton1Click and disconnecting the event once a valid key has been pressed. Here is an example by slightly editing my previous code:

local UserInputService = game:GetService("UserInputService")

button.MouseButton1Click:Connect(function()
    button.Text = ""
    local connection
	connection = UserInputService.InputBegan:Connect(function(inputObject)
	    if inputObject.UserInputType == Enum.UserInputType.Keyboard then -- check if input was from keyboard
			local value = inputObject.KeyCode.Value -- the value of the enum
			if value > 96 and value < 123 then -- letters are between 97 and 122
				button.Text = string.char(value):upper() -- convert the enum to a string and set it as the button's text
				connection:Disconnect()
			end
		end
	end)
end)
1 Like

EnumItems have a name value. Using the decimal representation of a letter and converting them from the string library is unnecessary. The name value will give you exactly the name of the key being held. For cases like special keys, it sends out the whole name, not the symbol representation.

local keyName = inputObject.KeyCode.Name
button.Text = keyName
connection:Disconnect()
3 Likes