gpt4 book ai didi

swift - NSEvent.keyCode 到 Swift 中的 unicode 字符

转载 作者:行者123 更新时间:2023-11-28 06:01:03 26 4
gpt4 key购买 nike

我正在尝试开发一个 macOS 应用程序,我需要监听按键事件。

我可以使用以下类来做到这一点:

import Cocoa

class EditorWindow: NSWindow {
override func keyDown(with event: NSEvent) {
super.keyDown(with: event)
Swift.print("Caught a key down: \(event.keyCode)!")
}
}

我能够得到每个字母的键码,例如A = 0S = 1

但是,在这种情况下,关键代码似乎不太适合 Unicode 标量,因此我无法将这些关键值转换为 Character

有什么方法可以将 NSEvent.keyCode 输出的 UInt16 输出转换为 CharacterString?

最佳答案

NSEvent 有一个名为 characters 的属性:

var characters: String?

The characters associated with a key-up or key-down event.

Declaration

var characters: String? { get }

Discussion

These characters are derived from a keyboard mapping that associates various key combinations with Unicode characters. This property is only valid for key-up and key-down events. It raises an NSInternalInconsistencyException if accessed on any other kind of event object.

This property is set to an empty string for dead keys, such as Option-e. However, for a key combination such as Option-Shift-e this property is set to the standard accent ("´").

For a list of constants corresponding to commonly-used Unicode characters, see NSText.

关于swift - NSEvent.keyCode 到 Swift 中的 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49724924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com