如何仅在美国键盘上仅知道其位置的情况下确定是否按下了某个键? [英] How to determine if a certain key is pressed, knowing only its position on U.S. keyboards?

查看:92
本文介绍了如何仅在美国键盘上仅知道其位置的情况下确定是否按下了某个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑一下,对于Windows视频游戏,我需要确定是否已按下在美国英语键盘布局上生成`和〜字符的键(通常位于Escape键下方,并保留为1).这听起来像是一个琐碎的问题,但对我而言似乎不是一个问题.

Consider that, for a Windows video game, I need to determine if the key which generates the ` and ~ characters on the U.S. English keyboard layout (which is usually below the Escape key and left to 1) has been pressed. This may sound like a trivial question, but it doesn't seem like one to me.

Windows发送键盘消息时,将指定虚拟键代码和OEM扫描代码.我们不能依靠OEM扫描代码,因为价值取决于OEM",也不能依靠虚拟键代码,因为它取决于当前活动的键盘布局.

When Windows sends keyboard messages, it specifies the virtual key code and the OEM scan code. We can't rely on the OEM scan code, because "the value depends on the OEM" - and nor can we depend on the virtual key code, because it depends on the currently active keyboard layout.

我们当前的解决方案"是使用LoadKeyboardLayoutMapVirtualKeyEx查找在美国英语键盘布局上生成`字符的按键的OEM扫描代码,然后仅监听该OEM扫描代码.问题是,如果用户未安装美国英语版式,则无法使用此功能.

Our current "solution" is to use LoadKeyboardLayout and MapVirtualKeyEx to find the OEM scan code of the key that generates the ` character on the U.S. English keyboard layout, then just listen for that OEM scan code. The problem is that this doesn't work if the user doesn't have the U.S. English layout installed.

在Windows上有真正的方法吗?

Is there a real way to do this on Windows?

推荐答案

OEM扫描代码在键盘之间不会更改.没有理由不使用它.

The OEM scancode does not change from keyboard to keyboard. No reason not to use it.

在DOS时代,相同的扫描代码已用于游戏,因为它是检测按键和按键事件的唯一方法.没有人遇到任何问题,我怀疑它会在将来改变.

Back in the DOS-days the same scancodes have been used for games because it has been the only way to detect key-up and key-down events. Noone had problems with it and I doubt it will change in the future.

如果需要其他选项,则可以尝试DirectInput-API.它也为您提供原始的扫描代码,如果我没记错的话,您还可以查询每个键的物理位置,尺寸和其他信息.

If you want another option you may want to give DirectInput-API a try. It gives you the raw scancodes as well and if I'm not mistaken you can also query the physical position, dimension and whatnot of each key.

这篇关于如何仅在美国键盘上仅知道其位置的情况下确定是否按下了某个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