gpt4 book ai didi

c++ - 检查鼠标滚轮状态 SFML

转载 作者:行者123 更新时间:2023-12-01 14:36:11 25 4
gpt4 key购买 nike

在我的程序中,我希望能够知道鼠标滚轮是否已经滚动,如果是,在什么方向滚动了多少。使用 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;
}

关于c++ - 检查鼠标滚轮状态 SFML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30961071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com