How to check if any key is pressed in pixel game engine

586 Views Asked by At

I know that a specific key, for example a can be checked as

if (GetKey(olc::A).bPressed){
            //do stuff
        }

but how can I check if any key is pressed.

Example

if (any key is pressed){
            //if i pressed a, it should output a.
            std::cout << the key which was pressed was a; 
        }

I am looking for a pixel game engine specific way to do this but I couldn't find anything on the internet.

2

There are 2 best solutions below

0
On BEST ANSWER

Turns out there is no way provided by the engine to do this, but there is a work-around provided to me by some awesome people. Its to set the make you own key char map and store them in a vector with appropriate values. Then iterate through that vector to see if any of those keys are pressed.

struct KeyCharMap 
{
    olc::Key key;
    char lower;
    char upper;
};

std::vector<KeyCharMap> valueInputKeys = 
{
                {olc::A, 'a', 'A'},
                {olc::B, 'b', 'B'},
                {olc::C, 'c', 'C'},
                {olc::D, 'd', 'D'},
                {olc::E, 'e', 'E'},
                {olc::F, 'f', 'F'},
                {olc::G, 'g', 'G'},
                {olc::H, 'h', 'H'},
                {olc::I, 'i', 'I'},
                {olc::J, 'j', 'J'},
                {olc::K, 'k', 'K'},
                {olc::L, 'l', 'L'},
                {olc::M, 'm', 'M'},
                {olc::N, 'n', 'N'},
                {olc::O, 'o', 'O'},
                {olc::P, 'p', 'P'},
                {olc::Q, 'q', 'Q'},
                {olc::R, 'r', 'R'},
                {olc::S, 's', 'S'},
                {olc::T, 't', 'T'},
                {olc::U, 'u', 'U'},
                {olc::V, 'v', 'V'},
                {olc::W, 'w', 'W'},
                {olc::X, 'x', 'X'},
                {olc::Y, 'y', 'Y'},
                {olc::Z, 'z', 'Z'},
                {olc::K0, '0', ')'},
                {olc::K1, '1', '!'},
                {olc::K2, '2', '@'},
                {olc::K3, '3', '#'},
                {olc::K4, '4', '$'},
                {olc::K5, '5', '%'},
                {olc::K6, '6', '^'},
                {olc::K7, '7', '&'},
                {olc::K8, '8', '*'},
                {olc::K9, '9', '('},
                {olc::NP0, '0', '0'},
                {olc::NP1, '1', '1'},
                {olc::NP2, '2', '2'},
                {olc::NP3, '3', '3'},
                {olc::NP4, '4', '4'},
                {olc::NP5, '5', '5'},
                {olc::NP6, '6', '6'},
                {olc::NP7, '7', '7'},
                {olc::NP8, '8', '8'},
                {olc::NP9, '9', '9'},
                {olc::NP_MUL, '*', '*'},
                {olc::NP_DIV, '/', '/'},
                {olc::NP_ADD, '+', '+'},
                {olc::NP_SUB, '-', '-'},
                {olc::NP_DECIMAL, '.', '.'},
                {olc::PERIOD, '.', '>'},
                {olc::SPACE, ' ', ' '},
                {olc::OEM_1, ';', ':'},
                {olc::OEM_2, '/', '?'},
                {olc::OEM_3, '`', '~'},
                {olc::OEM_4, '[', '{'},
                {olc::OEM_5, '\\', '|'},
                {olc::OEM_6, ']', '}'},
                {olc::OEM_7, '\'', '"'},
                {olc::OEM_8, '-', '-'},
                {olc::EQUALS, '=', '+'},
                {olc::COMMA, ',', '<'},
                {olc::MINUS, '-', '_'}
};

   for (auto& m : valueInputKeys)
        if (GetKey(m.key).bPressed)
             std::cout << m.lower;

                    
0
On

A little late, but you can create your own function by edditing the source code. This is what I did.

First create the function header, definition and a flag variable:

    // Returns true if any key is pressed
    bool MouseOrKeyHeld() const;
    bool PixelGameEngine::MouseOrKeyHeld() const { return pMouseOrKeyHeld; }
    bool pMouseOrKeyHeld{false};

Then add the following code in the function void PixelGameEngine::olc_CoreUpdate():

// Add this on the beggining of the function
pMouseOrKeyHeld = false;

// some more code

// find this line in the function
pStateOld[i] = pStateNew[i];
// And add this bellow it
pMouseOrKeyHeld = pMouseOrKeyHeld || pStateNew[i];

Explanation: You are creating a new flag that is set to true when any key is pressed or a mouse button is clicked. Note that this flag behaves like bHeld. It will be set to true every frame that input occurs, otherwise false.