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

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

问题描述

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



显然,有足够的Mac游戏可以获得正确的键盘输入,没有常见解决方案的缺点:



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

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

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



我不知道键重复行为是否可以禁用?我想你可以读取第一个keyDown键,然后记住key with keyCode x is down状态,直到接收到该键的keyUp事件,从而绕过重复延迟和重复速率问题。



解决方案#2:使用Quarts事件点击



请参阅 Quartz Event Services参考



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



我还读过Quartz事件tap需要应用程序运行 解决方案#3:



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



不可接受的缺点:没有人知道碳将在以后的Mac OS版本中继续支持事件。



除了Carbon是一个传统框架,这似乎仍然是最好的解决方案。


$ b

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



UPDATE:



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

解决方案

我有幸运#3 ,但是如果你想支持键盘以外的任何东西,它需要很多重的。



在我们潜水之前一个快点,Carbon和IOKit HID是两个独立的事情。碳可能在某点消失。但是IOKit HID在这里留下来,只是在10.5中得到了一个很好的改造。



有关这些东西如何组合在一起的一个完整的例子,看看 https://github.com/OpenEmu/OpenEmu/blob/master/OpenEmu/OEHIDManager.m 。这是一个小块的谜题,因为有其他文件也在那里。



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



同样,这不会很快消失,并且完全独立于碳和碳事件。 p>

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

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

Solution #1: Use keyUp / keyDown events

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

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.

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.

Solution #2: Use Quarts Event Taps

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.

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

Solution #3: Carbon Events / IOKit HID

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

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

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?

Questions:

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?

UPDATE:

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

解决方案

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

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.

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.

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

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

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

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