I'm making a menu screen and am having issues keeping my program from flickering to the active game and the menu screen. I'm using the M key to toggle between the game states and M is continuously active when I hold it down. How does one stop the program from reading the input if the key is being held down. I only want one iteration then the player must tap again. I tried a while loop and it created an infinite loop crashing the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//key input
elseif((al_key_down(&keyState, ALLEGRO_KEY_M)))
{
// keyPress and prevKeyPress are being used in an attempt to cancel the ChangeState
keyPress = ALLEGRO_KEY_M;
if (state == PLAYING && keyPress != prevKeyPress )
{ChangeState (state, MENU);
std::cout << keyPress;
keyPress = prevKeyPress;}
elseif (state == PLAYING && keyPress == prevKeyPress )
{
keyPress = prevKeyPress;}
elseif (state == MENU && keyPress != prevKeyPress )
{ChangeState (state, PLAYING);
keyPress = prevKeyPress;}
elseif (state == MENU && keyPress == prevKeyPress )
{
keyPress = prevKeyPress;}
}
I've never used Allegro. I glanced at the documentation, and I slapped this together. Sorry if there's anything that's wrong or missing - It's pseudo-code, and I'm also tired.
The idea is that in your allegro event loop, you set keys down if they're pressed, and set keys up when they're released. In the key_handler() function you lock the keys if they are pressed and haven't been locked, followed by whatever it is you want that key to do.