检查鼠标滚轮状态 SFML

Check mouse wheel state SFML

在我的程序中,我希望能够知道鼠标滚轮是否已经滚动,如果是,在什么方向滚动了多少。这对 C++ 和 SFML 可行吗?

到目前为止我有这个:

if (sf::Event::MouseWheelEvent().delta != 0)
{
    SimulationView.zoom(1 + (10 / sf::Event::MouseWheelEvent().delta));
}

但第二行永远不会退出,即使我滚动鼠标滚轮

您可以将鼠标滚轮作为每帧轮询一次的事件循环的一部分来读取:

int main()
{
    sf::RenderWindow window(sf::VideoMode(320, 256), "Title");

    sf::Event event;
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
            else if(event.type == sf::Event::MouseWheelMoved)
            {
                // display number of ticks mouse wheel has moved
                std::cout << event.mouseWheel.delta << '\n';
            }
        }

        window.clear();

        // draw window here

        window.display();
    }
}

注:SFML 2.3

感谢@Hiura 指出 sf::Event::MouseWheelMovedSFML 2.3 已弃用

Use this instead:

if(event.type == sf::Event::MouseWheelScrolled)
{
    if(event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
        std::cout << "wheel type: vertical" << std::endl;
    else if(event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel)
        std::cout << "wheel type: horizontal" << std::endl;
    else
        std::cout << "wheel type: unknown" << std::endl;

    std::cout << "wheel movement: " << event.mouseWheelScroll.delta << std::endl;
    std::cout << "mouse x: " << event.mouseWheelScroll.x << std::endl;
    std::cout << "mouse y: " << event.mouseWheelScroll.y << std::endl;
}