Patches #1982

darkplaces:terencehill/keys_fixes: a M merge request: Some fixes to the keyboard handler: ------- WGL ------- * Keyboard h...

Added by git-manager 3 months ago. Updated 3 months ago.

Status:ResolvedStart date:06/02/2014
Priority:NormalDue date:
Assignee:-% Done:

100%

Category:-
Target version:-

Description

Some fixes to the keyboard handler:

------- WGL -------
* Keyboard handler now returns Unicode chars
* Fix numlock key interpreted as pause key
* Fix numlock, scrollock and capslock binds not working
* When numlock is off numeric keypad keys now return K_END, K_DOWNARROW, etc... Previously they always returned K_KP_<num> 
regardless of numlock state

------- SDL -------
* Simulate press and release events when numlock and capslock get pressed, so that they can work similarly to the other keys 
(excluding key repetition, it doesn't and will never work). It's a workaround for the default SDL behaviour with these keys: 
it generate a press event when a key gets enabled, a release event when the key gets disabled. Scrollock already works as a 
normal key.
* When numlock is off numeric keypad keys now return K_END, K_DOWNARROW, etc... Previously they always returned K_KP_<num> 
regardless of numlock state

------- XGL -------
* Add numlock, scrollock and capslock keys

Repository: xonotic/darkplaces.git
Commit: 6726d487a6f5613a8b8ebb992f1492230c8f069f
Branch: terencehill/keys_fixes

Merge commands:

cd darkplaces
git checkout master
git reset --hard origin/master
git pull && git diff '6726d487a6f5613a8b8ebb992f1492230c8f069f'..'origin/terencehill/keys_fixes'

# please check that the diff you just saw did not contain anything complex that
# needs a new merge request, and review these changes

git reset --hard 'origin/terencehill/keys_fixes'
git rebase -i --onto origin/master origin/master

# please make sure that this rebase did not cause any conflicts; if it did, fix
# them and repeatedly use git rebase --continue till the rebase is complete
#
# also, THIS is the point to do final pre-merge testing
#
# use git rebase --abort; git reset --hard 'origin/terencehill/keys_fixes' to bail out

git svn dcommit --add-author-from && git push --delete origin 'terencehill/keys_fixes'

Diffstat:

 dpdefs/keycodes.qc |    2 +-
 keys.c             |    5 -----
 vid_glx.c          |    4 ++++
 vid_sdl.c          |   29 +++++++++++++++++++----------
 vid_wgl.c          |   43 ++++++++++++++++++++++++-------------------
 5 files changed, 48 insertions(+), 35 deletions(-)

Revision log:

commit 6726d487a6f5613a8b8ebb992f1492230c8f069f
Merge: 9370453 ef5c86f
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    Merge branch 'master' into terencehill/keys_fixes

commit 937045369d5e59b34ebfe90daff6a9651fea16b9
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    GLX: Add numlock, scrollock and capslock keys

    Signed-off-by: terencehill <piuntn@gmail.com>

commit 738e3cb627cd41e428a733343a0e2cf4da52cd2f
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    SDL and WGL: when numlock is off numeric keypad keys now return K_END, K_DOWNARROW, etc... Previously they always returned K_KP_<num> regardless of numlock state

    Signed-off-by: terencehill <piuntn@gmail.com>

commit 7c56108c604f62bda49992d337c9df6544578762
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    SDL: Simulate press and release events when numlock or capslock gets pressed so that they can be used like other keys

commit 8b7797bd35af056174d26da862f5e994064b4181
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    Replace a few remaining numbers with constant names in the scantokey array (K_TAB, K_ENTER, K_ESCAPE)

    Signed-off-by: terencehill <piuntn@gmail.com>

commit 873a2f133d5048ec2050ce03c3759f70402a7d4c
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    WGL: make work capslock and scrolllock keys

    Signed-off-by: terencehill <piuntn@gmail.com>

commit 9874e2fe5e264f456b3612f10af08bf5f3db4cdd
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    WGL: Fix numlock key interpreted as pause key

    Signed-off-by: terencehill <piuntn@gmail.com>

commit 9d55319303319d31efb09cef32f9c5ec9131e5b9
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    Get rid of some warnings

    Signed-off-by: terencehill <piuntn@gmail.com>

commit 1d2dd25ae135aaccad4b21a1b4d5562b1d915c10
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    WGL keyboard handler: return Unicode chars

    Signed-off-by: terencehill <piuntn@gmail.com>

commit 921833dccb74b464834ff6b4bebfbf45fb0b5e43
Author: terencehill <piuntn@gmail.com>
Commit: terencehill <piuntn@gmail.com>

    K_SCROLLLOCK --> K_SCROLLOCK (it's spelled this way everywhere else)

    Signed-off-by: terencehill <piuntn@gmail.com>

User agreed to the license.

Diff:

diff --git a/dpdefs/keycodes.qc b/dpdefs/keycodes.qc
index 65fe815..7752f50 100644
--- a/dpdefs/keycodes.qc
+++ b/dpdefs/keycodes.qc
@@ -43,7 +43,7 @@ float K_END        =    152;

 float K_NUMLOCK        = 154;
 float K_CAPSLOCK    = 155;
-float K_SCROLLLOCK    = 156;
+float K_SCROLLOCK    = 156;

 float K_KP_0    =    157;
 float K_KP_INS    =    157; // same as K_KP_0
diff --git a/keys.c b/keys.c
index 45d2daf..3264856 100644
--- a/keys.c
+++ b/keys.c
@@ -1766,11 +1766,6 @@ Key_Event (int key, int ascii, qboolean down)
         return;
     }

-    if (ascii == 0x80 && utf8_enable.integer) // pressing AltGr-5 (or AltGr-e) and for some reason we get windows-1252 encoding?
-        ascii = 0x20AC; // we want the Euro currency sign
-        // TODO find out which vid_ drivers do it and fix it there
-        // but catching U+0080 here is no loss as that char is not useful anyway
-
     // get key binding
     bind = keybindings[key_bmap][key];
     if (!bind)
diff --git a/vid_glx.c b/vid_glx.c
index 9e33fdc..8327b26 100644
--- a/vid_glx.c
+++ b/vid_glx.c
@@ -284,6 +284,10 @@ static int XLateKey(XKeyEvent *ev, Uchar *ascii)
         case XK_KP_Subtract: key = K_KP_MINUS; break;
         case XK_KP_Divide: key = K_KP_SLASH; break;

+        case XK_Num_Lock: key = K_NUMLOCK; break;
+        case XK_Caps_Lock: key = K_CAPSLOCK; break;
+        case XK_Scroll_Lock: key = K_SCROLLOCK; break;
+
         case XK_asciicircum:    *ascii = key = '^'; break; // for some reason, XLookupString returns "" on this one for Grunt|2

         case XK_section:    *ascii = key = '~'; break;
diff --git a/vid_sdl.c b/vid_sdl.c
index 003f1ba..d632a2b 100644
--- a/vid_sdl.c
+++ b/vid_sdl.c
@@ -231,18 +231,18 @@ static int MapKey( unsigned int sdlkey )
     case SDLK_KP_PLUS:            return K_KP_PLUS;
     case SDLK_KP_ENTER:           return K_KP_ENTER;
 #if SDL_MAJOR_VERSION == 1
-    case SDLK_KP_1:               return K_KP_1;
-    case SDLK_KP_2:               return K_KP_2;
-    case SDLK_KP_3:               return K_KP_3;
-    case SDLK_KP_4:               return K_KP_4;
+    case SDLK_KP_1:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_1 : K_END);
+    case SDLK_KP_2:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_2 : K_DOWNARROW);
+    case SDLK_KP_3:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_3 : K_PGDN);
+    case SDLK_KP_4:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_4 : K_LEFTARROW);
     case SDLK_KP_5:               return K_KP_5;
