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

Author Topic: event.text.unicode returning wrong code  (Read 1972 times)

0 Members and 1 Guest are viewing this topic.

antinoid

  • Newbie
  • *
  • Posts: 8
    • View Profile
event.text.unicode returning wrong code
« on: March 22, 2022, 06:43:24 pm »
I'm making a widgets class for some of my projects and I've implemented buttons, sliders, switches, etc, but having a problem with the input field, currently, I'm just trying to get whatever character I'm typing to print in console, but it seems like either, event.text.unicode, my computer, or my conversion method is wrong because I'm getting an output, but it's not the expected one.

here's the code. (taken directly from sfml events page, https://www.sfml-dev.org/tutorials/2.5/window-events.php)


        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            if (event.type == sf::Event::KeyPressed)
            {
            if (event.text.unicode < 128)
            std::cout << "event.text.unicode = " << event.text.unicode <<  ". ASCII character returned: " <<
            static_cast<char>(event.text.unicode) << std::endl;
            }
        }

here's the output when I click different keys:

(clicks "a" key)
event.text.unicode = 0. ASCII character returned:

(clicks "g" key)
event.text.unicode = 6. ASCII character returned: 

(clicks "1" key)
event.text.unicode = 27. ASCII character returned:

(clicks "7" key)
vent.text.unicode = 33. ASCII character returned: !
(that missing "e" at the start is actually there)

(clicks "9" key)
event.text.unicode = 35. ASCII character returned: #


these are very unexpected and I don't know what to do, I looked up a Unicode char on Wikipedia https://en.wikipedia.org/wiki/List_of_Unicode_characters but it didn't give me much info, any help would be appreciated thanks in advance.
« Last Edit: March 22, 2022, 10:24:51 pm by antinoid »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: event.text.unicode returning wrong code
« Reply #1 on: March 22, 2022, 09:48:42 pm »
The event type you want is sf::Event::TextEntered not sf::Event::KeyPressed

antinoid

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: event.text.unicode returning wrong code
« Reply #2 on: March 22, 2022, 10:24:37 pm »
Worked like a charm, thanks so much. :D
 :)  :)  :)  :)  :)