From c84339752b83c274ff6f0a1657b998cc3726d9b1 Mon Sep 17 00:00:00 2001
From: james <justsomejames2@gmail.com>
Date: Fri, 18 Jan 2019 20:08:32 -0800
Subject: [PATCH] Set modifiers in a non-reactive manner

Original version, <https://git.magicalgirl.moe/james/SRB2/commit/356249dade14c25a0404e75fd5eacd61acbed443>.
---
 src/console.c     | 10 ----------
 src/d_main.c      | 30 ------------------------------
 src/hu_stuff.c    | 10 ----------
 src/sdl/i_video.c | 14 ++++++++++++++
 4 files changed, 14 insertions(+), 50 deletions(-)

diff --git a/src/console.c b/src/console.c
index 54c708415..eb9fdb6c3 100644
--- a/src/console.c
+++ b/src/console.c
@@ -1088,16 +1088,6 @@ boolean CON_Responder(event_t *ev)
 	else if (key == KEY_KPADSLASH)
 		key = '/';
 
-	// capslock
-	if (key == KEY_CAPSLOCK)	// it's a toggle.
-	{
-		if (capslock)
-			capslock = false;
-		else
-			capslock = true;
-		return true;
-	}
-
 	// same capslock code as hu_stuff.c's HU_responder. Check there for details.
 	if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z'))
 	{
diff --git a/src/d_main.c b/src/d_main.c
index 018390144..5aac12884 100644
--- a/src/d_main.c
+++ b/src/d_main.c
@@ -189,33 +189,6 @@ UINT8 shiftdown = 0; // 0x1 left, 0x2 right
 UINT8 ctrldown = 0; // 0x1 left, 0x2 right
 UINT8 altdown = 0; // 0x1 left, 0x2 right
 boolean capslock = 0; // jeez i wonder what this does.
-//
-// D_ModifierKeyResponder
-// Sets global shift/ctrl/alt variables, never actually eats events
-//
-static inline void D_ModifierKeyResponder(event_t *ev)
-{
-	if (ev->type == ev_keydown || ev->type == ev_console) switch (ev->data1)
-	{
-		case KEY_LSHIFT: shiftdown |= 0x1; return;
-		case KEY_RSHIFT: shiftdown |= 0x2; return;
-		case KEY_LCTRL: ctrldown |= 0x1; return;
-		case KEY_RCTRL: ctrldown |= 0x2; return;
-		case KEY_LALT: altdown |= 0x1; return;
-		case KEY_RALT: altdown |= 0x2; return;
-		default: return;
-	}
-	else if (ev->type == ev_keyup) switch (ev->data1)
-	{
-		case KEY_LSHIFT: shiftdown &= ~0x1; return;
-		case KEY_RSHIFT: shiftdown &= ~0x2; return;
-		case KEY_LCTRL: ctrldown &= ~0x1; return;
-		case KEY_RCTRL: ctrldown &= ~0x2; return;
-		case KEY_LALT: altdown &= ~0x1; return;
-		case KEY_RALT: altdown &= ~0x2; return;
-		default: return;
-	}
-}
 
 //
 // D_ProcessEvents
@@ -229,9 +202,6 @@ void D_ProcessEvents(void)
 	{
 		ev = &events[eventtail];
 
-		// Set global shift/ctrl/alt down variables
-		D_ModifierKeyResponder(ev); // never eats events
-
 		// Screenshots over everything so that they can be taken anywhere.
 		if (M_ScreenshotResponder(ev))
 			continue; // ate the event
diff --git a/src/hu_stuff.c b/src/hu_stuff.c
index 88027c66a..f0689e81e 100644
--- a/src/hu_stuff.c
+++ b/src/hu_stuff.c
@@ -1076,16 +1076,6 @@ boolean HU_Responder(event_t *ev)
 
 	c = (INT32)ev->data1;
 
-	// capslock (now handled outside of chat on so that it works everytime......)
-	if (c && c == KEY_CAPSLOCK)	// it's a toggle.
-	{
-		if (capslock)
-			capslock = false;
-		else
-			capslock = true;
-		return true;
-	}
-
 	if (!chat_on)
 	{
 		// enter chat mode
diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c
index b3587601b..0e0ad3397 100644
--- a/src/sdl/i_video.c
+++ b/src/sdl/i_video.c
@@ -960,6 +960,8 @@ void I_StartupMouse(void)
 //
 void I_OsPolling(void)
 {
+	SDL_Keymod mod;
+
 	if (consolevent)
 		I_GetConsoleEvents();
 	if (SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK)
@@ -974,6 +976,18 @@ void I_OsPolling(void)
 	I_GetMouseEvents();
 
 	I_GetEvent();
+
+	mod = SDL_GetModState();
+	/* Handle here so that our state is always synched with the system. */
+	shiftdown = ctrldown = altdown = 0;
+	capslock = false;
+	if (mod & KMOD_LSHIFT) shiftdown |= 1;
+	if (mod & KMOD_RSHIFT) shiftdown |= 2;
+	if (mod & KMOD_LCTRL)   ctrldown |= 1;
+	if (mod & KMOD_RCTRL)   ctrldown |= 2;
+	if (mod & KMOD_LALT)     altdown |= 1;
+	if (mod & KMOD_RALT)     altdown |= 2;
+	if (mod & KMOD_CAPS) capslock = true;
 }
 
 //
-- 
GitLab