如何检测OS X中大写锁键的键向上或键释放? [英] How to detect key up or key release for the capslock key in OS X?

查看:167
本文介绍了如何检测OS X中大写锁键的键向上或键释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了OS X检测键击问题。我需要检测一个键向下和键释放或键向上每当键盘键被按下。当使用 [NSEvent addLocalMonitorForEventsMatchingMask:handler:] 拦截应用程序的事件处理链时,这是相当简单的。这允许您截取和修改各种事件类型的 NSEvent ,包括 NSKeyUp NSKeyDown 用于常规打印键,以及 NSFlagsChanged ,可用于检测shift,ctrl,alt和cmd键。事实上,因为shift,ctrl,alt和cmd键的键向上和向下键都改变了修饰符标志,所以 NSFlagsChanged 可以用作键向上和键通过与 [NSEvent keyCode] 一起检查 [NSEvent modifierFlags]

I'm having a problem with OS X detecting keystrokes. I need to detect a key down AND a key release or key up whenever a keyboard key is pressed. This is fairly straight-forward when intercepting the application's event handling chain with [ NSEvent addLocalMonitorForEventsMatchingMask: handler: ]. That lets you intercept and modify NSEvents for a variety of event types, including NSKeyUp and NSKeyDown for the regular printing keys, as well as NSFlagsChanged which can be used to detect the shift, ctrl, alt, and cmd keys. In fact, because the modifier flags change on both the key up and key down for shift, ctrl, alt, and cmd keys, NSFlagsChanged can be used as a key up and key down event for those keys by checking the [NSEvent modifierFlags] along with the [NSEvent keyCode].

但是Capslock是不同的。因为大写锁定修改器真的只对一个键按下,当你按下capslock你只得到一个 NSFlagsChanged 与大写锁被按下,而不是当它被释放。和 NSKeyUp NSKeyDown 不会用修饰键如大写锁和shift和ctrl等发出。

Capslock is different, though. Because the capslock modifier really only acts on a key down, when you press capslock you only get a NSFlagsChanged with capslock is pressed, not when it's released. And NSKeyUp and NSKeyDown aren't emitted with modifier keys like capslock and shift and ctrl, etc.

任何人都可以建议一种方法,甚至是一个较低级别的接口,用于获取capslock键向上事件?

Can anyone suggest a way, maybe even a lower-level interface, for getting capslock key up events? Am I going to have to resort to using kqueues or something?

推荐答案

IOHIDLib似乎是这样做的唯一方法。我(或者队友实际上)使用IOHIDManager来设置一个事件回调,它成功拦截了大小写锁定键和按键事件。

IOHIDLib seems to be the only way of doing this. I (or a teammate actually) used IOHIDManager to set up an event callback that successfully intercepted capslock key-up and key-down events.

这可怕的是,这完全超出了Cocoa / CoreFoundation事件分派机制,这意味着即使在应用程序失去焦点时也会收到事件。你最终必须自己做很多窗口管理和焦点检测。

The awful thing about this is that this is completely outside the Cocoa/CoreFoundation event dispatch mechanism, which means that you get events even when your application is out of focus. You end up having to do a lot of window management and focus detection yourself.

但它可以工作。

这篇关于如何检测OS X中大写锁键的键向上或键释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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