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

Author Topic: Bug with the keyboard ?  (Read 5380 times)

0 Members and 1 Guest are viewing this topic.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Bug with the keyboard ?
« 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.
« Last Edit: November 06, 2014, 03:11:02 pm by Lolilolight »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bug with the keyboard ?
« Reply #1 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).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Bug with the keyboard ?
« Reply #2 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...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Bug with the keyboard ?
« Reply #3 on: November 06, 2014, 07:25:01 pm »
Complete and minimal code please.
Laurent Gomila - SFML developer

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Bug with the keyboard ?
« Reply #4 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.
« Last Edit: November 06, 2014, 09:46:39 pm by Lolilolight »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Bug with the keyboard ?
« Reply #5 on: November 06, 2014, 09:49:43 pm »
  • this ain't a complete code!
  • you could at least make it looks nice..
  • you're really expecting a key *pressed* event when releasing a key?
SFML / OS X developer

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Bug with the keyboard ?
« Reply #6 on: November 06, 2014, 09:50:05 pm »
You're not pressing any new key so it generates only a key released event.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Bug with the keyboard ?
« Reply #7 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.




Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Bug with the keyboard ?
« Reply #8 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)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Bug with the keyboard ?
« Reply #9 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.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Bug with the keyboard ?
« Reply #10 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.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Bug with the keyboard ?
« Reply #11 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.
« Last Edit: November 08, 2014, 06:45:50 pm by Jesper Juhl »

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Bug with the keyboard ?
« Reply #12 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.
« Last Edit: November 10, 2014, 08:55:47 pm by Lolilolight »

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Bug with the keyboard ?
« Reply #13 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

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.