-    case SDLK_KP_6:               return K_KP_6;
-    case SDLK_KP_7:               return K_KP_7;
-    case SDLK_KP_8:               return K_KP_8;
-    case SDLK_KP_9:               return K_KP_9;
-    case SDLK_KP_0:               return K_KP_0;
+    case SDLK_KP_6:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_6 : K_RIGHTARROW);
+    case SDLK_KP_7:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_7 : K_HOME);
+    case SDLK_KP_8:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_8 : K_UPARROW);
+    case SDLK_KP_9:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_9 : K_PGUP);
+    case SDLK_KP_0:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_0 : K_INS);
 #endif
-    case SDLK_KP_PERIOD:          return K_KP_PERIOD;
+    case SDLK_KP_PERIOD:          return ((SDL_GetModState() & KMOD_NUM) ? K_KP_PERIOD : K_DEL);
 //    case SDLK_APPLICATION:        return K_APPLICATION;
 //    case SDLK_POWER:              return K_POWER;
     case SDLK_KP_EQUALS:          return K_KP_EQUALS;
@@ -1113,7 +1113,16 @@ void Sys_SendKeyEvents( void )
             case SDL_KEYUP:
                 keycode = MapKey(event.key.keysym.sym);
                 if (!VID_JoyBlockEmulatedKeys(keycode))
