我正在使用CGEventCreateKeyboardEvent并需要知道CGKeyCode要使用的值.
具体来说,我是关键的密钥代码Command.文档提供了其他键的示例:zis 6,shiftis 56.
某处必须有Mac虚拟键码列表?
给定一个没有修饰符的键的键代码,我想产生按shift +键的结果.示例:对于标准美国键盘,<shift> + <period>给出>.
相关的功能是UCKeytranslate,但我需要一些帮助来获得正确的细节.下面的代码片段是一个完整的程序,可以在XCode中运行.该程序的目的是给出<period>来产生字符>.
该计划的结果是:
Keyboard: <TSMInputSource 0x10051a930> KB Layout: U.S. (id=0)
Layout: 0x0000000102802000
Status: -50
UnicodeString: 97
String: a
Done
Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)
获得布局的部分似乎正在工作,但状态代码显示出现了问题.但是什么?
import Foundation
import Cocoa
import Carbon
import AppKit
// The current text input source (read keyboard) has a layout in which
// we can lookup how key-codes are resolved.
// Get the a reference keyboard using the current layout.
var unmanagedKeyboard = TISCopyCurrentKeyboardLayoutInputSource()
var keyboard = unmanagedKeyboard.takeUnretainedValue() as TISInputSource
print("Keyboard: ") …Run Code Online (Sandbox Code Playgroud) 我把一些函数返回CFArray的CFStringRef值.我需要从他们那里得到utf字符串.由于我不想让我的代码太复杂,我做了以下事情:
let initString = "\(TISCreateInputSourceList(nil, false).takeUnretainedValue())"
然后我只是用\ns 分割得到的字符串以获得一个Swift字符串数组.但是,当函数开始返回非ascii字符串时,启动了麻烦.我开始得到像"\ U2345\U2344"这样的字符串.
然后我尝试接受CFArray并迭代它获取值并可能将它们转换为字符串,但我无法从中获取值:
let ar = TISCreateInputSourceList(nil, true).takeUnretainedValue()
for i in 0...CFArrayGetCount(ar) - 1 {
print(">> ( CFArrayGetValueAtIndex(ar, i).memory )")
}
值始终为空.
我怎样才能得到实际值?