SDL2 - "SDL_GetModState(); // Windows 10 GCC v8.1.0 Warning?"

Hi,

Working on an older SDL2 project now.
Using Windows 10 with newest Code::Blocks with GCC 8.1.0.

KeyModState = SDL_GetModState();
if (KeyModState && KMOD_LSHIFT)  ShiftKeyPressed = true;
else if (KeyModState && KMOD_RSHIFT)  ShiftKeyPressed = true;
else  ShiftKeyPressed = false;

||=== Build: Release in TC4T-WinLinux-Retail2 (compiler: GNU GCC Compiler) ===|
warning: enum constant in boolean context [-Wint-in-bool-context]
(on first “else” above)

Above is the only warning on building.
Any ideas on how to fix this?
Thanks!

Jesse

You’re trying to use the returned data type from SDL_GetModState() as a bool, which is incorrect. The data type returned from that function is an SDL_Keymod, which is an enum containing bit flags.

In your code, you’re checking if KeyModState and KMOD_LSHIFT/KMOD_RSHIFT is both true.
What you probably want to do instead is using the AND operator (&) to check if the KMOD_LSHIFT or the KMOD_RSHIFT bit is set in KeyModState.

bool ShiftKeyPressed = false;

const SDL_Keymod KeyModState = SDL_GetModState();

	 if(KeyModState & KMOD_LSHIFT)	ShiftKeyPressed = true;
else if(KeyModState & KMOD_RSHIFT)	ShiftKeyPressed = true;
else								ShiftKeyPressed = false;

Note: Consider using the KMOD_SHIFT (along with other existing modifier key-) definitions, to check both the left and right ctrl/shift/alt key state(s).

bool CtrlKeyPressed		= false;
bool ShiftKeyPressed	= false;
bool AltKeyPressed		= false;

const SDL_Keymod KeyModState = SDL_GetModState();

if(KeyModState & KMOD_CTRL)		CtrlKeyPressed = true;
else							CtrlKeyPressed = false;

if(KeyModState & KMOD_SHIFT)	ShiftKeyPressed = true;
else							ShiftKeyPressed = false;

if(KeyModState & KMOD_ALT)		AltKeyPressed = true;
else							AltKeyPressed = false;

Yes, got it working, thank you!

Jesse