gpt4 book ai didi

c++ - SDL2 pollevent() Controller d'pad 连续保持?

转载 作者:行者123 更新时间:2023-11-27 23:45:58 25 4
gpt4 key购买 nike

我正在尝试注册 Controller 按钮/方向键按下并持续按住所述按钮,这样它会连续吐出输出,而不是一次按下一个按钮,然后退出轮询事件循环。现在我有一小段虚拟代码,如果我按住一个按钮,我正试图在流中打印它。对这个问题有什么帮助吗?

while( !quit_program )
{
//Handle events on queue
while( SDL_PollEvent( &e ))
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit_program = true;
}
else if(e.type == SDL_CONTROLLERBUTTONDOWN)
{
count++;
cout<<"button pushed# "<<count<<endl;
}
}
}

最佳答案

在您获得 SDL_CONTROLLERBUTTONUP(当然是针对同一个按钮)之前,您可以认为该按钮已被按下。然后计算你可以做这样的事情(对于一个按钮):

bool that_button_pressed{false}; 
while(!quit_program) {
//Handle events on queue
while(SDL_PollEvent(&e)) {
// User requests quit
if(e.type == SDL_QUIT)
quit_program = true;

if (e.type == SDL_CONTROLLERBUTTONDOWN && e.button == a_button_of_your_choice) {
that_button_pressed = true;
}

if (e.type == SDL_CONTROLLERBUTTONUP && e.button == a_button_of_your_choice) {
that_button_pressed = false;
}
}

if (that_button_pressed) {
count++;
// Print or implement your logic
}
}

当然这个计数器也取决于你的循环时间。这里 that_button_pressed 将代表 SDL_GameControllerButton 之一的。

关于c++ - SDL2 pollevent() Controller d'pad 连续保持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50577551/

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