Fix glfwGetKeyName incorrectly emitting error

glfwGetKeyName emitted GLFW_INVALID_VALUE when passed GLFW_KEY_UNKNOWN
and any scancode not associated with a key token on that platform.

This causes physical keys with no associated key token to emit
GLFW_INVALID_VALUE when the key and scancode are passed directly from
the key event to glfwGetKeyName.  This breaks the promise made in the
reference documentation for glfwGetKeyName.

This commit removes that error for the whole range of valid scancodes.

Fixes #1785
This commit is contained in:
Camilla Löwy 2023-12-13 15:27:30 +01:00
parent 7e2470d343
commit 86bf5698ec
4 changed files with 17 additions and 9 deletions

View File

@ -1659,14 +1659,15 @@ const char* _glfwGetScancodeNameCocoa(int scancode)
{ {
@autoreleasepool { @autoreleasepool {
if (scancode < 0 || scancode > 0xff || if (scancode < 0 || scancode > 0xff)
_glfw.ns.keycodes[scancode] == GLFW_KEY_UNKNOWN)
{ {
_glfwInputError(GLFW_INVALID_VALUE, "Invalid scancode %i", scancode); _glfwInputError(GLFW_INVALID_VALUE, "Invalid scancode %i", scancode);
return NULL; return NULL;
} }
const int key = _glfw.ns.keycodes[scancode]; const int key = _glfw.ns.keycodes[scancode];
if (key == GLFW_KEY_UNKNOWN)
return NULL;
UInt32 deadKeyState = 0; UInt32 deadKeyState = 0;
UniChar characters[4]; UniChar characters[4];

View File

@ -2223,14 +2223,17 @@ void _glfwSetCursorModeWin32(_GLFWwindow* window, int mode)
const char* _glfwGetScancodeNameWin32(int scancode) const char* _glfwGetScancodeNameWin32(int scancode)
{ {
if (scancode < 0 || scancode > (KF_EXTENDED | 0xff) || if (scancode < 0 || scancode > (KF_EXTENDED | 0xff))
_glfw.win32.keycodes[scancode] == GLFW_KEY_UNKNOWN)
{ {
_glfwInputError(GLFW_INVALID_VALUE, "Invalid scancode %i", scancode); _glfwInputError(GLFW_INVALID_VALUE, "Invalid scancode %i", scancode);
return NULL; return NULL;
} }
return _glfw.win32.keynames[_glfw.win32.keycodes[scancode]]; const int key = _glfw.win32.keycodes[scancode];
if (key == GLFW_KEY_UNKNOWN)
return NULL;
return _glfw.win32.keynames[key];
} }
int _glfwGetKeyScancodeWin32(int key) int _glfwGetKeyScancodeWin32(int key)

View File

@ -2535,8 +2535,7 @@ void _glfwSetCursorModeWayland(_GLFWwindow* window, int mode)
const char* _glfwGetScancodeNameWayland(int scancode) const char* _glfwGetScancodeNameWayland(int scancode)
{ {
if (scancode < 0 || scancode > 255 || if (scancode < 0 || scancode > 255)
_glfw.wl.keycodes[scancode] == GLFW_KEY_UNKNOWN)
{ {
_glfwInputError(GLFW_INVALID_VALUE, _glfwInputError(GLFW_INVALID_VALUE,
"Wayland: Invalid scancode %i", "Wayland: Invalid scancode %i",
@ -2545,6 +2544,9 @@ const char* _glfwGetScancodeNameWayland(int scancode)
} }
const int key = _glfw.wl.keycodes[scancode]; const int key = _glfw.wl.keycodes[scancode];
if (key == GLFW_KEY_UNKNOWN)
return NULL;
const xkb_keycode_t keycode = scancode + 8; const xkb_keycode_t keycode = scancode + 8;
const xkb_layout_index_t layout = const xkb_layout_index_t layout =
xkb_state_key_get_layout(_glfw.wl.xkb.state, keycode); xkb_state_key_get_layout(_glfw.wl.xkb.state, keycode);

View File

@ -2901,14 +2901,16 @@ const char* _glfwGetScancodeNameX11(int scancode)
if (!_glfw.x11.xkb.available) if (!_glfw.x11.xkb.available)
return NULL; return NULL;
if (scancode < 0 || scancode > 0xff || if (scancode < 0 || scancode > 0xff)
_glfw.x11.keycodes[scancode] == GLFW_KEY_UNKNOWN)
{ {
_glfwInputError(GLFW_INVALID_VALUE, "Invalid scancode %i", scancode); _glfwInputError(GLFW_INVALID_VALUE, "Invalid scancode %i", scancode);
return NULL; return NULL;
} }
const int key = _glfw.x11.keycodes[scancode]; const int key = _glfw.x11.keycodes[scancode];
if (key == GLFW_KEY_UNKNOWN)
return NULL;
const KeySym keysym = XkbKeycodeToKeysym(_glfw.x11.display, const KeySym keysym = XkbKeycodeToKeysym(_glfw.x11.display,
scancode, _glfw.x11.xkb.group, 0); scancode, _glfw.x11.xkb.group, 0);
if (keysym == NoSymbol) if (keysym == NoSymbol)