检查鼠标滚轮状态SFML

时间:2015-06-21 03:42:58

标签: c++ sfml mousewheel

在我的程序中,我希望能够知道鼠标滚轮是否已滚动,如果是,那么在多大方向上滚动了多少。这可能与C ++和SFML一起使用吗?

到目前为止,我有这个:

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

但即使滚动鼠标滚轮,第二行也不会显示

1 个答案:

答案 0 :(得分:5)

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

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::MouseWheelMoved中指出SFML 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;
}