游戏中的键盘输入(用于 GLUT)
几乎每个游戏都使用键盘作为输入。我已经在这个主题上搜索了两天并发现了很多相关内容。键盘有很多缺点,但我发现的主要问题是布局不同,其次是如果您同时按 3 个键,可能会导致损坏(行列错误)。如果您不知道我在说什么,键盘被制成网格,它会检查哪一行和哪列已连接。但是如果你按E,D(第1,2行第3列) 和R(第1行,第4列),键盘甚至可以显示F,因为它发现它被按下(第2行,第4列都被按下)。
所以我认为我们对此无能为力,但如果有人知道如何更好地解决它然后使用不形成 L 的键,我会很高兴:)
但我的主要问题是不同的键盘布局真正的痛苦。我是斯洛伐克人,所以斯洛伐克数字的布局如下所示: +ľščťžýáíé 和班次 1234567890,我们也有 QWERTZ,但您可以使用 QWERTY。 你们都知道英语是什么样子的,但可以肯定的是: 1234567890 和 shift !@#$%^&*()
大多数时候我用的是英文的,因为我在编程时已经习惯了。无论如何,不同的人使用不同的布局。当您制作依赖于按下哪个键的游戏时,例如良好的旧 WASD 模式,您不能在 AZERTY 布局的法国游戏上使用它。这会很奇怪。与动作游戏中使用数字选择枪支相同。如您所见,斯洛伐克语必须按 Shift 键才能使其正常工作。
我也在使用 OpeGL。当您映射按下的键时出现问题。例如,广泛使用的为每个字符制作 256 个布尔值的地图的解决方案就受到了 SHIFT 的影响。你按下 a,SHIFT,然后松开 a,你就会得到:a 向下,A 向上。所以我考虑将一些键绑定在一起,如 A 和 a、1 和 !,但后来我意识到我只需更改布局即可,但一切都是错误的。
那么解决方案是什么?我认为有人在游戏行业或制作了一些游戏并且必须解决这个问题。我想到的唯一解决方案是强制用户界面采用英语布局(并选择聊天布局)。
经过下一次搜索,我找到了我需要的东西,但我需要跨平台的: 虚拟键码
下一个搜索显示SDL key
结果:永远不要从 GLUT 开始如果你要制作游戏,请使用 SFML 或 SDL
感谢大家帮助我,这方面的问题比较多,所以键绑定/映射、SDL 等等的想法,每个都帮助了我很多.
Almost every game use keyboard as input. I have been searching for 2 days on this topic and found quite much about it. Keyboards have many disadvantages, but main problems I found are different layouts and second that if you are pressing 3 keys at time, it can lead to corruption (row-column error). If you don't know what I'm talking about, keyboard is made as grid and it checks which row and column has connected. But if you press E,D (row 1,2 column 3)
and R (row 1, column 4), keyboard can show even F because it find it pressed(row 2, column 4 both pressed).
So I think we can't do anything about that second, but if anyone got idea how to solve it better then use keys that don't form L, I'd be glad :)
But my main problem are different keyboard layouts which is real pain. I'm slovak so Slovak layout of numbers look like this:
+ľščťžýáíé and with shift 1234567890, we also got QWERTZ but you can use QWERTY.
You all know how the English look like but just for sure:
1234567890 and shift !@#$%^&*()
Most of time I'm using english one because I got used to it when programming. Anyway there are different people using different layouts. When you are making game that depend on which key is pressed, for example good old WASD pattern, you can't use that on french one which is AZERTY layout. It would be strange. Same as using numbers for choosing gun in action game. As you can see slovak would have to press shift to get it work.
I'm also using OpeGL. There is problem when you are mapping which keys are pressed. For example widely used solution to make map of 256 bools for each charakter, is suffering from SHIFT. You press a, SHIFT and release a you got: a down, A up. So I thought about binding some keys together, as A and a, 1 and !, but then I realized I'll just change layout and everything is wrong.
So what is solution for that? I think there is someone out there that is in game industry or made some game and had to solve this. Only solution that comes on my mind is to force english layout for UI (and choosen layout for chat).
After next searching I found what I need but I need cross-platform one:
virtual key codes
And next search revealed SDL key
Result: Don't ever start with GLUT if you go making games, use SFML or SDL
Thanks to everyone for helping me, there were more problems in this so idea of key binding/mapping, SDL and so on, each helped me alot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果每次用户按下某些内容时您都会得到一个“字符”,那么您的键盘例程不适合游戏输入 - 仅适合文本输入。
您需要使用完全忽略键盘布局切换并在某种原始键码上进行操作的输入例程(因此,当用户按下shift+a时,您会知道按下了shift并且按下了“a”键,但您不会' t 得到“A”字符)。另外,例程应该允许您查询是否按下了某个键。
对于 Windows 来说是 DirectInput 或 XInput。对于跨平台,这是 libsdl 和 SDL_GetKeyState。您需要为用户提供键盘映射选项。过剩可能不适合您的任务。
If you're getting a "character" every time user presses something, then your keyboard routines aren't suitable for game input - only for text entry.
You need to use input routines that completely ignore keyboard layout switching and operate on some kind of raw keycodes (so when user presses shift+a, you'll know that shift is pressed and that "a" key is pressed, but you won't get "A" character). Also, routines should allow you to query if a key is pressed or not.
For windows that is DirectInput or XInput. For cross-platform this is libsdl and SDL_GetKeyState. And you'll need to provide keymapping options for the user. Glut probably isn't suitable for your task.
常见的做法似乎是忽略这个问题。在早期阶段,“更坏就是更好”。
不幸的是,我使用的是 svorak 键盘布局,所以它并不只适合我。
我一直通过绑定到键盘上的多个键来解决同样的问题。这样玩家就可以从
x
和j
键跳跃。它在非射击跳跃类游戏中表现不佳。如果您能在键盘上找到行/列或一些靠近驱动程序的接口,那就太好了。
一些自动键盘配置软件会很简洁,尽管我还没有见过类似的东西。也许我们应该写一个?
The common approach seems to be to ignore the problem. Worse-is-better in its early stage.
Unfortunately I'm using svorak keyboard layout so it really doesn't just work for me.
I've been approaching this same problem by binding into multiple keys on the keyboard. So that player jumps from both
x
andj
-keys. It doesn't do so well in something that isn't shoot-jump -kind of game.Nice stuff would be if you could just find row/col or some driver-near interface to your keyboard.
Some auto-keyboard configuration software would be neat though I've not yet seen anything like that. Maybe we should write one?
首先,将按键与文本输入分开。当您按下带有 Shift 的键时,您也不应该关心会出现什么字母或数字 - 操作系统应该处理该问题并生成一个事件,您可以在需要文本的极少数情况下使用。通常,您应该只查找按键和任何 Shift 按键并对其进行操作。
其次,从数据文件加载从键到命令的绑定,而不是对其进行硬编码。分发 QWERTY 的默认绑定以及您拥有的任何默认布局。如果数据格式非常简单,那么人们不会介意对其进行自定义以适应他们的键盘和偏好。您还可以稍后添加游戏内按键绑定编辑器。
这实际上与 OpenGL 无关,因为默认情况下它不关心按键。也许您正在使用为您处理按键的插件库或扩展 - 确保您使用的任何内容都可以独立地为您提供单独的键值和 shift/alt/ctrl 的状态,并且它还通过独立系统提供文本输入。
First up, separate keypresses from text entry. You shouldn't care what letter or number comes up when you press a key with shift as well - the operating system should handle that and generate an event you can use in the rare times you need the text. Usually, you should just look for the key press and any shift presses and act on those.
Second, load the bindings from keys to commands from a data file, rather than hardcoding it. Distribute default bindings for QWERTY and whatever default layout you have. If the data format is quite straightforward then people won't mind customising it to fit their keyboard and preferences. You can also add an in-game keybinding editor later.
This isn't really about OpenGL since by default that doesn't care about keypresses. Perhaps you are using an addon library or extension that handles keys for you - ensure that whatever you're using can give you individual key values and the state of shift/alt/ctrl independently, and that it also provides text input via an independent system.
允许您的用户定义用于每个操作的键...
或使用箭头键..这应该是非常通用的:)
Allow your users to define the keys to use for each action ...
or use the arrow keys .. that should be pretty universal :)
将键盘输入转换为元数据,这样您就可以允许用户随意配置,但也可以根据配置文件中使用的键盘布局提供不同的键盘快捷键。
Turn the keyboard input into metadata, so you could allow users to configure at their will but also provide different keyboard shortcuts depending on the keyboard layout used in a config file .