检查Cocoa中Caps Lock的状态 [英] Checking state of Caps Lock in Cocoa
问题描述
好的。我开始我的第一个OS X应用程序。
OK. I'm starting my first OS X application. Instead of doing something obvious or simple, I'm going straight to the system stuff.
我想在菜单栏中放置大写锁定指示符,而不是执行明显或简单的操作。在C#或Delphi(我的主要语言),这是相当微不足道。在Mac OS X中,我不知道从哪里开始。当然,我已经下载Xcode并写了几个没什么风格的应用程序。现在我需要写一个应用程序,它将在开始运行,只有一个菜单栏组件,并工作。
I would like to place a caps lock indicator in the menu bar. In C# or Delphi (my primary languages) this is fairly trivial. In Mac OS X, I'm not really sure where to start. Of course I've downloaded Xcode and written a few little nothing style apps. Now I need to write an app that will run on start, only have a menu bar component, and work.
有我的背景。如何获取Cocoa中的caps lock键的状态?
There's my background. How can I get the state of the caps lock key in Cocoa?
推荐答案
开始运行:直到用户。如果他们希望它在开始运行,它们将把它放在登录项中。您可以添加一个按钮将其自动添加到登录项目;使用启动服务的共享文件列表API将其添加到会话登录项目列表。
Run on start: Up to the user. They will put it in Login Items if they want it to run on start. You can add a button to add it to Login Items automatically; use Launch Services' Shared File List API to add it to the Session Login Items list.
菜单栏项目:这被称为状态项。请参阅NSStatusItem。
Menu bar item: This is called a status item. See NSStatusItem.
检查大写锁定:首先,可能有多个键盘,其中大多数都有大写锁定,而不是所有键盘都具有相同大写锁定状态(用户可能有一个带有它,一个带有它关闭,第三个没有大写锁定键)。
Checking for caps lock: First off, there may be multiple keyboards, most of which will have a caps lock, and not all of which will have the same caps lock state (the user may have one with it on, one with it off, and a third with no caps lock key at all). Pay attention to each keyboard's caps lock state separately, and light up your indicator if any caps lock key is on.
要读取和监视大写锁定键,您将会看到每个键盘的大写锁定键,需要浸入I / O Kit。我相信您需要使用 HID Manager 。这是一个C API,但Dave Dribin已撰写 Objective- C封装器,用于其较低级别的前辈(最近在 1.0.1 )。
To read and monitor the caps lock keys, you'll need to dip into I/O Kit. I believe you'll need to use the HID Manager. That's a C API, but Dave Dribin has written an Objective-C wrapper for its lower-level predecessor (most recently at 1.0.1).
编辑添加:您可以使用Quartz事件敲击监视大写锁定,但不是每键盘,我不确定你会得到多个键盘的可靠结果,它仍然不会让你读取大写锁定键的初始状态,当你的应用程序启动。
Edited to add: You can monitor caps lock using a Quartz event tap, but it's not per-keyboard, so I'm not sure you'll get reliable results with multiple keyboards, and it still won't let you read the initial state of the caps lock keys when your app launches.
这篇关于检查Cocoa中Caps Lock的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!