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

Author Topic: Relation between sf::keyboard::key and char  (Read 4688 times)

0 Members and 1 Guest are viewing this topic.

Vot1_Bear

  • Newbie
  • *
  • Posts: 31
  • Noob in-training
    • View Profile
    • Game development blog
Relation between sf::keyboard::key and char
« on: August 17, 2013, 09:19:14 am »
Hello SFML community!

I was wondering about implementing customizable controls for a game I'm currently working on, and this question somewhat popped up on my mind: Is there any way we can convert an sf::keyboard::key (say, A) into its char/string name counterpart?

In order to create an actual options/controls screen, I'd need to be able to convert a sf::keyboard::key into a string of what it represents, which will be shown to the screen. Currently though, the only thing I can think of is manually represent each code as a specific char or string (using a ton of ifs to assign sf::keyboard::key::A into "A", sf::keyboard::key::Lshift into "Left Shift", etc).

I'm pretty sure i'm not the first to face this problem, so is there a better alternative to this?

Thanks in advance :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Relation between sf::keyboard::key and char
« Reply #1 on: August 17, 2013, 11:28:26 am »
Is there any way we can convert an sf::keyboard::key (say, A) into its char/string name counterpart?
Not with SFML, but with Thor: InputNames.hpp
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Vot1_Bear

  • Newbie
  • *
  • Posts: 31
  • Noob in-training
    • View Profile
    • Game development blog
Re: Relation between sf::keyboard::key and char
« Reply #2 on: August 19, 2013, 05:39:51 pm »
Is there any way we can convert an sf::keyboard::key (say, A) into its char/string name counterpart?
Not with SFML, but with Thor: InputNames.hpp
But that means I have to go through all those horrible CMake compiling experiences I had with SFML a few years back again :(

JK, I heard about it before and it does sound pretty awesome, though I didn't try it at first due to above reason. Gonna try it now, thanks for the link (and for the library too ofc)!

On an unrelated note, (correct me if I'm wrong, but) does thor also handle texts? I did remember reading about sf::Text's word wrapping issue some time ago, and I believe thor was also suggested as a solution. That was a pretty long time ago though (can't find where I even found it) and since I can't seem to find anything about it on the documentation... well, just wanting to make sure :)

Thanks again for the help!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Relation between sf::keyboard::key and char
« Reply #3 on: August 19, 2013, 05:45:07 pm »
But that means I have to go through all those horrible CMake compiling experiences I had with SFML a few years back again :(
It's only horrible if you don't read the tutorial :P

Setting Thor up is explained step by step, you just have to read carefully. On Windows, you can also use the Nightly builds (everything is described on the download page).

On an unrelated note, (correct me if I'm wrong, but) does thor also handle texts?
No, there is nothing specific for sf::Text. I once posted a workaround for a SFML bug here, but it's well possible that it has been fixed meanwhile.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Vot1_Bear

  • Newbie
  • *
  • Posts: 31
  • Noob in-training
    • View Profile
    • Game development blog
Re: Relation between sf::keyboard::key and char
« Reply #4 on: August 20, 2013, 11:24:15 am »
It's only horrible if you don't read the tutorial :P

Setting Thor up is explained step by step, you just have to read carefully. On Windows, you can also use the Nightly builds (everything is described on the download page).
Well back then there were version differences (I found SFML via a tutorial referring to v1.6, and back then the site's tutorial page was still the old one) so I guess that's what made stuff so complicated :P
No, there is nothing specific for sf::Text. I once posted a workaround for a SFML bug here, but it's well possible that it has been fixed meanwhile.
Weird, I was sure someone asked about the lack of wordwrapping in sfml and one of the solutions was using an external library...

Ah well, gotta stop getting off topic with the thread. Thanks again!

Velho

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Relation between sf::keyboard::key and char
« Reply #5 on: January 27, 2015, 10:25:09 pm »
Hello SFML community!

I was wondering about implementing customizable controls for a game I'm currently working on, and this question somewhat popped up on my mind: Is there any way we can convert an sf::keyboard::key (say, A) into its char/string name counterpart?

In order to create an actual options/controls screen, I'd need to be able to convert a sf::keyboard::key into a string of what it represents, which will be shown to the screen. Currently though, the only thing I can think of is manually represent each code as a specific char or string (using a ton of ifs to assign sf::keyboard::key::A into "A", sf::keyboard::key::Lshift into "Left Shift", etc).

I'm pretty sure i'm not the first to face this problem, so is there a better alternative to this?

Thanks in advance :)

For starters sorry  for bumping old topic.
You can always cast the key. According to the API documentation they are represented as ints. So basically
char letter_a = static_cast<char>('A' + sf::Keyboard::Key::A); // Should give you 'A'.
char letter_b = static_cast<char>('A' + sf::Keyboard::Key::B); // Should give you 'B'.
// etc ...
 

But i guess it's dependant on your locale, etc.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11036
    • View Profile
    • development blog
    • Email
Re: Relation between sf::keyboard::key and char
« Reply #6 on: January 27, 2015, 11:22:20 pm »
Please don't bump 1-2 year old threads to state a solution to a problem that has long been solved... ::)

Besides it's obvious that doing the check for every single key will work, it's just quite a bit of work, which Nexus has already done for everyone in Thor's InputNames.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/