Trying To Get A Enum.KeyCode From Symbols

Hello, I’ve Tried Many Things To Fix The Problem I’ve Been Having Trying To Make A Custom Keybind Script. I Have It Working With Enum.KeyCode Pieces, But Not With The Symbols. I Don’t Know How I Might Do This Because Every Time I Try I Get An Error Saying It Gets A Nil Value, Even When It Shouldn’t Be A Nil Value. (Symbol As In The Shown Key On The Keyboard)

Script:

if self.PlayerData[Player.UserId][KeybindUsage] and (typeof(Enum.KeyCode:GetEnumItems()[string.char(KeybindChange.Name)]) == "EnumItem" and Enum.KeyCode[string.char(KeybindChange.Name)].EnumType == Enum.KeyCode) and not BlacklistedKeybinds[Enum.KeyCode[string.char(KeybindChange.Name)]] then
		return true, string.char(Enum.KeyCode[KeybindChange]), Enum.KeyCode[string.char(KeybindChange.Name)]

Thank You In Advance For Any And All Answers To This To Help Me Understand Anything I Might Have Done Wrong. If You Need More Info, Ask Me Some Questions As Well. All The Items In The Script Are Defined Already. (Server Script)

According to this, KeyCode has a specific value for each of them. Have you tried using that?

I have tried using the values of the ‘KeybindChange’ but I’m trying to use a symbol such as ` so it doesn’t have a value so I’m trying to find a way to get the KeyCode that it correlates to.

You can get the value of a character by using string.byte on it

As for the getting a keycode part: I don’t know any built-in ways to look for a keycode using its associated character/value but you could use this:

Code
local function GetMatchingKeyCodeFromValue(Value: number)
	for i, Keycode in Enum.KeyCode:GetEnumItems() do
		if Keycode.Value == Value then
			return Keycode
		end
	end
end
1 Like

This is the correct way to do it:

Enum.KeyCode.Backquote

(i had to cycle through the entire keycode library lol)

All symbols have names, just like # is called hash, or ~ is called tilde.

I’m Trying To Get The Symbol, In This Case A ` Into An Enum, But I Don’t Want To Loop Through A Hand-Made Library To Find The Correct KeyCode.

The Problem With This, Is That When I Try Getting The Value Of It, It Is Nil. This Would Work In A Case Other Than Mine, Though.

You’re definitely somehow using it wrong. The Roblox Keycode enums are tied to the ASCII table: https://www.asciitable.com/. If you don’t find the key you need then that sounds like a different problem.

In your case, you’re looking for `, which is 96;
image
and
image

I’m not going from Enum’s I’m going from the text someone can input, such as a ` not Enum.KeyCode.Backquote

Then just do string.byte(text) to get the value and then use the function I gave you?

1 Like

Do you know if this would possibly work if you were to input the KeyCode’s name, such as ‘Backquote’ or ‘Period’ aswell?

Can’t you just do this?

if script.Parent.Text = "`" then
    return Enum.KeyCode.Backquote
end

I’d need a lot of of statements for a keybind changer if I were to use that.

2 Likes

You can make the original function I gave you check for the name of the key instead of its ASCII value:

local function GetMatchingKeyCodeFromName(Name: string): Enum.KeyCode?
	for i, Keycode in Enum.KeyCode:GetEnumItems() do
		if Keycode.Name == Name then
			return Keycode
		end
	end
end