Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

turning a keycode into a string?

so i didnt exactly describe what i want. heres my script

local player = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")
local bool = false -- going to make this turn on and off when a player enters a chair



userinputservice.InputBegan:Connect(function(object)
	print(tostring(object.KeyCode))
	if workspace.Seat:FindFirstChild("SeatWeld") then
		
--		game.ReplicatedStorage.KeyBoard:InvokeServer(object.KeyCode)
	end 
	
end)

basicly what i want is when the player pressed down a key. i want it to turn into a string and print (for now) but when i do it prints out "Enum.KeyCode.D" or whatever key i pressed. i just want it it print out "D". its kind of confusing but let me know if that kind of makes any since?

2 comments

0

object.KeyCode.Name lol

0

please tell me your kidding me XD

2 answers

✓ Accepted answer

Each Enum has a 'Value' key containing an ASCII code, so you could do

print(Enum.KeyCode.A.Value) -- 97

-- Convert ASCII value to a character
print(string.char(Enum.KeyCode.A.Value)) -- a

-- Get the uppercase ASCII value of the key code
print(string.char(Enum.KeyCode.A.Value):upper()) -- A

-- Get the ASCII value of the uppercase key code
print(string.byte(string.char(Enum.KeyCode.A.Value):upper())) -- 65

So for your case you could do

local player = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")
local bool = false -- going to make this turn on and off when a player enters a chair

userinputservice.InputBegan:Connect(function(object)
	print(string.char(object.KeyCode.Value):upper())
	if workspace.Seat:FindFirstChild("SeatWeld") then
		-- game.ReplicatedStorage.KeyBoard:InvokeServer(object.KeyCode)
	end 
end)

Take a look at this page for the keycodes:

https://developer.roblox.com/api-reference/enum/KeyCode

You probably won't need them, but its a good reference to have.

Since you can't get the specific KeyCode input by itself printed, you'd have to use the string library provided by Roblox to manipulate for your desired input.

This can be done in two steps: Converting the inputObject you receive from UserInputService.InputBegan into a string, then using substring to cut out unnecessary parts of the inputObject string.

function onKeyPress(inputObject, gameProcessedEvent)
	-- Converts the input into a string
        local keys = tostring(inputObject.KeyCode)

	--[[ Uses the substring function to get all characters after position 13
	    Enum.KeyCode.E
	                 ^ Position 14]]
        local result = string.sub(keys, 14, string.len(keys))

	-- Prints the end result
        print (result)
    end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

If there are any other questions feel free to contact me. Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.