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

Author Topic: sf::Event::KeyReleased procing each loop but i am holding the button  (Read 6220 times)

0 Members and 1 Guest are viewing this topic.

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Hello.
As the subject states.
In game i hold button "W" and i get sf::Event::KeyReleased triggered with key "W"

Code
case sf::Event::KeyReleased:
                        switch(eventList.key.code)
                        {
                        case sf::Keyboard::W:
                                        objBoo.isKWR = true;
                                        std::cout << "relased w" << std::endl;
                                break;

                        default:
                                break;
                        }

I get "released w" printed each loop... but i am holding the button didn't release


The whole function
void EventHandle::HandleEvents(sf::RenderWindow& renWin, Bools& objBoo, MemberInfo& objMem)
{
        sf::Event eventList;
        while(renWin.pollEvent(eventList))//While there are event to be handled
        {
                //Name input
                if(Bools::isGame_MemberInfo_TypingName == true)
                {
                        switch(eventList.type)
                        {
                                case sf::Event::TextEntered:
                                switch(eventList.text.unicode)
                                {
                                        case 13:
                                                //Return key
                                                Bools::isGame_MemberInfo_TypingName = false;
                                        break;

                                        case 8:
                                                //Backspace key
                                                if(objMem.objHeroUnit.name.size() > 0)
                                                {
                                                        objMem.objHeroUnit.name.erase(objMem.objHeroUnit.name.size() - 1);
                                                }
                                        break;

                                        default:
                                                if(eventList.text.unicode < 128 && objMem.objHeroUnit.name.size() < 14)
                                                {
                                                        objMem.objHeroUnit.name.push_back(static_cast<char>(eventList.text.unicode));
                                                }
                                        break;
                                }
                                default:
                                        break;
                        }
                        objMem.Update();
                }

                //Event handle
                switch(eventList.type)
                {
                case sf::Event::Closed:
                        objBoo.isAppOn = false;
                        break;

                case sf::Event::MouseButtonPressed:
                        switch(eventList.mouseButton.button)
                        {
                        case sf::Mouse::Left:
                                if(Bools::isMBLR == true)
                                {
                                        Bools::isMBLP = true;
                                        Bools::isMBLR = false;
                                        Bools::isGame_MemberInfo_TypingName = false;
                                }
                                break;

                        default:
                                break;
                        }

                case sf::Event::MouseButtonReleased:
                        switch(eventList.mouseButton.button)
                        {
                        case sf::Mouse::Left:
                                Bools::isMBLR = true;
                                break;

                        default:
                                break;
                        }

                case sf::Event::KeyPressed:
                        switch(eventList.key.code)
                        {
                        case sf::Keyboard::Escape:
                                objBoo.isGame_Map = false;
                                objBoo.isMainMenu = true;
                                break;

                        case sf::Keyboard::W:
                                if(objBoo.isKWR == true)
                                {
                                        objBoo.isKWP = true;
                                        objBoo.isKWR = false;
                                }
                                break;

                        case sf::Keyboard::S:
                                if(objBoo.isKSR == true)
                                {
                                        objBoo.isKSP = true;
                                        objBoo.isKSR = false;
                                }
                                break;

                        case sf::Keyboard::A:
                                if(objBoo.isKAR == true)
                                {
                                        objBoo.isKAP = true;
                                        objBoo.isKAR = false;
                                }
                                break;

                        case sf::Keyboard::D:
                                if(objBoo.isKDR == true)
                                {
                                        objBoo.isKDP = true;
                                        objBoo.isKDR = false;
                                }
                                break;

                        default:
                                break;
                        }

                case sf::Event::KeyReleased:
                        switch(eventList.key.code)
                        {
                        case sf::Keyboard::W:
                                        objBoo.isKWR = true;
                                        std::cout << "relased w" << std::endl;
                                break;

                        case sf::Keyboard::S:
                                        objBoo.isKSR = true;
                                break;

                        case sf::Keyboard::A:
                                        objBoo.isKAR = true;
                                break;

                        case sf::Keyboard::D:
                                        objBoo.isKDR = true;
                                break;

                        default:
                                break;
                        }

                default:
                        break;
                }
        }
}
 
