From afec46dbee7011c57e6ba75d6141823151158c92 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 5 Aug 2024 19:09:18 -0700
Subject: [PATCH] Re-added SDL_GetScancodeFromKey()
---
include/SDL3/SDL_keyboard.h | 20 ++++++++++++++++++++
src/dynapi/SDL_dynapi.sym | 1 +
src/dynapi/SDL_dynapi_overrides.h | 1 +
src/dynapi/SDL_dynapi_procs.h | 1 +
src/events/SDL_keyboard.c | 18 +++++++++++-------
5 files changed, 34 insertions(+), 7 deletions(-)
diff --git a/include/SDL3/SDL_keyboard.h b/include/SDL3/SDL_keyboard.h
index f5b474a33f1f4..5a6f86efbe6f6 100644
--- a/include/SDL3/SDL_keyboard.h
+++ b/include/SDL3/SDL_keyboard.h
@@ -199,9 +199,29 @@ extern SDL_DECLSPEC void SDLCALL SDL_SetModState(SDL_Keymod modstate);
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_GetKeyName
+ * \sa SDL_GetScancodeFromKey
*/
extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate, SDL_bool key_event);
+/**
+ * Get the scancode corresponding to the given key code according to the
+ * current keyboard layout.
+ *
+ * Note that there may be multiple scancode+modifier states that can generate
+ * this keycode, this will just return the first one found.
+ *
+ * \param key the desired SDL_Keycode to query.
+ * \param modstate a pointer to the modifier state that would be used when the
+ * scancode generates this key, may be NULL.
+ * \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
+ *
+ * \since This function is available since SDL 3.0.0.
+ *
+ * \sa SDL_GetKeyFromScancode
+ * \sa SDL_GetScancodeName
+ */
+extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromKey(SDL_Keycode key, SDL_Keymod *modstate);
+
/**
* Set a human-readable name for a scancode.
*
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index 7c4bcd6578bf1..572417966d3c5 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -411,6 +411,7 @@ SDL3_0.0.0 {
SDL_GetRendererProperties;
SDL_GetRevision;
SDL_GetSIMDAlignment;
+ SDL_GetScancodeFromKey;
SDL_GetScancodeFromName;
SDL_GetScancodeName;
SDL_GetSemaphoreValue;
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 4832cd7c093b9..7e8550db84d72 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -436,6 +436,7 @@
#define SDL_GetRendererProperties SDL_GetRendererProperties_REAL
#define SDL_GetRevision SDL_GetRevision_REAL
#define SDL_GetSIMDAlignment SDL_GetSIMDAlignment_REAL
+#define SDL_GetScancodeFromKey SDL_GetScancodeFromKey_REAL
#define SDL_GetScancodeFromName SDL_GetScancodeFromName_REAL
#define SDL_GetScancodeName SDL_GetScancodeName_REAL
#define SDL_GetSemaphoreValue SDL_GetSemaphoreValue_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index a9241744542cc..6cbded81cdf38 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -456,6 +456,7 @@ SDL_DYNAPI_PROC(const char *,SDL_GetRendererName,(SDL_Renderer *a),(a),return)
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetRendererProperties,(SDL_Renderer *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetRevision,(void),(),return)
SDL_DYNAPI_PROC(size_t,SDL_GetSIMDAlignment,(void),(),return)
+SDL_DYNAPI_PROC(SDL_Scancode,SDL_GetScancodeFromKey,(SDL_Keycode a, SDL_Keymod *b),(a,b),return)
SDL_DYNAPI_PROC(SDL_Scancode,SDL_GetScancodeFromName,(const char *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetScancodeName,(SDL_Scancode a),(a),return)
SDL_DYNAPI_PROC(Uint32,SDL_GetSemaphoreValue,(SDL_Semaphore *a),(a),return)
diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c
index c6d73d5d00348..ad4bd92010574 100644
--- a/src/events/SDL_keyboard.c
+++ b/src/events/SDL_keyboard.c
@@ -453,11 +453,11 @@ static SDL_Keycode SDL_ConvertNumpadKeycode(SDL_Keycode keycode, SDL_bool numloc
SDL_Keycode SDL_GetKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate, SDL_bool key_event)
{
SDL_Keyboard *keyboard = &SDL_keyboard;
- SDL_Keycode keycode;
if (key_event) {
SDL_Keymap *keymap = SDL_GetCurrentKeymap();
SDL_bool numlock = (modstate & SDL_KMOD_NUM) != 0;
+ SDL_Keycode keycode;
// We won't be applying any modifiers by default
modstate = SDL_KMOD_NONE;
@@ -474,13 +474,17 @@ SDL_Keycode SDL_GetKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate, S
if (keyboard->keycode_options & KEYCODE_OPTION_HIDE_NUMPAD) {
keycode = SDL_ConvertNumpadKeycode(keycode, numlock);
}
- } else {
- // Use the real keymap
- SDL_Keymap *keymap = keyboard->keymap;
-
- keycode = SDL_GetKeymapKeycode(keymap, scancode, modstate);
+ return keycode;
}
- return keycode;
+
+ return SDL_GetKeymapKeycode(keyboard->keymap, scancode, modstate);
+}
+
+SDL_Scancode SDL_GetScancodeFromKey(SDL_Keycode key, SDL_Keymod *modstate)
+{
+ SDL_Keyboard *keyboard = &SDL_keyboard;
+
+ return SDL_GetKeymapScancode(keyboard->keymap, key, modstate);
}
static int SDL_SendKeyboardKeyInternal(Uint64 timestamp, Uint32 flags, SDL_KeyboardID keyboardID, int rawcode, SDL_Scancode scancode, Uint8 state)