Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Checking if the Shift or Alt is pressed.

This thread is locked; no one can reply to it. rss feed Print
Checking if the Shift or Alt is pressed.
nshade
Member #4,372
February 2004

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

al_get_keyboard_state(&kbdState);
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?

========
EDIT
========
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;

torhu
Member #2,727
September 2002
avatar

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.

Go to: