SFML community forums

General => General discussions => Topic started by: TheGuerilla on March 25, 2016, 06:13:38 am

Title: Keyboard array?
Post by: TheGuerilla on March 25, 2016, 06:13:38 am
One of the few things I really liked about SDL was the fact that the keyboard could be gathered from an array; because I could store a record of the last frame by just copying the contents of the array into another and testing for key presses and releases by comparing the two. I don't like testing really anything inside the event poll, so is there any sort of array of the keyboard states in SDL? And if not, is there any other way than checking for key presses/releases than in the event poll? (Perhaps a functions?)
Title: Re: Keyboard array?
Post by: bitano on March 25, 2016, 07:55:58 am
It's really easy to create such an array yourself - a couple of lines of code.

    std::map<int,bool> keys; // Add this somewhere in a class or where it doesn't get destroyed
    // ---
    sf::Event event;
    while(window.pollEvent(event)) {
        switch(event.type) {
            case sf::Event::KeyPressed:
                keys[event.key.code] = true;
                break;
            case sf::Event::KeyReleased:
                keys[event.key.code] = false;
                break;
            default:
                break;
    }
 

every key's state can be checked with keys[sf::Keyboard:<key>]
Title: Re: Keyboard array?
Post by: nicox11 on March 25, 2016, 09:46:07 am
Can't you simply use sf::Keyboard::isKeyPressed ? (I surely don't understand your needs).

http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Keyboard.php (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Keyboard.php)
Title: AW: Keyboard array?
Post by: eXpl0it3r on March 25, 2016, 02:04:25 pm
Instead of int use the enum type sf::Keyboard::Key.
Title: Re: Keyboard array?
Post by: TheGuerilla on March 25, 2016, 06:36:52 pm
It's really easy to create such an array yourself - a couple of lines of code.

    std::map<int,bool> keys; // Add this somewhere in a class or where it doesn't get destroyed
    // ---
    sf::Event event;
    while(window.pollEvent(event)) {
        switch(event.type) {
            case sf::Event::KeyPressed:
                keys[event.key.code] = true;
                break;
            case sf::Event::KeyReleased:
                keys[event.key.code] = false;
                break;
            default:
                break;
    }
 

every key's state can be checked with keys[sf::Keyboard:<key>]

Dude I had no idea I could do this. Thanks a ton!
Title: Re: Keyboard array?
Post by: FRex on March 26, 2016, 01:36:53 am
You can use an array or a vector, only Unkown is -1, all the valid keys are in range from 0 to KeyCount - 1.
Title: Re: Keyboard array?
Post by: Nexus on April 05, 2016, 04:23:05 pm
Or use a map, to be on the safe side, even in presence of unlikely future changes ;)