Skip to content

Commit

Permalink
Sync up the caps/numlock state properly without sending key events.
Browse files Browse the repository at this point in the history
Partially fixes Bugzilla #2736 and #3125.
  • Loading branch information
icculus committed Dec 28, 2015
1 parent d3b323f commit 257b7af
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 45 deletions.
13 changes: 13 additions & 0 deletions src/events/SDL_keyboard.c
Expand Up @@ -845,6 +845,19 @@ SDL_SetModState(SDL_Keymod modstate)
keyboard->modstate = modstate;
}

/* Note that SDL_ToggleModState() is not a public API. SDL_SetModState() is. */
void
SDL_ToggleModState(const SDL_Keymod modstate, const SDL_bool toggle)
{
SDL_Keyboard *keyboard = &SDL_keyboard;
if (toggle) {
keyboard->modstate |= modstate;
} else {
keyboard->modstate &= ~modstate;
}
}


SDL_Keycode
SDL_GetKeyFromScancode(SDL_Scancode scancode)
{
Expand Down
3 changes: 3 additions & 0 deletions src/events/SDL_keyboard_c.h
Expand Up @@ -62,6 +62,9 @@ extern void SDL_KeyboardQuit(void);
/* Convert to UTF-8 */
extern char *SDL_UCS4ToUTF8(Uint32 ch, char *dst);

/* Toggle on or off pieces of the keyboard mod state. */
extern void SDL_ToggleModState(const SDL_Keymod modstate, const SDL_bool toggle);

#endif /* _SDL_keyboard_c_h */

/* vi: set ts=4 sw=4 expandtab: */
8 changes: 2 additions & 6 deletions src/video/cocoa/SDL_cocoakeyboard.m
Expand Up @@ -341,8 +341,7 @@ - (NSArray *)validAttributesForMarkedText
newMask = newMods & NSAlphaShiftKeyMask;

if (oldMask != newMask) {
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
SDL_ToggleModState(KMOD_CAPS, newMask != 0);
}
}

Expand Down Expand Up @@ -501,10 +500,7 @@ - (NSArray *)validAttributesForMarkedText
/* On pre-10.6, you might have the initial capslock key state wrong. */
if (floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_6) {
data->modifierFlags = [NSEvent modifierFlags];
if (data->modifierFlags & NSAlphaShiftKeyMask) {
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
}
SDL_ToggleModState(KMOD_CAPS, (data->modifierFlags & NSAlphaShiftKeyMask) != 0);
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -589,15 +589,11 @@ - (void)windowDidBecomeKey:(NSNotification *)aNotification
[NSMenu setMenuBarVisible:NO];
}

/* On pre-10.6, you might have the capslock key state wrong now. */
/* On pre-10.6, you might have the capslock key state wrong now because we can't check here. */
if (floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_6) {
const unsigned int oldflags = _data->videodata->modifierFlags & NSAlphaShiftKeyMask;
const unsigned int newflags = [NSEvent modifierFlags] & NSAlphaShiftKeyMask;
if (oldflags != newflags) {
_data->videodata->modifierFlags = (_data->videodata->modifierFlags & ~NSAlphaShiftKeyMask) | newflags;
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
}
_data->videodata->modifierFlags = (_data->videodata->modifierFlags & ~NSAlphaShiftKeyMask) | newflags;
SDL_ToggleModState(KMOD_CAPS, newflags != 0);
}
}

Expand Down
14 changes: 2 additions & 12 deletions src/video/windows/SDL_windowskeyboard.c
Expand Up @@ -104,18 +104,8 @@ WIN_InitKeyboard(_THIS)
SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");

/* Are system caps/num/scroll lock active? Set our state to match. */
if (GetKeyState(VK_CAPITAL) & 0x0001) {
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
}
if (GetKeyState(VK_NUMLOCK) & 0x0001) {
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR);
}
if (GetKeyState(VK_SCROLL) & 0x0001) {
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_SCROLLLOCK);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_SCROLLLOCK);
}
SDL_ToggleModState(KMOD_CAPS, (GetKeyState(VK_CAPITAL) & 0x0001) != 0);
SDL_ToggleModState(KMOD_NUM, (GetKeyState(VK_NUMLOCK) & 0x0001) != 0);
}

void
Expand Down
23 changes: 3 additions & 20 deletions src/video/x11/SDL_x11events.c
Expand Up @@ -348,27 +348,10 @@ X11_ReconcileKeyboardState(_THIS)

X11_XQueryKeymap(display, keys);

/* Get the keyboard modifier state */
/* Sync up the keyboard modifier state */
if (X11_XQueryPointer(display, DefaultRootWindow(display), &junk_window, &junk_window, &x, &y, &x, &y, &mask)) {
unsigned num_mask = X11_GetNumLockModifierMask(_this);
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
Uint8 capslockState = keystate[SDL_SCANCODE_CAPSLOCK];
Uint8 numlockState = keystate[SDL_SCANCODE_NUMLOCKCLEAR];

/* Toggle key mod state if needed */
if (!!(mask & LockMask) != !!(SDL_GetModState() & KMOD_CAPS)) {
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
if (capslockState == SDL_RELEASED) {
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
}
}

if (!!(mask & num_mask) != !!(SDL_GetModState() & KMOD_NUM)) {
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR);
if (numlockState == SDL_RELEASED) {
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR);
}
}
SDL_ToggleModState(KMOD_CAPS, (mask & LockMask) != 0);
SDL_ToggleModState(KMOD_NUM, (mask & X11_GetNumLockModifierMask(_this)) != 0);
}

for (keycode = 0; keycode < 256; ++keycode) {
Expand Down

0 comments on commit 257b7af

Please sign in to comment.