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

Author Topic: Get Key Name  (Read 6339 times)

0 Members and 1 Guest are viewing this topic.

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Get Key Name
« on: July 07, 2012, 02:09:14 am »
Hi

I´m writing the configuration system for my game. I need a  function that returns the name of a given key. This is my code:

#include <array>
#include <string>
#include <SFML/Window/Keyboard.hpp>

...

std::string ige::AdmEntrada::obtNombreTecla (const sf::Keyboard::Key tecla) {

    // Array con los nombres de todas las teclas en castellano
    const static std::array <std::string, sf::Keyboard::Key::KeyCount - 1>  nombreTeclas = {

        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
        "R", "S", "T", "U", "V", "W", "X", "Y", "Z",

        "Cero", "Uno", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete", "Ocho", "Nueve",

        "Esc",
        "Ctrl Izq.", "May. Izq.", "Alt Izq.", "Sistema Izq.",
        "Ctrl Der.", "May. Der.", "Alt Der.", "Sistema Der.",

        "Menu", "Abre cor.", "Cierra cor.", "Punto y coma", "Coma", "Punto",
        "Comillas", "Barra", "Barra inv.", "Tilde", "Igual", "Guion",

        "Barra espaciadora", "Enter", "Backspace", "Tab", "Page Up", "Page Down",
        "Fin", "Inicio", "Insertar", "Suprimir",
        "Agregar", "Restar", "Multiplicar", "Dividir",

        "Flecha Izq.", "Flecha Der.", "Flecha Arriba", "Flecha Abajo",
        "0 Numpad", "1 Numpad", "2 Numpad", "3 Numpad", "4 Numpad",
        "5 Numpad", "6 Numpad", "7 Numpad", "8 Numpad", "9 Numpad",

        "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",
        "F11", "F12", "F13", "F14", "F15", "Pausa"

    };


    return    nombreTeclas [tecla];
}
 


I got 1 error and 1 warning:

E:\Programacion\TV Circo\JUEGO\AdmEntrada.cpp|389|warning: missing braces around initializer for 'std::array<std::basic_string<char>, 100u>::value_type [100] {aka std::basic_string<char> [100]}' [-Wmissing-braces]|
E:\Programacion\TV Circo\JUEGO\AdmEntrada.cpp|389|error: too many initializers for 'const std::array<std::basic_string<char>, 100u>'|
||=== Build finished: 1 errors, 1 warnings (0 minutes, 0 seconds) ===|
 


How can I initialize the std::array correctly? It doesnt support std::initializer_list ctor as far as I know. Adding braces thows only the error (too many initializers)

I´m using MinGW/GCC 4.8.0 (c++0x enabled), SFML 2.0
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Get Key Name
« Reply #1 on: July 07, 2012, 04:57:11 am »
Try double-braces:
const static std::array <std::string, sf::Keyboard::Key::KeyCount - 1>  nombreTeclas = {{ /* stuff here */ }};
 

The reason for this is because std::array<T, n> is equivalent to this:
struct array {
    T values[n];
};
 

That said, in this context I'm pretty sure it's supposed to work without the extra braces, so this may be a compiler bug. If nothing else works, you could always just use a bare array.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Get Key Name
« Reply #2 on: July 07, 2012, 08:06:21 am »
Single braces are allowed, I don't know why g++ outputs a warning.

The problem is described in the error message, please read it carefully... "Too many initializers", because you use KeyCount - 1 as size. Why - 1?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Get Key Name
« Reply #3 on: July 07, 2012, 09:18:47 am »
You should use a switch that does explicit mapping. In the future, these constants might be reordered or changed.
Laurent Gomila - SFML developer

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Get Key Name
« Reply #4 on: July 08, 2012, 06:25:04 am »
Try double-braces

Adding braces thows only the error (too many initializers)

The problem is described in the error message, please read it carefully... "Too many initializers", because you use KeyCount - 1 as size. Why - 1?

I should remember the enum starts at 0 by default, not 1  ;D

You should use a switch that does explicit mapping. In the future, these constants might be reordered or changed.

I prefer to hate you now for this long switch sentence, before that hate you more in SFML 3   :P (just kidding haha)
Thanks to all for the answers
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

