Change GetStringForKeyCode() to Return the String of Keycodes

As a Roblox developer, it is currently too hard to support all input types by sharing the correct input prompts to players.

If this issue is addressed, it would improve my development experience because I wouldn’t have to make my own system for input displaying based on other Roblox features.

1 Like

There is no standardized keyboard layout that changes the position of LeftShift, so it wouldn’t make sense to call GetStringForKeyCode on it.

Are you asking for a behavior change to return the name, even if the keycode won’t map differently? You can already achieve that:

local function GetStringForKeyCode(KeyCode: Enum.KeyCode): string
	local String = UserInputService:GetStringForKeyCode(KeyCode)
	
	if String == "" then
		return KeyCode.Name
	end
	
	return String
end
1 Like

No and yes then. It was a bad example.

For Qwerty and azerty keyboards, right. Q is the same as A. So, when using the API, with those keyboards and that relationship in mind (Q is A), it should give the string ‘Q’ on a Qwerty keyboard and the string ‘A’ on a azerty keyboard.

I don’t have an azerty keyboard to check if the API actually does give back ‘A’. So, idk if that works. But, I do have a qwerty keyboard, and when I give it Q it gives ’ ’ back, not ‘Q’ back. That’s not intuitive.

I don’t think there’s a way to check what kind of keyboard the player has (qwerty or otherwise). So, I can’t make my code tailor to keyboards OTHER than this API, right. And since I can’t check if the player is using qwerty, I don’t know if I need to do a check with the API. So, I have to always use the API.

The “solution” here can end up showing the name of the key code, which CAN be not the prompt the user has/needs to interact with whatever.

It appears to work fine for me, then.

Output

I’m on Windows 11 with the default US keyboard layout. I’m able to change the layout by going to Time & languageLanguage & regionEnglish (United States)Language optionsInstalled keyboards.

If it’s returning you a blank string, you should file a bug report with your system info, keyboard manufacturer and settings.

1 Like

Wow. That’s very fair, it could be a bug specific to my stuff. I thought the enum printing nothing for Q was intentional and everyone had it because I saw this bug report. I must’ve misinterpreted it.

Sadly, for some personal reasons I won’t have Internet to test things on my laptop for IDK how long (I’m using mobile data and my phone rn). That makes dealing with this annoying. Thanks a bunch for testing it out on your end! I’ve been basically whining about this for like 2 months. Will test when I can. :sweat_smile:

2 Likes

This was an issue in my personal project code that was fixed when I made a Repro. My bad.


Hi (also @idevride), and thanks, the solution of checking if the string returns "" fixed my issue with Enum.Keycode.LeftShift. However, I had to update the solution to include " " in addition to the "" to fix Enum.Keycode.Space returning how I didn’t want it to.

Code (drop down)
local UserInputService = game:GetService("UserInputService")

local inputTable = {
	testInput_1 = {
		name = "testInput_1",
		value = "Enum.KeyCode.Space",
	},
	
	testInput_2 = {
		name = "testInput_2",
		value = "Enum.KeyCode.LeftShift",
	},
	
	testInput_3 = {
		name = "testInput_3",
		value = "Enum.KeyCode.Q",
	},
}


local function updateText()
	local valueToShow = inputTable.testInput_1.value
	-- the above is a string
		
	local step_a = string.sub(valueToShow,14) 
	print(step_a)
	
	local step_b = Enum.KeyCode[step_a]
	local step_c = UserInputService:GetStringForKeyCode(step_b)
	
	print(step_c)
	
	if step_c == "" or step_c == " " then
		print("checked this?!")
		step_c = step_b.Name
		print(step_b.Name)
	end
	
	print(step_c)		
end

updateText()

I personally think UserInputService:GetStringForKeyCode(Enum.KeyCode.Space) should return "Space" and not " ". Are there any other Enum’s I should account for here? By that I mean are there any Enums that wouldn’t return "", " ", or the logical name I’d assume it would (i.e. space returning “space”)?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.