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

Author Topic: How do I create a switch statement based on the real-time keyboard state?  (Read 3362 times)

0 Members and 2 Guests are viewing this topic.

two-tone-

  • Newbie
  • *
  • Posts: 23
    • View Profile
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
        }
}

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How do I create a switch statement based on the real-time keyboard state?
« Reply #1 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.

two-tone-

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: How do I create a switch statement based on the real-time keyboard state?
« Reply #2 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.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: How do I create a switch statement based on the real-time keyboard state?
« Reply #3 on: February 01, 2014, 09:55:29 am »
You could use events instead.

two-tone-

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: How do I create a switch statement based on the real-time keyboard state?
« Reply #4 on: February 01, 2014, 10:05:27 am »
Not fast enough.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How do I create a switch statement based on the real-time keyboard state?
« Reply #5 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, could'vs written that in here, given that it's about the same thing...
« Last Edit: February 01, 2014, 10:41:30 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

two-tone-

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: How do I create a switch statement based on the real-time keyboard state?
« Reply #6 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How do I create a switch statement based on the real-time keyboard state?
« Reply #7 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/