SFML community forums

Help => Window => Topic started by: Lolilolight on November 06, 2014, 03:06:23 pm

Title: Bug with the keyboard ?
Post by: Lolilolight on November 06, 2014, 03:06:23 pm
Hi, I'm actually coding a system which calls callback functions depending on which events are generated. (Or which keys or buttons are held down/pressed once)

When I have two keys pressed, and when I release only one, no more keypressed events are generated, even if a key is still pressed.

Is it a hardware bug or a SFML issue ?

PS : I'm on ubuntu.

Thanks.
Title: Re: Bug with the keyboard ?
Post by: Nexus on November 06, 2014, 03:39:21 pm
Are you talking about KeyPressed events, or Keyboard::isKeyPressed real-time input? Please provide a minimal complete example.

The former only trigger once when actually pressing the key (and later again if keyboard repeat is active).
Title: Re: Bug with the keyboard ?
Post by: Lolilolight on November 06, 2014, 05:05:49 pm
Quote
Are you talking about KeyPressed events, or Keyboard::isKeyPressed real-time input? Please provide a minimal complete example.

Both produce the same effect.

When I press more than one key and then if I release only one key, it tells me that no keys are pressed.

I hadn't this bug on windows, so...
Title: Re: Bug with the keyboard ?
Post by: Laurent on November 06, 2014, 07:25:01 pm
Complete and minimal code please.
Title: Re: Bug with the keyboard ?
Post by: Lolilolight on November 06, 2014, 09:42:46 pm
int main() {
 // Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
if(event.type == sf::Event::KeyPressed)
   std::cout<<"a key is pressed"<<std::endl;
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
}
// Clear screen
window.clear();
window.display();
}
return EXIT_SUCCESS;
 

When I press the "z " and the "a" key and then when  I release "z" (letting the "a" key held down) the message "a key is pressed" isn't displayed.
Title: Re: Bug with the keyboard ?
Post by: Hiura on November 06, 2014, 09:49:43 pm
Title: Re: Bug with the keyboard ?
Post by: Strelok on November 06, 2014, 09:50:05 pm
You're not pressing any new key so it generates only a key released event.
Title: Re: Bug with the keyboard ?
Post by: Lolilolight on November 08, 2014, 04:41:00 pm
Ok, so I'll rewrite this function (pollEvent) anyway.

Because I want to keep going to generate events even if a key is released.



Title: Re: Bug with the keyboard ?
Post by: Ixrec on November 08, 2014, 04:55:19 pm
Wouldn't it be a lot simpler to just check for both events in your code?

if(event.type == sf::Event::KeyPressed || event.type == sf::Event::KeyReleased)
Title: Re: Bug with the keyboard ?
Post by: Jesper Juhl on November 08, 2014, 05:02:06 pm
Simply keep track of relevant key state yourself. Like if you get a keydown event for the fire button, just do "fire_button = true;" and set it false when you get a keyrelease event. Then check your bools when you need to deside what to do. Depending on keydown events fireing repeatedly seems like a bad idea.
Title: Re: Bug with the keyboard ?
Post by: Ixrec on November 08, 2014, 05:11:37 pm
If that's the behavior he's after then he could simply not bother with events and use sf::Keyboard::isKeyPressed() instead.
Title: Re: Bug with the keyboard ?
Post by: Jesper Juhl on November 08, 2014, 05:14:32 pm
Well, at the moment I'm not entirely clear on what exactely he wants, so I'm guessing a bit.

Edit: in any case, talk about rewriting pollEvent just raises a giant "you are doing it wrong" red flag.
Title: Re: Bug with the keyboard ?
Post by: Lolilolight on November 10, 2014, 08:52:19 pm
Hi!

I want a behaviour like this : (I also need to have generic events, no only sfml event) so I've a function pointer array for event conversions, now what I want is to be able to generates event for every keys which are pressed and to stop to generate events when keys are released, I also want to set up a timer for events repetition, so, I've start to encode something like this :

void SFMLWindow::checkEvents(void) {
     sf::Event event;
     while (m_window.pollEvent(event)) {
        SystemEvent curr_sys_event(event.type);
        m_current_mapper.setEvent(event);
        m_current_mapper[curr_sys_event];
        std::map<SystemEvent, IEventMapperManager::EventMapperID>::iterator it = m_mappers.find(curr_sys_event);
        if (it == m_mappers.end()) {
            SFMLEventMapper mapper;
            mapper.setEvent(event);
            m_mappers.insert(std::make_pair(curr_sys_event, m_eventManager.registerEventMapper(mapper)));
        }
        for (it = m_mappers.begin(); it != m_mappers.end();it++) {
             SystemEvent sysEvent(event.type);
             m_eventManager.changeCurrentMapping(it->second);
             m_eventManager[sysEvent];
             m_events.insert(m_events.begin(), sysEvent);
        }
        for (it = m_mappers.begin(); it != m_mappers.end();) {
            if (event.type != sf::Event::KeyPressed && event.type != sf::Event::MouseButtonPressed) {
                m_eventManager.deleteEventMapper(it->second);
                it = m_mappers.erase(it);
            } else {
                it++;
            }
        }
        if (event.type == sf::Event::KeyReleased || event.type == sf::Event::MouseButtonReleased) {
            curr_sys_event.eventInfos.type = SystemEvent::Type::Pressed;
            it = m_mappers.find(curr_sys_event);
            m_eventManager.deleteEventMapper(it->second);
            it = m_mappers.erase(it);
        }
    }
}
SystemEvent SFMLWindow::popEvent() {
    checkEvents();
    if (m_events.size() == 0) {
        SystemEvent event(-1);
        event.eventInfos.origin = SystemEvent::Origin::NoEvent;
        event.eventInfos.typeID = SystemEvent::Type::NoType;
        event.eventInfos.type = SystemEvent::Type::NoType;
        event.paramTypeID = SystemEvent::Type::NoType;
        return event;
    } else {
        SystemEvent event = m_events.back();
        m_events.pop_back();
        return event;
    }
}
 

It seems to work fine for event conversion but I haven't tested yet for event repetition, and I haven't set up the timer yet.
Title: Re: Bug with the keyboard ?
Post by: Lolilolight on November 11, 2014, 07:43:26 pm
Fixed!

Finally I've found a manual solution to get what I want :

https://github.com/Lolilolight/ODFAEG/tree/odfaeg-lastEngine (https://github.com/Lolilolight/ODFAEG/tree/odfaeg-lastEngine)

I've made a timer to populate events at each time interval, and, I generate events for each keys which are held down so if the keys a and b are pressed I get a b a b a b, I've also set up a priority sustem to get b a b  a instead of a b a b, and a context so if I have a classe which inherits from the window I can get events from this class instead of the sfml window.