+                {
+                    if(keycode == K_NUMLOCK || keycode == K_CAPSLOCK)
+                    {
+                        // simulate down followed by up
+                        Key_Event(keycode, event.key.keysym.unicode, true);
+                        Key_Event(keycode, event.key.keysym.unicode, false);
+                        break;
+                    }
                     Key_Event(keycode, event.key.keysym.unicode, (event.key.state == SDL_PRESSED));
+                }
                 break;
             case SDL_ACTIVEEVENT:
                 if( event.active.state & SDL_APPACTIVE )
diff --git a/vid_wgl.c b/vid_wgl.c
index 4ff183e..544d24b 100644
--- a/vid_wgl.c
+++ b/vid_wgl.c
@@ -153,10 +153,11 @@ static qboolean vid_isfullscreen;
 //void VID_MenuDraw (void);
 //void VID_MenuKey (int key);

-//LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
-//void AppActivate(BOOL fActive, BOOL minimize);
-//void ClearAllStates (void);
-//void VID_UpdateWindowStatus (void);
+LONG WINAPI MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
+void AppActivate(BOOL fActive, BOOL minimize);
+static void ClearAllStates(void);
+qboolean VID_InitModeGL(viddef_mode_t *mode);
+qboolean VID_InitModeSOFT(viddef_mode_t *mode);

 //====================================

@@ -326,17 +327,15 @@ void VID_Finish (void)
 //==========================================================================

-
-
 static unsigned char scantokey[128] =
 {
 //  0           1       2    3     4     5       6       7      8         9      A          B           C       D           E           F
-    0          ,27    ,'1'  ,'2'  ,'3'  ,'4'    ,'5'    ,'6'   ,'7'      ,'8'   ,'9'       ,'0'        ,'-'   ,'='         ,K_BACKSPACE,9    ,//0
-    'q'        ,'w'   ,'e'  ,'r'  ,'t'  ,'y'    ,'u'    ,'i'   ,'o'      ,'p'   ,'['       ,']'        ,13    ,K_CTRL      ,'a'        ,'s'  ,//1
+    0          ,K_ESCAPE,'1'  ,'2'  ,'3'  ,'4'    ,'5'        ,'6'   ,'7'      ,'8'   ,'9'       ,'0'        ,'-'    ,'='         ,K_BACKSPACE,K_TAB,//0
+    'q'        ,'w'     ,'e'  ,'r'  ,'t'  ,'y'    ,'u'        ,'i'   ,'o'      ,'p'   ,'['       ,']'        ,K_ENTER,K_CTRL      ,'a'        ,'s'  ,//1
     'd'        ,'f'   ,'g'  ,'h'  ,'j'  ,'k'    ,'l'    ,';'   ,'\''     ,'`'   ,K_SHIFT   ,'\\'       ,'z'   ,'x'         ,'c'        ,'v'  ,//2
-    'b'        ,'n'   ,'m'  ,','  ,'.'  ,'/'    ,K_SHIFT,'*'   ,K_ALT    ,' '   ,0         ,K_F1       ,K_F2  ,K_F3        ,K_F4       ,K_F5 ,//3
-    K_F6       ,K_F7  ,K_F8 ,K_F9 ,K_F10,K_PAUSE,0      ,K_HOME,K_UPARROW,K_PGUP,K_KP_MINUS,K_LEFTARROW,K_KP_5,K_RIGHTARROW,K_KP_PLUS  ,K_END,//4
-    K_DOWNARROW,K_PGDN,K_INS,K_DEL,0    ,0      ,0      ,K_F11 ,K_F12    ,0     ,0         ,0          ,0     ,0           ,0          ,0    ,//5
+    'b'        ,'n'     ,'m'  ,','  ,'.'  ,'/'    ,K_SHIFT    ,'*'   ,K_ALT    ,' '   ,K_CAPSLOCK,K_F1       ,K_F2   ,K_F3        ,K_F4       ,K_F5 ,//3
+    K_F6       ,K_F7    ,K_F8 ,K_F9 ,K_F10,K_PAUSE,K_SCROLLOCK,K_HOME,K_UPARROW,K_PGUP,K_KP_MINUS,K_LEFTARROW,K_KP_5 ,K_RIGHTARROW,K_KP_PLUS  ,K_END,//4
+    K_DOWNARROW,K_PGDN  ,K_INS,K_DEL,0    ,0      ,0          ,K_F11 ,K_F12    ,0     ,0         ,0          ,0      ,0           ,0          ,0    ,//5
     0          ,0     ,0    ,0    ,0    ,0      ,0      ,0     ,0        ,0     ,0         ,0          ,0     ,0           ,0          ,0    ,//6
     0          ,0     ,0    ,0    ,0    ,0      ,0      ,0     ,0        ,0     ,0         ,0          ,0     ,0           ,0          ,0     //7
 };
