SDL: Only pass keypresses up the responder chain when text input is active (39781)

From 39781eef094dcb0e7969cecae17673bb3708faa1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 2 Aug 2023 00:19:57 -0700
Subject: [PATCH] Only pass keypresses up the responder chain when text input
 is active

This is another attempt to make sure we don't cause beeps from unhandled key presses while still allowing full text input functionalty.

If this isn't selective enough, we might need to go up the responder chain to see what's going to handle the event before passing it along.

Fixes https://github.com/libsdl-org/SDL/pull/6962

(cherry picked from commit a8abe612ed5597cbaabb903c0121b5b48baee91d)
(cherry picked from commit 335e9c769a5d49fe023b528c48a45c8ce3ed60d0)
---
 src/video/uikit/SDL_uikitview.m | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m
index c35864897bc9..dab0e54cdf07 100644
--- a/src/video/uikit/SDL_uikitview.m
+++ b/src/video/uikit/SDL_uikitview.m
@@ -412,7 +412,9 @@ - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)eve
             SDL_SendKeyboardKey(SDL_PRESSED, scancode);
         }
     }
-    [super pressesBegan:presses withEvent:event];
+    if (SDL_TextInputActive()) {
+        [super pressesBegan:presses withEvent:event];
+    }
 }
 
 - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
@@ -423,7 +425,9 @@ - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)eve
             SDL_SendKeyboardKey(SDL_RELEASED, scancode);
         }
     }
-    [super pressesEnded:presses withEvent:event];
+    if (SDL_TextInputActive()) {
+        [super pressesEnded:presses withEvent:event];
+    }
 }
 
 - (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
@@ -434,13 +438,17 @@ - (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *
             SDL_SendKeyboardKey(SDL_RELEASED, scancode);
         }
     }
-    [super pressesCancelled:presses withEvent:event];
+    if (SDL_TextInputActive()) {
+        [super pressesCancelled:presses withEvent:event];
+    }
 }
 
 - (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
 {
     /* This is only called when the force of a press changes. */
-    [super pressesChanged:presses withEvent:event];
+    if (SDL_TextInputActive()) {
+        [super pressesChanged:presses withEvent:event];
+    }
 }
 
 #endif /* TARGET_OS_TV || defined(__IPHONE_9_1) */