SFML community forums

Help => General => Topic started by: Vot1_Bear on August 17, 2013, 09:19:14 am

Title: Relation between sf::keyboard::key and char
Post by: Vot1_Bear 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 :)
Title: Re: Relation between sf::keyboard::key and char
Post by: Nexus 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 (http://www.bromeon.ch/libraries/thor/v2.0/doc/_input_names_8hpp.html)
Title: Re: Relation between sf::keyboard::key and char
Post by: Vot1_Bear 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 (http://www.bromeon.ch/libraries/thor/v2.0/doc/_input_names_8hpp.html)
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!
Title: Re: Relation between sf::keyboard::key and char
Post by: Nexus 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.
Title: Re: Relation between sf::keyboard::key and char
Post by: Vot1_Bear 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!
Title: Re: Relation between sf::keyboard::key and char
Post by: Velho 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.
Title: Re: Relation between sf::keyboard::key and char
Post by: eXpl0it3r 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 (https://github.com/Bromeon/Thor/blob/master/src/InputNames.cpp#L88).