From 2b0140a91f2ec32f674957068cdf7ead053e6e05 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 27 Oct 2015 11:17:32 -0700 Subject: [PATCH] Add a new SDL_KEYMAPCHANGED SDL event to abstract notification of keyboard layout or input language changes. --- include/SDL_events.h | 3 +++ src/events/SDL_events.c | 6 ++++++ src/events/SDL_events_c.h | 1 + src/video/cocoa/SDL_cocoakeyboard.m | 9 ++++++--- src/video/windows/SDL_windowsevents.c | 1 + src/video/x11/SDL_x11events.c | 2 ++ 6 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/SDL_events.h b/include/SDL_events.h index 2ccb10132..8a364c2ad 100644 --- a/include/SDL_events.h +++ b/include/SDL_events.h @@ -94,6 +94,9 @@ typedef enum SDL_KEYUP, /**< Key released */ SDL_TEXTEDITING, /**< Keyboard text editing (composition) */ SDL_TEXTINPUT, /**< Keyboard text input */ + SDL_KEYMAPCHANGED, /**< Keymap changed due to a system event such as an + input language or keyboard layout change. + */ /* Mouse events */ SDL_MOUSEMOTION = 0x400, /**< Mouse moved */ diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c index 4f5bb2d9a..513cb55ed 100644 --- a/src/events/SDL_events.c +++ b/src/events/SDL_events.c @@ -649,4 +649,10 @@ SDL_SendSysWMEvent(SDL_SysWMmsg * message) return (posted); } +int +SDL_SendKeymapChangedEvent(void) +{ + return SDL_SendAppEvent(SDL_KEYMAPCHANGED); +} + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/events/SDL_events_c.h b/src/events/SDL_events_c.h index ebd983d36..0ead5f304 100644 --- a/src/events/SDL_events_c.h +++ b/src/events/SDL_events_c.h @@ -38,6 +38,7 @@ extern void SDL_QuitInterrupt(void); extern int SDL_SendAppEvent(SDL_EventType eventType); extern int SDL_SendSysWMEvent(SDL_SysWMmsg * message); +extern int SDL_SendKeymapChangedEvent(void); extern int SDL_QuitInit(void); extern int SDL_SendQuit(void); diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m index 41ecf1f63..9abca976a 100644 --- a/src/video/cocoa/SDL_cocoakeyboard.m +++ b/src/video/cocoa/SDL_cocoakeyboard.m @@ -398,7 +398,7 @@ HandleModifiers(_THIS, unsigned short scancode, unsigned int modifierFlags) } static void -UpdateKeymap(SDL_VideoData *data) +UpdateKeymap(SDL_VideoData *data, SDL_bool send_event) { TISInputSourceRef key_layout; const void *chr_data; @@ -454,6 +454,9 @@ UpdateKeymap(SDL_VideoData *data) } } SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES); + if (send_event) { + SDL_SendKeymapChangedEvent(); + } return; } @@ -466,7 +469,7 @@ Cocoa_InitKeyboard(_THIS) { SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; - UpdateKeymap(data); + UpdateKeymap(data, SDL_FALSE); /* Set our own names for the platform-dependent but layout-independent keys */ /* This key is NumLock on the MacBook keyboard. :) */ @@ -564,7 +567,7 @@ Cocoa_HandleKeyEvent(_THIS, NSEvent *event) case NSKeyDown: if (![event isARepeat]) { /* See if we need to rebuild the keyboard layout */ - UpdateKeymap(data); + UpdateKeymap(data, SDL_TRUE); } SDL_SendKeyboardKey(SDL_PRESSED, code); diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c index bb99f2977..3ce8291df 100644 --- a/src/video/windows/SDL_windowsevents.c +++ b/src/video/windows/SDL_windowsevents.c @@ -626,6 +626,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) case WM_INPUTLANGCHANGE: { WIN_UpdateKeymap(); + SDL_SendKeymapChangedEvent(); } returnCode = 1; break; diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c index 7ec4edf75..2f730c440 100644 --- a/src/video/x11/SDL_x11events.c +++ b/src/video/x11/SDL_x11events.c @@ -625,6 +625,7 @@ X11_DispatchEvent(_THIS) } X11_UpdateKeymap(_this); + SDL_SendKeymapChangedEvent(); } return; } @@ -1143,6 +1144,7 @@ X11_DispatchEvent(_THIS) notice and reinit our keymap here. This might not be the right approach, but it seems to work. */ X11_UpdateKeymap(_this); + SDL_SendKeymapChangedEvent(); } } break;