检测 macOS 上键盘是否有 Globe 键

5
最近的苹果产品在键盘上有一个地球符号(︎)。在AppKit级别上,它实际上只是一个行为类似于NSEvent.ModifierFlags.function的fn键。但是,有没有办法知道用户当前使用的键盘是否有地球键,还是只有fn键?
似乎系统以某种方式知道,因为菜单中的快捷键显示会根据键盘而变化。只有当键盘实际上有地球键时,才显示地球符号。否则,显示"fn"。

screenshot of menu with Globe symbol screenshot of menu with fn symbol

但是,到目前为止,我还没有找到在AppKit/SwiftUI世界中以编程方式检测它的方法。我想在我的自定义视图中实现显示这些菜单命令的键盘快捷方式。

1
为什么要给负评呢?这似乎是一个公平的问题。了解用户是否在键盘上有这个按键会很有帮助。 - undefined
我无法测试这个,但你应该继续设置键盘快捷键,就像所有键盘都有一个“功能”键一样,让 macOS 在菜单中显示正确的符号。或者你是想在菜单键盘快捷键之外做些什么? - undefined
@HangarRash 正如我之前提到的,我想在我的视图中绘制快捷方式。 - undefined
2个回答

2

I/O注册表似乎有关于是否存在全球键的信息("SupportsGlobeKey" = YesNo)。

在我的环境中(MacBook Pro 2023),ioreg -rln AppleHIDKeyboardEventDriverV2给出了以下结果。 (我没有台式机,所以无法验证是否缺少全球键)。

$ ioreg -rln AppleHIDKeyboardEventDriverV2
+-o AppleHIDKeyboardEventDriverV2  <class AppleHIDKeyboardEventDriverV2, id 0x100000ba5, registered, matched, active, busy 0 (0 ms), retain 10>
  | {
  |   "LocationID" = 49
  |   "IOPersonalityPublisher" = "com.apple.driver.AppleTopCaseDriverV2"
  |   "Keyboard" = {"Elements"=({"VariableSize"=0,"UnitExponent"=0,"IsRelative"=No,"UsagePage"=7,"Max"=1,"IsArray"=No,"Type"=2,"Size"=1,"Min"=0,"Flags"=134217730,"ReportID"=1,"Usage"=224,"ReportCount"=1,"Unit"=0,"Ha$
  |   "IOMatchCategory" = "IODefaultMatchCategory"
  |   "CapsLockLanguageSwitch" = No
  |   "HIDServiceSupport" = Yes
*snip*
  |   "KeyboardEnabled" = Yes
  |   "PrimaryUsagePage" = 1
  |   "CFBundleIdentifierKernel" = "com.apple.driver.AppleHIDKeyboard"
  |   "SupportsGlobeKey" = Yes
  |   "SensorProperties" = {}
  |   "ProductID" = 0
*snip*

要以编程方式获取I/O注册表信息,请使用IOKit。
例如,要获取是否存在全球键,请使用以下代码。
import IOKit

let entry = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("AppleHIDKeyboardEventDriverV2"))
if let property = IORegistryEntryCreateCFProperty(entry, "SupportsGlobeKey" as CFString, kCFAllocatorDefault, 0)?.takeRetainedValue() {
  print(property) // => 1
}

IOObjectRelease(entry)

1
我找到了一种使用ApplicationServices.HIServices中的私有API来检测它的方法。
HIS_XPC_GetGlobeKeyAvailability();

虽然我只需要使用公共API...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接