-
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;
}
}
}
-
Which operating system and SFML revision do you use?
Can you show a minimal and complete example (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) (so that we're sure there is no other bug in your code)? Please use [code=cpp] tags, not [quote].
-
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
-
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.
-
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!