« Last Edit: January 10, 2013, 08:29:33 pm by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Event::KeyReleased procing each loop but i am holding the button
« Reply #1 on: January 10, 2013, 06:36:09 pm »
Which operating system and SFML revision do you use?

Can you show a minimal and complete example (so that we're sure there is no other bug in your code)? Please use [code=cpp] tags, not [quote].
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: sf::Event::KeyReleased procing each loop but i am holding the button
« Reply #2 on: January 10, 2013, 07:06:59 pm »
Windows 7 32bit and SFML 2.0 (DD:9 MM:Jan YYYY:2013 downloaded)

This is the minimal code that reproduces error

Above code is my full code witch has same error
int main()
{
    sf::RenderWindow renWin;
    renWin.create(sf::VideoMode(1024, 768, 32), "Title", sf::Style::Default);

    while(renWin.isOpen() == true)
    {
       sf::Event eventList;
        while(renWin.pollEvent(eventList))//While there are event to be handled
        {
            //Event handle
            switch(eventList.type)
            {
            case sf::Event::Closed:
                renWin.close();
                break;

            case sf::Event::KeyPressed:
                switch(eventList.key.code)
                {
                case sf::Keyboard::W:
                    std::cout << "Pressed W" << std::endl;
                    break;
                default:
                    break;
                }

            case sf::Event::KeyReleased:
                switch(eventList.key.code)
                {
                case sf::Keyboard::W:
                        std::cout << "relased w" << std::endl;
                        break;

                default:
                    break;
                }

            default:
                break;
            }
        }
        renWin.clear(sf::Color::Magenta);
        renWin.display();
    }
    return 0;
}
 

The console window is displaying "Pressed W\nreleased W per look" and also additional "released w" when the key is actually released
« Last Edit: January 10, 2013, 07:11:03 pm by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

CrazyBillyO

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: sf::Event::KeyReleased procing each loop but i am holding the button
« Reply #3 on: January 10, 2013, 08:11:55 pm »
The sf::Event::KeyPressed doesn't have a break after its switch.  After its cout statement executes it breaks out of the eventList.key.code switch but not the eventList.type switch, so it's falling through to the sf::Event::KeyReleased case.  Try adding a break statement after the switch in case sf::Event::KeyPressed.  I'd also recommend doing the same for case sf::Event::KeyReleased.  In fact, I'd also consider restructuring the code a little more in this area; switch statements in case statements can get somewhat confusing.

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: sf::Event::KeyReleased procing each loop but i am holding the button
« Reply #4 on: January 10, 2013, 08:35:05 pm »
The sf::Event::KeyPressed doesn't have a break after its switch.  After its cout statement executes it breaks out of the eventList.key.code switch but not the eventList.type switch, so it's falling through to the sf::Event::KeyReleased case.  Try adding a break statement after the switch in case sf::Event::KeyPressed.  I'd also recommend doing the same for case sf::Event::KeyReleased.  In fact, I'd also consider restructuring the code a little more in this area; switch statements in case statements can get somewhat confusing.
Considering the missing "break;" i cant find it :(
Also putting "break;" after switch statement would not run the code after it but break out. Making the code after that (In this case sf::Event::KeyReleased) to not be checked.

Also in switch statement if case is not found "default is run" witch in each case is "break;" and thus will not run the code after it causing unexpected behavior.

About reconstructing code. I am surely considering that in my next project, but as it currently stands i started this way and am gonna finish it this way, just for the sake i don't edit thousand of lines to update the code cause am just training/learning.

EDIT:: I understand what your saying now. Took me some rethinking of what i did
int q = 1;
int i = 1;
switch(q)
{
case 0:
    break;
   
case 1:
    switch(i)
    {
    case 0:
        break;
       
    default:
        break; // Breaking switch(i) but not the switch(q) and thus i get error
    }
   
default:
    break;
}
 

Thank you clever man!
« Last Edit: January 10, 2013, 08:47:57 pm by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0