How to find out if a pressed key is a letter of the alphabet

Help

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		-- how to get is letter of alphobet pressed
		
	end
end)
2 Likes

in fact, this can be done with an ingeniously simple crutch, which I just caught up with. #input.KeyCode.Name <= 1

1 Like
	local key = UserInputService:GetStringForKeyCode(input.KeyCode)
	if #key <= 1 and #key > 0 then
		print(key, input.KeyCode)
	end
1 Like

Why are you checking if the length is less than or equal to 1 but also greater than 0? shouldn’t you just use

if #key == 1 then
    ...
end

Secondly, this won’t work properly at all. Take the “\” key for example (Enum.KeyCode.Backslash). The string for this KeyCode is “\”, which is in fact 1 character, so your check will be true, even though this is not a letter of the alphabet. This check will also be true for all numbers, which is probably not intended.

Here is a function that will work properly:

function check(code)
    local num = code.Value

    return num >= Enum.KeyCode.A.Value and num <= Enum.KeyCode.Z.Value
end

This function works by comparing the Value of the keycode to the values of A and Z, making sure it is in between the two.

1 Like