SDL2_pollevent() 控制器 dpad 连续保持?

SDL2_pollevent() Controller dpad continuous hold?

本文关键字:连续 dpad 控制器 pollevent SDL2      更新时间:2023-10-16

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

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之一。