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

Author Topic: [SOLVED]Problem with Mouse.x stored in Event.key.code  (Read 1751 times)

0 Members and 1 Guest are viewing this topic.

Husenap

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
[SOLVED]Problem with Mouse.x stored in Event.key.code
« on: March 25, 2015, 08:29:10 pm »
So, my application has been closing all the time and driving me crazy, and I've thought that it crashed, but I later figured out that the problem lies here:

if(event.type == event.Closed || (event.key.code == sf::Keyboard::Escape)){
    m_window.close();
}
 

So what's happening is that the mouse.x position is stored in event.key.code every time the mouse is moved.
Now, sf::Keyboard::Escape == 36
So, every time my mouse is at pixel 36, the application closes, does anyone know how I can rewrite the above code, if possible, to make it work with the escape button?

Appreciate all answers! :)
« Last Edit: March 25, 2015, 10:07:51 pm by Husenap »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Problem with Mouse.x stored in Event.key.code
« Reply #1 on: March 25, 2015, 08:34:34 pm »
You are reading "event.key.code" before checking "event.type" to see if the event is a type where that field is valid. That's a straight up bug in your code.
The sf::Event is a union and you must check the 'type' member first before accessing other members. And then only access members that are actually valid for that specific type of event.
« Last Edit: March 25, 2015, 08:36:56 pm by Jesper Juhl »

Husenap

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Problem with Mouse.x stored in Event.key.code
« Reply #2 on: March 25, 2015, 08:38:55 pm »
Thanks for the quick answer :)

Silly of me not to check
if(event.type == event.KeyPressed)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Problem with Mouse.x stored in Event.key.code
« Reply #3 on: March 25, 2015, 08:44:26 pm »
That should be

if (event.type ==  sf::Event::KeyPressed)
« Last Edit: March 25, 2015, 08:47:02 pm by Jesper Juhl »

Husenap

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Problem with Mouse.x stored in Event.key.code
« Reply #4 on: March 25, 2015, 08:47:39 pm »
What is the difference between using sf::Event::KeyPressed and event.KeyPressed when they return the same value?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem with Mouse.x stored in Event.key.code
« Reply #5 on: March 25, 2015, 09:14:08 pm »
event.KeyPressed just happens to work, but what you mean is really sf::Event::KeyPressed, since the value is not tied to the event instance.
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [SOLVED]Problem with Mouse.x stored in Event.key.code
« Reply #6 on: March 25, 2015, 10:45:31 pm »
What Laurent said.
Or phrased differently: What do you want to rely on? A guarantee given to you by the library you use or something that happens to work (for now) by sheer dumb luck?
« Last Edit: March 25, 2015, 11:25:19 pm by Jesper Juhl »

 

anything