Multikey Input Functions

I wrote a couple functions for multiple key input, in case you want to use up arrow, w, and space for jump as an example. Here's what I have:

// Multikey checks
bool check_key_down(array<int>@ keys) {
    // Requires array of keys to check, e.g. {KEY_LALT, KEY_RALT}
    // Return true if at least one of the specified keys is pressed.
    for (uint i = 0; i < keys.length(); i++)
    {
        if (key_down(keys[i]) == true)
        {
            return true;
        }
    }
    return false;
}
                                                                                                                                                                          
                                                                                                                                                                          
bool check_all_keys(array<int>@ keys) {
    // Requires array of keys to check, e.g. {KEY_LALT, KEY_RALT}
    // Return true only if all the specified keys are pressed.
    for (uint i = 0; i < keys.length(); i++)
    {
        if (key_down(keys[i]) == false)
        {
            return false;
        }
    }
    return true;
}

Here is example usage:

// Define multiple keys for actions.
    array<int> jumpKeys = {KEY_UP, KEY_W, KEY_SPACE};
    array<int> leftKeys = {KEY_LEFT, KEY_A};
    array<int> runKeys = {KEY_LCTRL, KEY_RCTRL};
    array<int> rightKeys = {KEY_RIGHT, KEY_D};

// Handle movement
        if (check_key_down(runKeys)) { // Run!
            moveInterval = 100;
        }
        else {
            moveInterval = 200;
        }
        if ((check_key_down(leftKeys)) && (currentTime - lastMoveTime >= moveInterval)) {
            if (playerPosition > 0) {
                playerPosition--;
                lastMoveTime = currentTime;
                if (!isJumping) soundPool.play_stationary("sounds/footstep.wav", false);
            } else {
                soundPool.play_stationary("sounds/edge.wav", false);
                lastMoveTime = currentTime;
            }
        }
        else if ((check_key_down(rightKeys)) && (currentTime - lastMoveTime >= moveInterval)) {
            if (playerPosition < roomWidth) {
                playerPosition++;
                lastMoveTime = currentTime;
                if (!isJumping) soundPool.play_stationary("sounds/footstep.wav", false);
            } else {
                soundPool.play_stationary("sounds/edge.wav", false);
                lastMoveTime = currentTime;
            }
        }
                                                                                                                                                                          
        // Handle jumping
        if ((check_key_down(jumpKeys)) && (isJumping == false)) {
            isJumping = true;
            jumpStartTime = currentTime;
            soundPool.play_stationary("sounds/jump.wav", false);
        }

I hope someone finds this useful. Also, feedback or suggestion for improvement welcome.

1 Like