I've been using SDL2 on my Macbook with Xcode for a little bit, and I'm very pleased. Lately I tried to receive a focus lost event, but it wasn't working. After some tinkering, I found out that no window events were being received, except a window event that had a type of 512 that showed up 2-5 times a second at random intervals. The WindowEventID
s are in an enum, not in hex format, so it shouldn't be some hex number. I ran a search through the SDL2 framework for 512 and found nothing. Other events, like SDL_QUIT
and SDL_KEYDOWN
work just fine. Anybody know what's going on?
Here is my event loop:
SDL_Event event;
bool running = false;
while(running) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT)
running = false;
else if(event.type == SDL_KEYDOWN) {
cout << event.key.type << endl;
}
else if(event.type == SDL_WINDOWEVENT) {
cout << event.window.type << endl;
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
You need to check event.window.event
rather than event.window.type
, since type is more general and has some other usage (code 512 is related to SDL_WINDOW_INPUT_FOCUS and it triggered whenever you moved cursor to or from window). So your code could be like this:
while(SDL_PollEvent(&event)) {
switch(event.type)
{
case SDL_WINDOWEVENT:
switch(event.window.event)
{
case SDL_WINDOWEVENT_ENTER:
cout << "entered" << endl;
break;
case SDL_WINDOWEVENT_LEAVE:
cout << "left" << endl;
break;
}
break;
case SDL_KEYDOWN:
cout << "key pressed: " << event.key.keysym.sym << endl;
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With