Here is my code:
这是我的代码:
void MainGame::ProcessInput()
{SDL_Event Evnt;
while (SDL_PollEvent(&Evnt));
{switch(Evnt.type)
{case SDL_QUIT: {_GameState = GameState::EXIT;}
break;
case SDL_MOUSEMOTION: {}
break;
case SDL_KEYDOWN: {_InputManager.PressedKey(Evnt.key.keysym.scancode);}
break;
case SDL_KEYUP: {_InputManager.ReleasedKey(Evnt.key.keysym.scancode);}
break;
case 771: {std::cout << "Info1 = " << Evnt.key.keysym.scancode << std::endl;
_InputManager.PressedKey(Evnt.key.keysym.scancode); }
break;
}
}
}
So in this code, if I push up, down, left and right arrows, KEYDOWN is working properly. If I hit any letters on my keyboard, it will only return code 771. Here I made a test including 771 in my switch. When I hit letters, it does go to 771 and print a silly number like 30445778. And this number change every time I re-run the program. Man what's going on here?!? please help me
所以在这段代码中,如果我向上,向下,向左和向右箭头,KEYDOWN正常工作。如果我点击键盘上的任何字母,它只返回代码771.在这里,我在我的开关中进行了测试,包括771。当我点击字母时,它会转到771并打印一个像30445778这样的愚蠢数字。每次重新运行程序时,这个数字都会改变。男人这里发生了什么事?!?请帮我
1 个解决方案
#1
0
The problem you are facing is an additional ;
after your while
condition. Citing SDL wiki, your code should be written as follows:
你面临的问题是额外的;在你的条件之后。引用SDL wiki,您的代码应编写如下:
while (1) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
/* handle your event here */
}
/* do some other stuff here -- draw your app, etc. */
}
Note that there's no semicolon after while
.
请注意,之后没有分号。
However, although the code you've written does not do what you want it to do, it's still correct from C++ point of view.
但是,尽管您编写的代码没有按照您的意愿执行,但从C ++的角度来看,它仍然是正确的。
This line:
while (SDL_PollEvent(&Evnt));
will enter a loop that will iterate up until the point SDL_PollEvent
returns 0
, while doing nothing.
将进入一个迭代的循环,直到SDL_PollEvent返回0,而什么都不做。
After that, your switch
statement will only run once, processing the last polled event (disregarding its type). In the same time, your switch
statement is not ready to handle most of the event types.
之后,您的switch语句将只运行一次,处理最后一次轮询的事件(忽略其类型)。同时,您的switch语句尚未准备好处理大多数事件类型。
#1
0
The problem you are facing is an additional ;
after your while
condition. Citing SDL wiki, your code should be written as follows:
你面临的问题是额外的;在你的条件之后。引用SDL wiki,您的代码应编写如下:
while (1) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
/* handle your event here */
}
/* do some other stuff here -- draw your app, etc. */
}
Note that there's no semicolon after while
.
请注意,之后没有分号。
However, although the code you've written does not do what you want it to do, it's still correct from C++ point of view.
但是,尽管您编写的代码没有按照您的意愿执行,但从C ++的角度来看,它仍然是正确的。
This line:
while (SDL_PollEvent(&Evnt));
will enter a loop that will iterate up until the point SDL_PollEvent
returns 0
, while doing nothing.
将进入一个迭代的循环,直到SDL_PollEvent返回0,而什么都不做。
After that, your switch
statement will only run once, processing the last polled event (disregarding its type). In the same time, your switch
statement is not ready to handle most of the event types.
之后,您的switch语句将只运行一次,处理最后一次轮询的事件(忽略其类型)。同时,您的switch语句尚未准备好处理大多数事件类型。