Mac OS X 游戏如何接收低级键盘输入事件? [英] How can Mac OS X games receive low-level keyboard input events?

查看:35
本文介绍了Mac OS X 游戏如何接收低级键盘输入事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

游戏需要对键盘输入进行低级访问.在 Windows 上,有 DirectInput.但是 Mac OS X 游戏开发者使用什么技术呢?

Games need low-level access to keyboard input. On Windows, there's DirectInput. But what technology do Mac OS X game developers use?

显然,有足够多的 Mac 游戏可以恰到好处地输入键盘,而没有常见解决方案的缺点:

Obviously, there's enough Mac games which get keyboard input just right, without the drawbacks of the commonly known solutions:

解决方案 #1:使用 keyUp/keyDown 事件

-(void)keyUp:(NSEvent*)event;
-(void)keyDown:(NSEvent*)event;

不可接受的缺点: keyDown 事件会根据按键重复率"和按键重复延迟"的系统偏好设置重复.这会导致一个初始 keyDown 事件,然后在它开始以系统首选项设置定义的速率重复之前暂停.此方案不能用于连续的关键事件.

Inacceptable drawback: keyDown events are repeated based on the system preference settings for "key repeat rate" and "key repeat delay". This causes an initial keyDown event followed by a pause before it starts repeating at a rate defined by a system preference setting. This solution can not be used for continuous key events.

我想知道是否可以禁用按键重复行为?我想您可以读取第一个 keyDown 键,然后在您的类中记住带有 keyCode x 的键已关闭"状态,直到收到该键的 keyUp 事件,从而绕过重复延迟和重复率问题.

I wonder if the key repeat behavior can be disabled? I suppose that you could read the first keyDown key, then remember the "key with keyCode x is down" state in your class until the keyUp event is received for that key, thus bypassing the repeat delay and repeat rate issues.

解决方案 #2:使用 Quarts Event Taps

请参阅 Quartz 事件服务参考.它似乎足够低级.

See Quartz Event Services Reference. It seems to be sufficiently low-level.

不可接受的缺点: 需要在通用访问 - 键盘下的系统首选项中启用辅助设备.虽然这可能默认开启,但不能保证它可能会在某些系统上关闭.

Inacceptable drawback: requires Assistive Devices to be enabled in system preferences under Universal Access - Keyboard. While this may be on by default, there is no guarantee that it might be turned off on some systems.

我还读到 Quartz 事件点击需要应用程序以 root 身份运行,但没有找到对此的确认.

I also read that Quartz event taps require the app to run as root, but found no confirmation for this.

解决方案 #3:碳事件/IOKit HID

碳事件管理器参考 被标记为旧版,不应用于新开发.

The Carbon Event Manager Reference is marked as legacy and should not be used for new development.

不可接受的缺点:没有人知道未来的 Mac OS 版本会继续支持 Carbon 事件多长时间.

Inacceptable Drawback: no one knows how long the Carbon events will continue to be supported in future Mac OS versions.

除了 Carbon 是一个遗留框架之外,这似乎仍然是最好的解决方案.但是使用 Carbon Events 还有其他缺点吗?

Apart from Carbon being a legacy framework, this still seems to be the best solution. But are there any other drawbacks for using Carbon Events?

问题:

Mac OS X 游戏开发人员使用哪种技术来接收低级键盘输入事件?如果他们使用上述技术之一,他们如何解决我提到的缺点?

Which technology do Mac OS X game developers use for receiving low-level keyboard input events? If they use one of the above technologies, how do they work around the drawbacks I mentioned?

更新:

我最终转向使用常规的 NSEvent 消息并将它们包装在 用于轮询键盘状态的简洁 API.

I eventually turned to using the regular NSEvent messages and wrapped them in a neat API for polling the keyboard states.

推荐答案

我在#3 上运气不错,但是如果你想支持键盘以外的任何东西,这需要很多繁重的工作.

I have had good luck with #3, but it requires a lot of heavy lifting if you want to support anything beyond the keyboard.

不过,在我们深入研究之前,请快速指出一点,Carbon 和 IOKit HID 是两个独立的东西.碳可能会在某个时候消失.但 IOKit HID 将继续存在,并在 10.5 中进行了一次不错的改造.

One quick point before we dive in though, Carbon and IOKit HID are two separate thing. Carbon may go away at some point. But IOKit HID is here to stay and just got a nice facelift in 10.5.

有关这些东西如何组合在一起的完整示例,请查看 https://github.com/OpenEmu/OpenEmu/blob/master/OpenEmu/OEHIDManager.m.这只是其中的一小部分,因为其中还有其他文件.

For a full example of how this stuff all fits together, take a look at https://github.com/OpenEmu/OpenEmu/blob/master/OpenEmu/OEHIDManager.m. That is a small piece of the puzzle as there are other files in there as well.

可以找到您想要做的文档 http://developer.apple.com/library/mac/#documentation/DeviceDrivers/Conceptual/HID/new_api_10_5/tn2187.html

The documentation for what you're wanting to do can be found http://developer.apple.com/library/mac/#documentation/DeviceDrivers/Conceptual/HID/new_api_10_5/tn2187.html

同样,这不会很快消失,并且与 Carbon 和 Carbon Events 完全分开.

Again, this is not going to disappear anytime soon and is completely separate from Carbon and Carbon Events.

这篇关于Mac OS X 游戏如何接收低级键盘输入事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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