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

Author Topic: application generates multiple key release event  (Read 4223 times)

0 Members and 1 Guest are viewing this topic.

6Peppered9

  • Newbie
  • *
  • Posts: 9
    • View Profile
application generates multiple key release event
« on: March 12, 2019, 04:39:30 am »
hello,

I'm dealing with the sfml events and have a problem that my application generates several KeyReleased events.

My pollEvent loop

while(rootWindow->pollEvent(event)) {
    for(auto it : drawStack) {
        if(it->eventable) {
            it->handleEvent(event);
        }
    }

 

drawStack is a stack of pointers of objects that I want to draw on the screen.
The polled event should be forwarded to the respective objects, which then decide what happens.

handleEvent

void handleEvent(sf:: Event) {
    switch(event.type) {
        case sf::Event::KeyReleased:
            cout << "obj 1 KeyReleased" << endl;
            break;
    }
}

 

everything works as long as there is only one object on the stack.
But with two objects on the stack I get such an output:
obj 1 KeyReleased
obj 2 KeyReleased
obj 2 KeyReleased
obj 2 KeyReleased
obj 2 KeyReleased
obj 2 KeyReleased
...
 

This is what i want:
obj 1 KeyReleased
obj 2 KeyReleased
 

is my procedure right?
Is there another way to do it?
Or did I not understand sf :: Event?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: application generates multiple key release event
« Reply #1 on: March 12, 2019, 07:34:25 am »
You get one event through polling, but then you handle the same event for each object and since they all use the same code, you'll get the same message printed n times.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

6Peppered9

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: application generates multiple key release event
« Reply #2 on: March 12, 2019, 08:05:28 am »
no, every object have another KeyRelease function.
I can assign a different function to each object via function pointer.

But I tried something and saved the objects in a fixed array, so I can access the objects via an index and it works.
no idea why it does not work by iterator

i had used this class for drawStack:
#include <stack>
#include <deque>

template<typename T, typename Container = std::deque<T>>
class IStack : public std::stack<T, Container>
{
    using std::stack<T, Container>::c;

        public:
        // expose just the iterators of the underlying container
        auto begin() {return std::begin(c);}
        auto end() {return std::end(c);}

        auto begin() const {return std::begin(c);}
        auto end() const {return std::end(c);}
};
 
https://stackoverflow.com/questions/525365/does-stdstack-expose-iterators

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: application generates multiple key release event
« Reply #3 on: March 12, 2019, 08:13:12 am »
I'm sure we could figure it out if you provide a minimal but complete example. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/