SFML community forums
General => Feature requests => Topic started by: Mr. X on October 12, 2008, 11:56:18 am
-
Hello,
I have not find a way to find out, what key is pressed.
It would be great if there is a method that returns the pressed key as a string, so if you press for example the Enter Key, it returns "\n", or if you press the Equal Key, it returns "=".
If thats already available, please say me, how to use it.
It would be also good, if there is an option to find out if any key is pressed.(Or I haven't found it)
-
Just catch the event TextEnter. It's exactly what you need.
-
Ah, Thank you. Is it part of sf::Event?
-
Yes. Catch sf::Event::TextEntered and check evt.Text.Unicode. It's a UTF-32 unicode character, but if you're interested only in the ASCII range you can directly convert it to a char.
By the way, it's explained in the event handling tutorial.
-
Thank you.
And how do I find out, what key is pressed?
-
This is the KeyPressed event.
-
But I haven't found a way to get it...
My code: while(SF.App.GetEvent(SF.Event)) {
if(SF.Event.Type == sf::Event::Closed) {
SF.App.Close();
}
if(SF.Event.Type == sf::Event::TextEntered) {
SF.ETB->AddText(/*Here should stand the pressed key*/);
}
}
-
So you don't want to know which key has been pressed, but which text / character has been entered (there's a difference : F2 is not a character, and ΓΌ is not a key). And for this, refer to my previous answer.
-
My compiler told me, that event.text.unicode is an Integer...
Is it a character code?
If Yes, how could I convert it to a char?
-
Form the tutorial (http://www.sfml-dev.org/tutorials/1.3/window-events.php):
sf::Event Event;
while (App.GetEvent(Event))
{
// Escape key pressed
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Running = false;
}
And there is a list of keys here. (http://www.sfml-dev.org/documentation/1.3/namespacesf_1_1Key.htm) You'll notice that they are setup to use ASCII characters:char(sf::Key::A) == 'a' //To convert to ASCII just cast to char.
-
No! For text input you must use the TextEntered event. There's a huge difference between a key pressed and a character entered.
My compiler told me, that event.text.unicode is an Integer...
Is it a character code?
If Yes, how could I convert it to a char?
Do you actually read my answers ?
It's a UTF-32 unicode character, but if you're interested only in the ASCII range you can directly convert it to a char.
-
I didn't find a way, to use the TextEntered Event for getting back a character without a compiler error. What code do I need, if I want the character. What have I to write down?
This gives a compiler Error that a sf::Event::EventType couldn't be converted to a std::string:SF.ETB->AddText(SF.Event.TextEntered);
But As I tried to convert it first to a char and then to a string, I get runtime Errors.
What Is the mistake?
-
if (SF.Event.Text.Unicode < 128) // ASCII range
{
std::string Text;
Text += static_cast<char>(SF.Event.Text.Unicode);
SF.ETB->AddText(Text);
}