How do I use the KeyCode Value Instead of the Name?

On the documentation for keycode, it shows values that I think you should be able to use instead of the keycode name, I’m not sure how to use them though and I’ve already searched but they don’t show you. Does anyone know how?

Assuming a UserInputService example

UserInputService.InputBegan:Connect(function(input, gpe)
    if gpe then 
    if input.KeyCode.Value == 56 then -- Number 8
        print("8 pressed")

I’m trying to use it in ContextActionService:BindAction().

You can’t give a keycode value for BindAction, it errors, you have to provide the keycode itself or its name

I know that… :sweat_smile: I was just hoping I was doing something wrong, thanks anyway.

1 Like

Could you use it like this:?

Enum.KeyCode[slot.Name:match("%d") == "1" and One or Two]

I know this doesn’t work but do you know what I could do instead?

What are you trying to do exactly? That could would work if One and Two are variables that contain strings, though I recommend using Roblox’s way of doing a condensed if statement

if condition then true_value else false_value

I’m looping through an object’s children which will have two things called Slot1 and Slot2 and slot is what each of them will be stored in when looping. So slot.Name:match("%d") for Slot1 should be 1 and the same for Slot2. And I’m trying to activate a function when the keycode is pressed for each.

Sorry if that sounded confusing.

Then what you said should work if you do

Enum.KeyCode[slot.Name:match("%d") == 1 and "One" or "Two"]

Though why exactly do you need to do it like this if you can bind 2 actions for One and two immediately without the condesnesed if statement

Thanks, I was just about to try this. You talked about a different way of doing it like this:

if slot.Name:match("%d") == "1" then "One" else "Two"]

Is there a difference between the two?

Okay wait I should’ve used the new method

Enum.KeyCode[if slot.Name:match("%d") == 1 then "One" else "Two"]

They’re both the same, though if the thing after the and is falsey (null or false) then it breaks a bit and doesn’t give an expected value, the new method prevents that fro mhappening

1 Like

Although ‘Index’ may not correspond to the enumeration item’s value.

1 Like