How can I convert a Keycode to a number?

I’m using UserInputService.InputBegan:Connect(function(Key)
to detect when a user clicks 1 key on their keyboard, using Key.KeyCode I get Enum.KeyCode.One, how would convert Enum.KeyCode.One into just 1?

print(Enum.KeyCode.One.Value) -- 49.
print(Enum.KeyCode.One.Value - 1) -- 48
1 Like

What I like to do is

local KeyCode = Enum.KeyCode.One

local Number = tonumber(UserInputService:GetStringForKeyCode(KeyCode))
2 Likes

Each keycode has a value; Enum.KeyCode.One.Value returns 49.
Therefore, if you just subtract 48 from the value of the keycode, you get the associated number.

local function getKeyCodeNumber(keycode)
return keycode.Value - 48
end
5 Likes