From 9f25821ffdde1e39a49d5ae5defb634a83aadcdc Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 15 Jan 2025 07:39:05 -0800
Subject: [PATCH] Fixed detection of function keys on Emscripten
Fixes https://github.com/libsdl-org/SDL/issues/11973
(cherry picked from commit 67382e9c839a41188dc06e0b8089445123453611)
---
src/video/emscripten/SDL_emscriptenevents.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/src/video/emscripten/SDL_emscriptenevents.c b/src/video/emscripten/SDL_emscriptenevents.c
index 4f1cc43ae0911..4ce3c8adee82e 100644
--- a/src/video/emscripten/SDL_emscriptenevents.c
+++ b/src/video/emscripten/SDL_emscriptenevents.c
@@ -794,6 +794,20 @@ static EM_BOOL Emscripten_HandleTouch(int eventType, const EmscriptenTouchEvent
return preventDefault;
}
+static bool IsFunctionKey(SDL_Scancode scancode)
+{
+ if (scancode >= SDL_SCANCODE_F1 && scancode <= SDL_SCANCODE_F12) {
+ return true;
+ }
+ if (scancode >= SDL_SCANCODE_F13 && scancode <= SDL_SCANCODE_F24) {
+ return true;
+ }
+ return false;
+}
+
+/* This is a great tool to see web keyboard events live:
+ * https://w3c.github.io/uievents/tools/key-event-viewer.html
+ */
static EM_BOOL Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
{
const SDL_Keycode keycode = Emscripten_MapKeyCode(keyEvent);
@@ -823,7 +837,7 @@ static EM_BOOL Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent
(scancode == SDL_SCANCODE_UP) ||
(scancode == SDL_SCANCODE_RIGHT) ||
(scancode == SDL_SCANCODE_DOWN) ||
- ((scancode >= SDL_SCANCODE_F1) && (scancode <= SDL_SCANCODE_F15)) ||
+ IsFunctionKey(scancode) ||
keyEvent->ctrlKey) {
is_nav_key = SDL_TRUE;
}