Quick answer: Pygame's pygame.key.get_mods() returning Shift-held after Alt-Tab? OS doesn't send keyup when focus leaves - listen for WINDOWFOCUSLOST and clear modifier state manually.
Player Alt-Tabs while Shift is held. Comes back; every keypress is sprint-modified.
Clear on focus loss
if event.type == pygame.WINDOWFOCUSLOST:
pygame.key.set_mods(0)Resets the internal modifier mask. Next key event re-reads from a clean state.
Re-query on focus regain
On focus regain, query OS-level modifier state via pygame.key.get_mods(). The OS knows even if Pygame doesn't.
Track held keys per-frame
For exact correctness, track every keydown/keyup yourself. get_mods is a convenience that breaks under focus changes.
“Focus loss is an unreliable event boundary. Modifier state has to be resilient.”
If you're shipping a Pygame game, test the alt-tab cycle deliberately. The bugs only appear when focus moves and most testers don't switch windows.