Get number KeyCode given NumKeys

Ex; When I press “1”, I want to bind it to Enum.KeyCode.One.
But I gotta make a table containing 10 number to strings.

Is there a hackier way?

The only way I’m aware of is a dictionary like you were already planning on due to it returning a string. (Unless you want to be even more extra)

There’s this on StackOverflow which suggests doing the same.

Why can you not use the default keycodes?

local ContextActionService = game:GetService("ContextActionService")
function whateveryouwanttodo()
  print("Stuff here")
  --code here

ContextActionService:BindAction("ButtonPress", whateveryouwanttodo(), false, Enum.KeyCode.One)

To put the above contextactionservice into context, use this link

  1. String to represent action
  2. What function you want it to
  3. Create a GUI button
  4. What inputs you want. at the same time you could use a table with a loop to make it simpler

If I am understanding correctly, you are asking how to figure out what key a given Enum is.

This should be simple enough to figure out; all of the keycode Enums have a name, you can just use tostring() on the enum to return the name of the KeyCode.


This errors. 32 isn’t a valid EnumItem and tostring is a global, not a method. Think you meant to do this:

print(Enum.KeyCode.Space.Name) --> Space
print(Enum.KeyCode.Space.Value) --> 32

In any case: for UserInputService, pressing the 1 key already fires with Enum.KeyCode.One supplied as the KeyCode. Not sure of the specific use case but there isn’t really any other way out other than a dictionary, hacky or not.

1 Like

Thanks for the correction. I’m still trying to figure out some of the lesser known ways to mess with keycodes.