Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

turning a keycode into a string?

Asked by 6 years ago

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?

0
object.KeyCode.Name lol User#19524 175 — 6y
0
please tell me your kidding me XD popthecorn145 16 — 6y

2 answers

Log in to vote
1
Answered by
Peyt_n 20
6 years ago
Edited 6 years ago

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.

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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!

Answer this question