Getting a InputObject's Keycode in string form

Hello everybody,

I’ve recently been trying to make this line of code work, but there seems to be a problem

print(tostring(InputObject.KeyCode)) -- simplified

I am trying to print out the key (like E, R, etc) that is pressed (with a BindAction), but when I print it, it prints this instead

    Enum.KeyCode.keythatwaspressed

Any solutions? Thanks!

2 Likes

EnumItem’s have a Name property.

From there you would just do

print(InputObject.KeyCode.Name)

And it wouldn’t return the Enum.X. bit.

Now, for your case, there is a method called UserInputService:GetStringForKeyCode if you would like to check that out as well.

So for something like

print(UserInputService:GetStringForKeyCode(Enum.KeyCode.Two))

It would return 2 and not Two. But it all depends on your use case!

11 Likes

Nono, and no, not use the tostring function to this, just why? I just use this:

If InputObject.KeyCode.Name == "E" then --E ist just a example
    print("KeyCode is E")
end

Or you can use numbers:

if InputObject.KeyCode == 101 then
    print("KeyCode is E")
end

You can find a list of all numbers here:
https://developer.roblox.com/en-us/api-reference/enum/KeyCode

Or (and my fav. method):

if InputObject.KeyCode == Enum.KeyCode.E then
    print("KeyCode is E")
end

Hope this helps.