@@ -368,6 +367,9 @@ static int MapKey (int key, int virtualkey)

     if ( !is_extended )
     {
+        if(((GetKeyState(VK_NUMLOCK)) & 0xffff) == 0)
+            return result;
+
         switch ( result )
         {
         case K_HOME:
@@ -396,6 +398,9 @@ static int MapKey (int key, int virtualkey)
     }
     else
     {
+        if(virtualkey == VK_NUMLOCK)
+            return K_NUMLOCK;
+
         switch ( result )
         {
         case 0x0D:
@@ -542,7 +547,8 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM lParam)
     LONG    lRet = 1;
     int        fActive, fMinimized, temp;
     unsigned char state[256];
-    unsigned char asciichar[4];
+    const unsigned int UNICODE_BUFFER_LENGTH = 4;
+    WCHAR unicode[UNICODE_BUFFER_LENGTH];
     int        vkey;
     int        charlength;
     qboolean down = false;
@@ -575,14 +581,13 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM lParam)
             GetKeyboardState (state);
             // alt/ctrl/shift tend to produce funky ToAscii values,
             // and if it's not a single character we don't know care about it
-            charlength = ToAscii (wParam, lParam >> 16, state, (LPWORD)asciichar, 0);
-            if (vkey == K_ALT || vkey == K_CTRL || vkey == K_SHIFT || charlength == 0)
-                asciichar[0] = 0;
-            else if( charlength == 2 ) {
-                asciichar[0] = asciichar[1];
-            }
+            charlength = ToUnicode(wParam, lParam >> 16, state, unicode, UNICODE_BUFFER_LENGTH, 0);
+            if(vkey == K_ALT || vkey == K_CTRL || vkey == K_SHIFT || charlength == 0)
+                unicode[0] = 0;
+            else if(charlength == 2)
+                unicode[0] = unicode[1];
             if (!VID_JoyBlockEmulatedKeys(vkey))
-                Key_Event (vkey, asciichar[0], down);
+                Key_Event(vkey, unicode[0], down);
             break;

         case WM_SYSCHAR:

History

#1 Updated by terencehill 3 months ago

I can't compile GLX so "GLX: Add numlock, scrollock and capslock keys" is untested but it's likely to work.

If anybody is so kind to test it: git checkout terencehill/key_fixes in DarkPlaces, start a local game, execute this script in the console

alias +key_state_test "echo $* ^2ON" 
alias -key_state_test "echo $* ^1OFF" 
bind numlock "+key_state_test numlock" 
bind capslock "+key_state_test capslock" 
bind scrollock "+key_state_test scrollock"

return to the game and press in sequence numlock, scrollock and capslock keys.
The expected output in the console should be: numlock on, numlock off, capslock on, capslock off, scrollock on, scrollock off.

#2 Updated by divVerent 3 months ago

  • Status changed from New to Resolved
  • % Done changed from 0 to 100

Also available in: Atom PDF