按下A时,在SFML 2.0中也按下了鼠标左键

时间:2016-10-25 02:09:14

标签: c++ sfml

我刚刚开始学习SFML 2.0,我遇到了一些问题。问题是当我按下按钮“A”然后在输出中它还显示“按下鼠标左按钮”。我正在使用代码::块16.04和SFML 2.0。 “MouseWheelMoved”事件也会发生同样的事情,如果我按任何其他数字然后它会显示消息“MouseWheelMoved”。这是代码。

#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
    sf::RenderWindow window(sf::VideoMode(400,400),"All Events ");
    sf::Event event;
    window.setKeyRepeatEnabled(false);
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
            case sf::Event::Closed:
                window.close();
                break;
            case sf::Event::KeyPressed:
                switch(event.key.code)
                {
                case sf::Keyboard::A:
                    std::cout<<"A Button Pressed "<<std::endl;
                    break;
                case sf::Keyboard::S:
                    std::cout<<"S Button Pressed "<<std::endl;
                    break;
                case sf::Keyboard::D:
                    std::cout<<"D Button Pressed "<<std::endl;
                    break;
                case sf::Keyboard::W:
                    std::cout<<"W Button Pressed "<<std::endl;
                    break;
                case sf::Keyboard::Return:
                    std::cout<<"Enter Button Pressed"<<std::endl;
                    break;
                case sf::Keyboard::Up:
                    std::cout<<"Up Button Pressed "<<std::endl;
                    break;
                }
                case sf::Event::MouseButtonPressed:
                    if(event.mouseButton.button == sf::Mouse::Left)     {std::cout<<"Mouse Left Button Pressed "<<std::endl;
                    break;}
                    else if(event.mouseButton.button == sf::Mouse::Right){std::cout<<"Mouse Right Button Pressed"<<std::endl;
                    break;}
                case sf::Event::MouseWheelMoved:
                    std::cout<<"Mouse Wheel Moved "<<event.mouseWheel.delta<<std::endl;
                    break;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

你有两个switch(event.type) // switch 1 { // (...) case sf::Event::KeyPressed: switch(event.key.code) // switch 2 { case sf::Keyboard::A: std::cout<<"A Button Pressed "<<std::endl; break; // <- this breaks switch 2 and... // (...) } // ...you end up here. Here switch 1 doesn't have a break // so it falls through to the next case. case sf::Event::MouseButtonPressed: // (...) // here also falls through. case sf::Event::MouseWheelMoved: std::cout<<"Mouse Wheel Moved "; break; } 嵌套,你错过了其中一个

#!/bin/bash
# My first script
sleep 15 & wait
python pythonFileName.py & wait
python pythonFileName.py & wait
python pythonFileName.py & wait