SDL/src/events
Sam Lantinga 195b8bd8ee Fixed bug 3249 - keysym.mod is incorrect when mod keys are pressed for SDL_KEYDOWN events
Adam M.

The keysym.mod field does not reflect the state of the modified keys when processing key down events for the modifier keys themselves. The documentation says that it returns the current key modifiers, but they are not current for key down events involving modifier keys. I interpret "current" to mean "equal to SDL_GetModState() at the instant the event is processed/enqueued".

For example, if you depress the Shift key you get a key down event with .mod == 0. However, .mod should not be 0 because a shift key is down. If you then release the Shift key, you get a key up event with .mod == 0. Neither event reports the modifier key.

If you press Shift and then A, .mod is incorrect (== 0) when Shift is pressed, but is correct later when A is pressed (== KMOD_LSHIFT).

You might say this behavior is deliberate, i.e. keysym.mod is the value /before/ the event, not the current value as documented, but that explanation is incorrect because only key down events behave that way. Key up events correctly give the current value, not the value before the event.

Not only is it inconsistent with itself, I think it makes keyboard processing harder.

The problem is near line 740 in SDL_keyboard.c:

if (SDL_KEYDOWN == type) {
    modstate = keyboard->modstate; // SHOULD THIS BE MOVED DOWN?
    switch (keycode) {
    case SDLK_NUMLOCKCLEAR:
        keyboard->modstate ^= KMOD_NUM;
        break;
    case SDLK_CAPSLOCK:
        keyboard->modstate ^= KMOD_CAPS;
        break;
    default:
        keyboard->modstate |= modifier;
        break;
    }
} else {
    keyboard->modstate &= ~modifier;
    modstate = keyboard->modstate;
}

In the key down path, modstate (and thus keysym.mod) ends up being the modifier state /before/ the event, but in the key up path modstate ends up being the modifier state /after/ the event. Personally I think the "modstate = keyboard->modstate" line should just be moved after the entire if/else statement, so that keysym.mod always reflects the current state.
2017-08-12 12:34:09 -07:00
..
blank_cursor.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
default_cursor.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
scancodes_darwin.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
scancodes_linux.h Fixed bug 3703 - Missing media keys support on Amazon Fire TV remote control 2017-07-20 10:46:38 -07:00
scancodes_windows.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
scancodes_xfree86.h Fixed bug 3703 - Missing media keys support on Amazon Fire TV remote control 2017-07-20 10:46:38 -07:00
SDL_clipboardevents.c Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_clipboardevents_c.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_dropevents.c Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_dropevents_c.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_events.c Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_events_c.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_gesture.c Removed newlines from error messages. 2017-03-26 21:00:19 +02:00
SDL_gesture_c.h Fixed compiler warning about redundant declaration. 2017-07-09 23:00:25 +02:00
SDL_keyboard.c Fixed bug 3249 - keysym.mod is incorrect when mod keys are pressed for SDL_KEYDOWN events 2017-08-12 12:34:09 -07:00
SDL_keyboard_c.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_mouse.c Added a hint SDL_HINT_TOUCH_MOUSE_EVENTS to control whether touch events generate synthetic mouse events. 2017-08-03 09:48:44 -07:00
SDL_mouse_c.h Added a hint SDL_HINT_TOUCH_MOUSE_EVENTS to control whether touch events generate synthetic mouse events. 2017-08-03 09:48:44 -07:00
SDL_quit.c Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_sysevents.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_touch.c Fixed bug 3583 - X11 touch device can be permanently lost 2017-02-11 11:14:48 -08:00
SDL_touch_c.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_windowevents.c Updated copyright for 2017 2017-01-01 18:33:28 -08:00
SDL_windowevents_c.h Updated copyright for 2017 2017-01-01 18:33:28 -08:00