Checking key release

From Allegro Wiki

Jump to: navigation, search

There are many occasions where a programmer needs to know if a key was just released. There are many ways of doing this, and here is small function that checks if a key was released.

C version:

int keyrel(int k)
{
        static int initialized = 0;
        static int keyp[KEY_MAX];

        if(!initialized)
        {
                /* Set the keyp (key pressed) flags to false */
                int i;
                for(i = 0; i < KEY_MAX; i++) keyp[i] = 0;
                initialized = 1;
        }

        /* Now for the checking
        Check if the key was pressed
        */
        if(key[k] && !keyp[k])
        {
                /* Set the flag and return */
                keyp[k] = 1;
                return 0;
        }
        else if(!key[k] && keyp[k])
        {
                /* The key was released */
                keyp[k] = 0;
                return 1;
        }
        /* Nothing happened? */
        return 0;
}

C++ version:

bool keyrel(int k)
{
        static bool initialized = false;
        static bool keyp[KEY_MAX];

        if(!initialized)
        {
                // Set the keyp (key pressed) flags to false
                for(int i = 0; i < KEY_MAX; i++) keyp[i] = false;
                initialized = true;
        }

        // Now for the checking
        // Check if the key was pressed
        if(key[k] && !keyp[k])
        {
                // Set the flag and return
                keyp[k] = true;
                return false;
        }
        else if(!key[k] && keyp[k])
        {
                // The key was released
                keyp[k] = false;
                return true;
        }
        // Nothing happened?
        return false;
}

Example usage:

/* Initialize game etc. */
/* Start the main game loop */
while(game_is_running)
{
        /* The key checking code goes in the logic part of the loop */
        /* Lets just pretend this is here. */
        if(keyrel(KEY_SPACE)) /* Space has been pressed and released */
        {
                /* Do something with this */
        }
        if(keyrel(KEY_ESC)) /* Escape key has been released */
        {
                /* Exit the game, for example. */
                game_is_running = FALSE;
        }
}

What it does:

Well, first, it declares some static variables ( initialized and keyp (which is an array of flags that are set or not)). The initialized variable is first set to false until we set all the flags in keyp (key pressed) to false. This is to insure that we do not repeat this code on every function call.

Then we check if a key was pressed and was previously not hold down. If that is true, we set the flag in the keyp array for the appropriate key and return false (Because no key was released).

If a key was not pressed now but was in the previous call, then we can assume that it was released, now we can set the appropriate flag in keyp to false and return true.

If nothing happens we return false.

Personal tools