From 0cc0d8eb605fc772dec89d8abcfe6c66fa49d78b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <slouken@libsdl.org>
Date: Mon, 13 Apr 2009 08:42:09 +0000
Subject: [PATCH] Fixed bug #618

   Description From  Tim Angus   2008-08-30 12:23:56   (-) [reply]

As we all know SDL 1.2 doesn't handle dead keys well since one key press
potentially equals two (or more) characters. For example, on many layouts,
keying <backquote>,<space> results in <no character>,<backquote><space>. Since
the unicode member of the SDL_keysym struct only has room for one character,
only one can be returned.

On Linux, the first character is returned. On Windows however, unless the exact
number of characters generated by the keypress is 1, nothing is returned. The
following patch addresses this inconsistency.

Updated patch which includes a further fix to the handling of the numpad when
numlock is on. This further fix is courtesy Amanieu d'Antras.

--HG--
branch : SDL-1.2
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/branches/SDL-1.2%403578
---
 src/video/windib/SDL_dibevents.c | 8 +++++++-
 src/video/windx5/SDL_dx5events.c | 8 +++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/video/windib/SDL_dibevents.c b/src/video/windib/SDL_dibevents.c
index 53355edb..2fa967dc 100644
--- a/src/video/windib/SDL_dibevents.c
+++ b/src/video/windib/SDL_dibevents.c
@@ -552,7 +552,13 @@ static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym *keysym,
 		Uint16	wchars[2];
 
 		GetKeyboardState(keystate);
-		if (SDL_ToUnicode((UINT)vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) == 1)
+		/* Numlock isn't taken into account in ToUnicode,
+		 * so we handle it as a special case here */
+		if ((keystate[VK_NUMLOCK] & 1) && vkey >= VK_NUMPAD0 && vkey <= VK_NUMPAD9)
+		{
+			keysym->unicode = vkey - VK_NUMPAD0 + '0';
+		}
+		else if (SDL_ToUnicode((UINT)vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) > 0)
 		{
 			keysym->unicode = wchars[0];
 		}
diff --git a/src/video/windx5/SDL_dx5events.c b/src/video/windx5/SDL_dx5events.c
index 4ce1f14c..7e413e83 100644
--- a/src/video/windx5/SDL_dx5events.c
+++ b/src/video/windx5/SDL_dx5events.c
@@ -912,7 +912,13 @@ static SDL_keysym *TranslateKey(UINT scancode, SDL_keysym *keysym, int pressed)
 		keysym->unicode = vkey;
 #else
 		GetKeyboardState(keystate);
-		if (SDL_ToUnicode(vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) == 1)
+		/* Numlock isn't taken into account in ToUnicode,
+		 * so we handle it as a special case here */
+		if ((keystate[VK_NUMLOCK] & 1) && vkey >= VK_NUMPAD0 && vkey <= VK_NUMPAD9)
+		{
+			keysym->unicode = vkey - VK_NUMPAD0 + '0';
+		}
+		else if (SDL_ToUnicode(vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) > 0)
 		{
 			keysym->unicode = wchars[0];
 		}
-- 
2.18.1