如果按下某个键,我的SDL2程序将忽略鼠标单击。这是我的MCVE:
#include <SDL.h>
void myEventHandler(bool& mouseClicked, bool& letsQuit)
{
SDL_Event event;
while (SDL_PollEvent(&event))
switch (event.type)
{
case SDL_QUIT: letsQuit = true; break;
case SDL_MOUSEBUTTONDOWN: mouseClicked = true;
}
}
int main(int argc, char** argv)
{
//Init SDL
SDL_Window* sldWindow;
SDL_Renderer* sdlRenderer;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
return -1;
if (!(sldWindow = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,640, 480, 0)))
return -1;
if (!(sdlRenderer = SDL_CreateRenderer(sldWindow, -1, 0)))
return -1;
SDL_ClearError();
//Initialize some conditions
bool letsDrawRed= true; //Draw a red square, not blue
bool letsQuit = false; //Quit the program
while (! letsQuit)
{
SDL_RenderPresent(sdlRenderer);
//static int framesSinceLastMouseClick = 0; //latency
bool mouseClicked = false;
myEventHandler (mouseClicked, letsQuit);
if (letsDrawRed) SDL_SetRenderDrawColor(sdlRenderer, 255, 0, 0, 255); //draw square as red
else SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 255, 255); //else draw it blue
static SDL_Rect rect = { 0, 0, 100, 100 }; //draw the square
SDL_RenderFillRect(sdlRenderer, &rect);
//if clicked, and enough time since last click...
//(All of these commented-out conditions fail too)
//if (framesSinceLastMouseClick > 600 && mouseClicked)
//if (framesSinceLastMouseClick > 600 && SDL_GetMouseState(NULL, NULL))
//if (mouseClicked)
if (SDL_GetMouseState(NULL,NULL))
{
//framesSinceLastMouseClick = 0;
letsDrawRed = !letsDrawRed;
}
//else ++framesSinceLastMouseClick;
}
SDL_DestroyWindow(sldWindow);
SDL_Quit();
return 0;
}
我在评论中留下了一些东西,以证明它们不是问题。一种是等待时间条件(framesSinceLastMouseClick
)。另一个是从SDL_PollEvent
获取鼠标事件,而不是调用SDL_GetMouseState
。)
要注意的另一件事是这仅在按下的键可打印时才是问题。 CapsLock,Shift,Alt,Ctrl和Function键不会造成问题。
平台是MS Windows上的Visual Studio。
那么...在按下某个键时,如何才能找到那只鼠标?在真实游戏中这将是一个问题!
答案 0 :(得分:0)
这不是SDL,而是我所在的计算机,当按下可打印字符时,它无法从鼠标(实际上是触控板)发送信息。显然,从键盘发送多个信号是一个长期存在的问题,结果是“键盘重影”(某些键-在我的情况下是触控板的点击-丢失了)。
发布时,我能够检查键盘/触控板可以在https://keyboardtester.co/mouse-click-tester.html发送的输出。