我在SDL2中有一些Windows焦点问题。
我有两个窗户,听取焦点增益和丢失事件。
当我点击Window 2时,会触发以下事件:
"Window 1 lost focus"
"Window 2 gained focus."
当我点击Window 1时,会触发以下事件:
"Window 2 lost focus."
"Window 1 gained focus."
"Window 1 lost focus."
我可以通过操作系统围绕它的发光效果清楚地告诉窗口有焦点 此外,在窗口1上测试时,获取焦点信息的其他SDL2函数给出相同,错误的答案。
我将代码缩减到几乎最小的测试用例:
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int main(int argc, char **argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* w1=SDL_CreateWindow("Window 1",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
250,200,SDL_WINDOW_SHOWN);
SDL_Window* w2=SDL_CreateWindow("Window 2",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
200,250,SDL_WINDOW_SHOWN);
bool quit=false;
while(!quit){
SDL_Event e;
while(!quit && SDL_PollEvent(&e)){
switch(e.type){
case SDL_WINDOWEVENT :
{ // this block just scopes 'targetWindow' and 'title'
SDL_Window* targetWindow=SDL_GetWindowFromID(e.window.windowID);
const char* title=SDL_GetWindowTitle(targetWindow);
switch(e.window.event){
case SDL_WINDOWEVENT_FOCUS_GAINED :
// tell which window gained focus
cout << title << " gained focus!" << endl;
break;
case SDL_WINDOWEVENT_FOCUS_LOST :
// tell which window lost focus
cout << title << " lost focus!" << endl;
break;
}
}
break;
case SDL_QUIT :
quit=true;
break;
}
}
}
SDL_Quit();
return 0;
}
这是SDL2多窗口支持中的错误吗? 它取决于底层的窗口系统吗?
更重要的是,有没有办法为SDL2的多个窗口提供正确的焦点信息?
提前感谢您的帮助。
答案 0 :(得分:1)