SFML community forums

General => Feature requests => Topic started by: Mr. X on October 12, 2008, 11:56:18 am

Title: Finding out, what key is pressed, Key code "all"
Post 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)
Title: Finding out, what key is pressed, Key code "all"
Post by: bullno1 on October 12, 2008, 02:17:25 pm
Just catch the event TextEnter. It's exactly what you need.
Title: Finding out, what key is pressed, Key code "all"
Post by: Mr. X on October 12, 2008, 06:25:30 pm
Ah, Thank you. Is it part of sf::Event?
Title: Finding out, what key is pressed, Key code "all"
Post by: Laurent on October 12, 2008, 06:39:23 pm
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.
Title: Finding out, what key is pressed, Key code "all"
Post by: Mr. X on October 12, 2008, 08:07:27 pm
Thank you.
And how do I find out, what key is pressed?
Title: Finding out, what key is pressed, Key code "all"
Post by: Laurent on October 12, 2008, 08:19:19 pm
This is the KeyPressed event.
Title: Finding out, what key is pressed, Key code "all"
Post by: Mr. X on October 12, 2008, 08:27:03 pm
But I haven't found a way to get it...
My code:
Code: [Select]
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*/);
}
}
Title: Finding out, what key is pressed, Key code "all"
Post by: Laurent on October 12, 2008, 08:55:15 pm
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.
Title: Finding out, what key is pressed, Key code "all"
Post by: Mr. X on October 12, 2008, 10:23:45 pm
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?
Title: Finding out, what key is pressed, Key code "all"
Post by: Imbue on October 12, 2008, 10:45:54 pm
Form the tutorial (http://www.sfml-dev.org/tutorials/1.3/window-events.php):

Code: [Select]
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:
Code: [Select]
char(sf::Key::A) == 'a' //To convert to ASCII just cast to char.
Title: Finding out, what key is pressed, Key code "all"
Post by: Laurent on October 13, 2008, 08:19:46 am
No! For text input you must use the TextEntered event. There's a huge difference between a key pressed and a character entered.

Quote
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 ?
Quote
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.
Title: Finding out, what key is pressed, Key code "all"
Post by: Mr. X on October 13, 2008, 08:49:42 am
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:
Code: [Select]
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?
Title: Finding out, what key is pressed, Key code "all"
Post by: Laurent on October 13, 2008, 01:19:50 pm
Code: [Select]
if (SF.Event.Text.Unicode < 128) // ASCII range
{
    std::string Text;
    Text += static_cast<char>(SF.Event.Text.Unicode);
    SF.ETB->AddText(Text);
}