Checking if the Shift or Alt is pressed.

I'm trying to check if a modifier has been press on the keyboard by itself
Here is my code..

if (al_key_down(&kbdState, ALLEGRO_KEYMOD_SHIFT)) keyshift = TRUE;
else keyshift = FALSE;
if (al_key_down(&kbdState, ALLEGRO_KEYMOD_ALT))  keyalt = TRUE;
else keyalt = FALSE;

If I did the following:

if (al_key_down(&kbdState, ALLEGRO_KEY_A)) keyshift = TRUE;

This works when I press down "A" - so do I need to do something different with modifiers?

Figured it out

if (al_key_down(&kbdState, ALLEGRO_KEY_LSHIFT) || (al_key_down(&kbdState, ALLEGRO_KEY_RSHIFT))) keyshift = TRUE;
else keyshift = FALSE;
if (al_key_down(&kbdState, ALLEGRO_KEY_ALT) || (al_key_down(&kbdState, ALLEGRO_KEY_ALTGR)))  keyalt = TRUE;
else keyalt = FALSE;


If that's your actual code, you can simplify to this:

bool keyshift = al_key_down(&kbdState, ALLEGRO_KEY_LSHIFT) ||
                al_key_down(&kbdState, ALLEGRO_KEY_RSHIFT);
bool keyalt = al_key_down(&kbdState, ALLEGRO_KEY_ALT) ||
              al_key_down(&kbdState, ALLEGRO_KEY_ALTGR);

The Allegro 5 way would probably be to update the booleans based on events instead, though.

Thread #617163. Printed from