Re: [AD] Keyboard layout detection in Windows

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Tuesday 23 November 2004 10:45, Evert Glebbeek wrote:
> The attached patch is an attempt to merge Lennart Steinke's keyboard 
layout
> detection code for Windows with Allegro.

Oops, forgot the attachment...

Evert
Index: include/allegro/internal/aintern.h
===================================================================
RCS file: /cvsroot/alleg/allegro/include/allegro/internal/aintern.h,v
retrieving revision 1.21
diff -u -r1.21 aintern.h
--- include/allegro/internal/aintern.h	9 Nov 2004 18:50:18 -0000	1.21
+++ include/allegro/internal/aintern.h	23 Nov 2004 09:37:14 -0000
@@ -192,6 +192,8 @@
 
 AL_VAR(int, _key_standard_kb);
 
+AL_VAR(char *, _keyboard_layout);
+
 
 /* various bits of joystick stuff */
 AL_VAR(int, _joy_type);
Index: src/misc/pckeys.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/misc/pckeys.c,v
retrieving revision 1.16
diff -u -r1.16 pckeys.c
--- src/misc/pckeys.c	16 Feb 2004 04:43:40 -0000	1.16
+++ src/misc/pckeys.c	23 Nov 2004 09:37:17 -0000
@@ -62,7 +62,7 @@
 
 int _key_standard_kb = TRUE;
 
-
+char *_keyboard_layout = NULL;
 
 /* lookup table for converting hardware scancodes into Allegro format */
 static unsigned char hw_to_mycode[128] =
@@ -627,7 +627,7 @@
    char filename[1024], tmp1[128], tmp2[128], *ext, *datafile;
    AL_CONST char* name;
 
-   name = get_config_string(uconvert_ascii("system", tmp1), uconvert_ascii("keyboard", tmp2), NULL);
+   name = get_config_string(uconvert_ascii("system", tmp1), uconvert_ascii("keyboard", tmp2), _keyboard_layout);
 
    if ((!name) || (!ugetc(name)))
       return;
Index: src/win/wkeybd.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/win/wkeybd.c,v
retrieving revision 1.25
diff -u -r1.25 wkeybd.c
--- src/win/wkeybd.c	3 Oct 2004 10:58:00 -0000	1.25
+++ src/win/wkeybd.c	23 Nov 2004 09:37:18 -0000
@@ -60,6 +60,37 @@
    _pckey_scancode_to_ascii
 };
 
+/* Map Windows keyboard IDs to Allegro ID strings */
+typedef struct {
+    unsigned int code;
+    char* name;
+} language_map;
+
+static language_map language_map[] = {
+    { 0x0813, "BE" },
+    { 0x0416, "BR" },
+    { 0x1009, "CF" },
+    { 0x1009, "CF" },
+    { 0x0807, "CH" },
+    { 0x0405, "CZ" },
+    { 0x0407, "DE" },
+    { 0x0406, "DK" },
+    { 0x040a, "ES" },
+    { 0x040b, "FI" },
+    { 0x040c, "FR" },
+    { 0x0410, "IT" },
+    { 0x0414, "NO" },
+    { 0x0415, "PL" },
+    { 0x0416, "PT" },
+    { 0x0816, "PT" },
+    { 0x0419, "RU" },
+    { 0x041d, "SE" },
+    { 0x041b, "SK" },
+    { 0x0424, "SK" },
+    { 0x0809, "UK" },
+    { 0x0409, "US" },
+    { 0, NULL }    
+};
 
 #define DINPUT_BUFFERSIZE 256
 static HANDLE key_input_event = NULL;
@@ -396,6 +427,22 @@
  */
 static int key_directx_init(void)
 {
+   char buffer[KL_NAMELENGTH+1];
+   unsigned int lang_id;
+   int i;
+   
+   /* Detect default keyboard layout */
+   if (GetKeyboardLayoutName(buffer)) {
+      lang_id = strtol(buffer, NULL, 16);
+      lang_id &= 0xffff;
+      for (i=0; language_map[i].code; i++) {
+         if (language_map[i].code == lang_id) {
+            _keyboard_layout = language_map[i].name;
+            break;
+         }
+      }
+   }
+   
    /* Because DirectInput uses the same scancodes as the pc keyboard
     * controller, the DirectX keyboard driver passes them into the
     * pckeys translation routines.
@@ -431,6 +478,7 @@
       /* now we can free all resources */
       CloseHandle(key_input_processed_event);
    }
+   _keyboard_layout = NULL;
 }
 
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/