c-SDL记住最后一个实例";s用户输入



我正在用C编写SDL项目,在输入处理方面遇到了问题。

每当我启动SDL应用程序时,它都会记住最后一个应用程序输入的键,SDL_PollEvent总是重复给出在程序的前一个实例中按下的最后一个键。

顺便说一句,我在WSL窗口中编译代码,我在visual studio环境中尝试过同样的代码,它运行得非常好。有人能解释一下发生了什么吗?

这是示例代码:

#include <SDL2/SDL.h>
#include <stdio.h>
int main() {
SDL_Event event;
SDL_Window* sdlwind;
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "SDL_Init Error : %s", SDL_GetError());
return 1;
}
sdlwind = SDL_CreateWindow("Engine", 0, 0, 1080, 840, SDL_WINDOW_SHOWN);
if(!sdlwind) {
fprintf(stderr, "SDL_CreateWindow Error : %s", SDL_GetError());
return 1;
}
printf("Start");
while (1)
{
while (SDL_PollEvent(&event))
{
printf("%cn", event.key.keysym.sym);
}
if(event.key.keysym.sym == SDLK_a) {
break;
}
}

SDL_DestroyWindow(sdlwind);
SDL_Quit();

return 0;
}

此示例代码已首次输出:

OUTPUT
,
,
,
,
,
,
// etc since the last key pressed in the previous instance was , and after terminating the program by pressing a
// the next output becomes
OUTPUT
a
a
a
a
a
// etc

我尝试删除可执行文件并启动另一个SDL程序,但同样的问题仍然存在。我后来尝试用getchar()正常输入,但它没有注册任何键,所以我不确定,但这可能不是与stdin有关的问题,它必须是SDL和WSL的smthng。

听起来您遇到了与此超级用户问题和针对WSLg的问题#58相同的根本问题。

它在最新的预览版中得到了修复,您可以通过此Microsoft商店链接进行安装(因为您似乎使用的是Windows 11(。

Github问题从未结束,我也从未看到任何关于它的发布说明,所以它可能是一个上游修复程序(也许是FreeRDP(,但对我和超级用户问题的OP来说,它肯定已经解决了。

最新更新