Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Keyboard array?  (Read 4924 times)

0 Members and 1 Guest are viewing this topic.

TheGuerilla

  • Newbie
  • *
  • Posts: 27
  • Cynical Prick.exe
    • View Profile
    • Email
Keyboard array?
« 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?)

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Keyboard array?
« Reply #1 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>]
« Last Edit: March 25, 2016, 08:04:03 am by bitano »

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Keyboard array?
« Reply #2 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Keyboard array?
« Reply #3 on: March 25, 2016, 02:04:25 pm »
Instead of int use the enum type sf::Keyboard::Key.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

TheGuerilla

  • Newbie
  • *
  • Posts: 27
  • Cynical Prick.exe
    • View Profile
    • Email
Re: Keyboard array?
« Reply #4 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!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Keyboard array?
« Reply #5 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.
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Keyboard array?
« Reply #6 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 ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything