处理 SDL 和 C++ 中的关键事件
我正在将程序从 GLUT 迁移到 SDL。在我当前的程序中,按 a
键会产生与按 A
键不同的响应。这在 GLUT 中非常简单,因为键盘函数回调传递了按下的键的 ASCII 值。
void keyPressedFn(unsigned char key, int x, int y){
switch(key){
case 'a':
// do work for a
break;
case 'A':
// do work for A
break;
}
}
我正在努力在 SDL 中复制类似的功能,因为无论是否同时按下 SHIFT 或 CAPS LOCK,按下 a
键都会产生相同的响应。
有没有一种简单的方法可以在 SDL 中复制上述功能?
编辑:在上面的示例中,我只展示了如何处理一个键,但在实践中,我将列出大约 15 个键,如果按下 Shift 键,我希望对这些键做出不同的响应以及。
I'm in the process of migrating a program from GLUT to SDL. In my current program pressing the a
key results in a different response then pressing the A
key. This was pretty straightforward to do in GLUT as it the keyboard function callback passed in the ASCII value of the key that was pressed.
void keyPressedFn(unsigned char key, int x, int y){
switch(key){
case 'a':
// do work for a
break;
case 'A':
// do work for A
break;
}
}
I'm struggling to replicate similar functionality in SDL as pressing the a
key produces the same response regardless of if SHIFT or CAPS LOCK are pressed as well.
Is there a simple way of replicating the above function in SDL?
Edit: In the example above I only show how to handle one key, in practice however, I am going to have a list of about 15 keys that I want to respond to differently if the shift key is pressed as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查收到 keydown 事件时出现的键盘修饰符。例如:
Check the keyboard modifiers that are present when you get a keydown event. For example:
SDL_keysym
具有mod
字段,其中包含事件发出时修改键的状态。与KMOD_SHIFT
进行位与以检查 Shift 是否已激活。请参阅 SDL wiki。SDL_keysym
hasmod
field, which contains state of modifier keys at the time the event has been emitted. Bit-AND it withKMOD_SHIFT
to check whether Shift has been active. See SDL wiki.为什么不直接这样做呢?
您是否还有其他一些问题无法按照我的建议进行?如果不是,我认为这很简单。
Why not just do this?
Do you have some other concerns that you cannot do as what I have suggested? If not I think this is as simple as it can get.