SFML community forums

Help => Window => Topic started by: two-tone- on February 01, 2014, 06:00:13 am

Title: How do I create a switch statement based on the real-time keyboard state?
Post by: two-tone- on February 01, 2014, 06:00:13 am
The idea is pretty simple, I would like to write a switch that reads the output from sf::Keyboard. I know I could create a series of if statements, but I feel that makes my code harder to read/more complicated than need be.

I would like it to be something similiar to this

switch(keyboardState)
{
    case A:
        {
            //does stuff
        }
}
Title: Re: How do I create a switch statement based on the real-time keyboard state?
Post by: G. on February 01, 2014, 06:59:10 am
You can't, and it doesn't really have a meaning since multiple keys can be pressed at the same time.
Title: Re: How do I create a switch statement based on the real-time keyboard state?
Post by: two-tone- on February 01, 2014, 07:30:29 am
Ah, damn. So much for no if statements.

There has to be a simpler way than writing out a billion if statements.
Title: Re: How do I create a switch statement based on the real-time keyboard state?
Post by: Jesper Juhl on February 01, 2014, 09:55:29 am
You could use events instead.
Title: Re: How do I create a switch statement based on the real-time keyboard state?
Post by: two-tone- on February 01, 2014, 10:05:27 am
Not fast enough.
Title: Re: How do I create a switch statement based on the real-time keyboard state?
Post by: eXpl0it3r on February 01, 2014, 10:39:43 am
The question is: What are you trying to achieve?

If you want text input, then this is the wrong way to approach it. Instead you need to use the TextEntered event.
If you want key mapping, then I can highly recommend Thor's Action class. Nexus has done the big work in writing a big enum "converter".

Edit: Just saw your other post (http://en.sfml-dev.org/forums/index.php?topic=14291.msg100158;topicseen#new), could'vs written that in here, given that it's about the same thing...
Title: Re: How do I create a switch statement based on the real-time keyboard state?
Post by: two-tone- on February 01, 2014, 10:54:35 am
While they are related, yes, the two problems are very different in nature.  For this question I just wanted an easy way to create a switch based on input from the raw input of the keyboard for key mapping (easier to read), for the other I need to figure out how to move the sprite from one location to x other locations with out having to write an exponential amount of if/if else statements.
Title: Re: How do I create a switch statement based on the real-time keyboard state?
Post by: eXpl0it3r on February 01, 2014, 11:05:17 am
See the answer on the other thread, it should answer this question as well.
And again if you want key mapping, you should definitely look into Thor's Action class.