BlackHC

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Get Key Name
« Reply #5 on: September 23, 2012, 07:09:58 pm »
Helper snippet :)
const char *getKeyName( const sf::Keyboard::Key key ) {
        switch( key ) {
        default:
        case sf::Keyboard::Unknown:
                return "Unknown";
        case sf::Keyboard::A:
                return "A";
        case sf::Keyboard::B:
                return "B";
        case sf::Keyboard::C:
                return "C";
        case sf::Keyboard::D:
                return "D";
        case sf::Keyboard::E:
                return "E";
        case sf::Keyboard::F:
                return "F";
        case sf::Keyboard::G:
                return "G";
        case sf::Keyboard::H:
                return "H";
        case sf::Keyboard::I:
                return "I";
        case sf::Keyboard::J:
                return "J";
        case sf::Keyboard::K:
                return "K";
        case sf::Keyboard::L:
                return "L";
        case sf::Keyboard::M:
                return "M";
        case sf::Keyboard::N:
                return "N";
        case sf::Keyboard::O:
                return "O";
        case sf::Keyboard::P:
                return "P";
        case sf::Keyboard::Q:
                return "Q";
        case sf::Keyboard::R:
                return "R";
        case sf::Keyboard::S:
                return "S";
        case sf::Keyboard::T:
                return "T";
        case sf::Keyboard::U:
                return "U";
        case sf::Keyboard::V:
                return "V";
        case sf::Keyboard::W:
                return "W";
        case sf::Keyboard::X:
                return "X";
        case sf::Keyboard::Y:
                return "Y";
        case sf::Keyboard::Z:
                return "Z";
        case sf::Keyboard::Num0:
                return "Num0";
        case sf::Keyboard::Num1:
                return "Num1";
        case sf::Keyboard::Num2:
                return "Num2";
        case sf::Keyboard::Num3:
                return "Num3";
        case sf::Keyboard::Num4:
                return "Num4";
        case sf::Keyboard::Num5:
                return "Num5";
        case sf::Keyboard::Num6:
                return "Num6";
        case sf::Keyboard::Num7:
                return "Num7";
        case sf::Keyboard::Num8:
                return "Num8";
        case sf::Keyboard::Num9:
                return "Num9";
        case sf::Keyboard::Escape:
                return "Escape";
        case sf::Keyboard::LControl:
                return "LControl";
        case sf::Keyboard::LShift:
                return "LShift";
        case sf::Keyboard::LAlt:
                return "LAlt";
        case sf::Keyboard::LSystem:
                return "LSystem";
        case sf::Keyboard::RControl:
                return "RControl";
        case sf::Keyboard::RShift:
                return "RShift";
        case sf::Keyboard::RAlt:
                return "RAlt";
        case sf::Keyboard::RSystem:
                return "RSystem";
        case sf::Keyboard::Menu:
                return "Menu";
        case sf::Keyboard::LBracket:
                return "LBracket";
        case sf::Keyboard::RBracket:
                return "RBracket";
        case sf::Keyboard::SemiColon:
                return "SemiColon";
        case sf::Keyboard::Comma:
                return "Comma";
        case sf::Keyboard::Period:
                return "Period";
        case sf::Keyboard::Quote:
                return "Quote";
        case sf::Keyboard::Slash:
                return "Slash";
        case sf::Keyboard::BackSlash:
                return "BackSlash";
        case sf::Keyboard::Tilde:
                return "Tilde";
        case sf::Keyboard::Equal:
                return "Equal";
        case sf::Keyboard::Dash:
                return "Dash";
        case sf::Keyboard::Space:
                return "Space";
        case sf::Keyboard::Return:
                return "Return";
        case sf::Keyboard::BackSpace:
                return "BackSpace";
        case sf::Keyboard::Tab:
                return "Tab";
        case sf::Keyboard::PageUp:
                return "PageUp";
        case sf::Keyboard::PageDown:
                return "PageDown";
        case sf::Keyboard::End:
                return "End";
        case sf::Keyboard::Home:
                return "Home";
        case sf::Keyboard::Insert:
                return "Insert";
        case sf::Keyboard::Delete:
                return "Delete";
        case sf::Keyboard::Add:
                return "Add";
        case sf::Keyboard::Subtract:
                return "Subtract";
        case sf::Keyboard::Multiply:
                return "Multiply";
        case sf::Keyboard::Divide:
                return "Divide";
        case sf::Keyboard::Left:
                return "Left";
        case sf::Keyboard::Right:
                return "Right";
        case sf::Keyboard::Up:
                return "Up";
        case sf::Keyboard::Down:
                return "Down";
        case sf::Keyboard::Numpad0:
                return "Numpad0";
        case sf::Keyboard::Numpad1:
                return "Numpad1";
        case sf::Keyboard::Numpad2:
                return "Numpad2";
        case sf::Keyboard::Numpad3:
                return "Numpad3";
        case sf::Keyboard::Numpad4:
                return "Numpad4";
        case sf::Keyboard::Numpad5:
                return "Numpad5";
        case sf::Keyboard::Numpad6:
                return "Numpad6";
        case sf::Keyboard::Numpad7:
                return "Numpad7";
        case sf::Keyboard::Numpad8:
                return "Numpad8";
        case sf::Keyboard::Numpad9:
                return "Numpad9";
        case sf::Keyboard::F1:
                return "F1";
        case sf::Keyboard::F2:
                return "F2";
        case sf::Keyboard::F3:
                return "F3";
        case sf::Keyboard::F4:
                return "F4";
        case sf::Keyboard::F5:
                return "F5";
        case sf::Keyboard::F6:
                return "F6";
        case sf::Keyboard::F7:
                return "F7";
        case sf::Keyboard::F8:
                return "F8";
        case sf::Keyboard::F9:
                return "F9";
        case sf::Keyboard::F10:
                return "F10";
        case sf::Keyboard::F11:
                return "F11";
        case sf::Keyboard::F12:
                return "F12";
        case sf::Keyboard::F13:
                return "F13";
        case sf::Keyboard::F14:
                return "F14";
        case sf::Keyboard::F15:
                return "F15";
        case sf::Keyboard::Pause:
                return "Pause";
        }
}
 

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: Get Key Name
« Reply #6 on: September 23, 2012, 07:30:48 pm »
Helper snippet :)
...snip...
Did you put this on the Wiki? You should! :)

 

anything