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

Author Topic: Finding out, what key is pressed, Key code "all"  (Read 29205 times)

0 Members and 3 Guests are viewing this topic.

Mr. X

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Finding out, what key is pressed, Key code "all"
« 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)

bullno1

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Finding out, what key is pressed, Key code "all"
« Reply #1 on: October 12, 2008, 02:17:25 pm »
Just catch the event TextEnter. It's exactly what you need.

Mr. X

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Finding out, what key is pressed, Key code "all"
« Reply #2 on: October 12, 2008, 06:25:30 pm »
Ah, Thank you. Is it part of sf::Event?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Finding out, what key is pressed, Key code "all"
« Reply #3 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.
Laurent Gomila - SFML developer

Mr. X

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Finding out, what key is pressed, Key code "all"
« Reply #4 on: October 12, 2008, 08:07:27 pm »
Thank you.
And how do I find out, what key is pressed?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Finding out, what key is pressed, Key code "all"
« Reply #5 on: October 12, 2008, 08:19:19 pm »
This is the KeyPressed event.
Laurent Gomila - SFML developer

Mr. X

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Finding out, what key is pressed, Key code "all"
« Reply #6 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*/);
}
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Finding out, what key is pressed, Key code "all"
« Reply #7 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.
Laurent Gomila - SFML developer

Mr. X

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Finding out, what key is pressed, Key code "all"
« Reply #8 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?

Imbue

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Finding out, what key is pressed, Key code "all"
« Reply #9 on: October 12, 2008, 10:45:54 pm »
Form the tutorial:

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. 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Finding out, what key is pressed, Key code "all"
« Reply #10 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.
Laurent Gomila - SFML developer

Mr. X

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Finding out, what key is pressed, Key code "all"
« Reply #11 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Finding out, what key is pressed, Key code "all"
« Reply #12 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);
}
Laurent Gomila - SFML developer

 

anything