如何使用 SDL 实现同时按键重复?
如何使用 SDL 检测同时按键重复?现在,我可以检测到同时按下的按键(但如果全部按下则无法检测到)或按下的单个按键。我希望能够检测到在任一时间按下的所有键。
我的代码
SDL_EnableKeyRepeat (100, 200);
while (SDL_PollEvent (&event)) {
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_t) {
} else if (event.key.keysym.sym == SDL_y) {
}
} else if (blah) {
//blah blah blah
}
}
How do you detect simultaneous key repeats with SDL? Right now I can detect keys pressed at the same time (but not if they are all held down) or a single key held down. I want to be able to detect all the keys that are held down at any one time.
My code
SDL_EnableKeyRepeat (100, 200);
while (SDL_PollEvent (&event)) {
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_t) {
} else if (event.key.keysym.sym == SDL_y) {
}
} else if (blah) {
//blah blah blah
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先检测所有按下的键可能被证明是不可能的。由于键盘限制,大多数键盘支持同时按下 4-6 个按钮,之后按下/释放按键将无法注册。其次我不建议使用按键重复。不知道其他人的体验可能有所不同,但是您可以通过存储您感兴趣的密钥状态来获得更好的准确性。即在简单的游戏中,这将是方向键。您只需设置按下左键时向左移动,并在释放左键或按下右键时取消设置。如果您需要它来进行诸如打字之类的操作,您可能可以使用一些数组 displayed_keys 来实现它,并将按键 ID 放入按下的按键中。 (我假设你正在尝试制作类似游戏的东西)。
First detecting all keys that are held down might prove impossible. Due to keyboard limitations, most keyboards support 4-6 buttons pressed at the same time, after that key presses/releases wont register. Second i wouldn't suggest using key repeat. Dunno maybe other peoples experience differs, but you can get better accuracy by storing states of keys that interest you. I.e. in simple game that would be directional keys. And you just set moving left when left is pressed, and unset it when left is released or maybe when right is pressed. If you need it for something like typing you could probably make it using some array pressed_keys and put key ids there of keys that are pressed. (Im assuming your trying to make something game like).