给定一个不带修饰符的按键代码,我想产生按shift + key的结果。示例:对于标准的美国键盘, + 给出>。
相关的函数是UCKeytranslate,但是我需要一些帮助来确保细节正确。下面的代码段是一个完整的程序,准备在Xcode中运行。给出程序的意图是来产生字符>。
该程序的结果是:
Keyboard: <TSMInputSource 0x10051a930> KB Layout: U.S. (id=0)
Layout: 0x0000000102802000
Status: -50
UnicodeString: 97
String: a
Done
Program ended with exit code: 0
获得布局的零件似乎正在工作,但是状态代码表明出了点问题。但是呢
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: ") ; println(keyboard)
// Get the layout
var ptrLayout = TISGetInputSourceProperty(keyboard, kTISPropertyUnicodeKeyLayoutData)
var layout = UnsafeMutablePointer<UCKeyboardLayout>(ptrLayout)
print("Layout: "); println(layout)
// Let's see what the result of pressing <shift> and <period> (hopefully the result is > )
var keycode = UInt16(kVK_ANSI_Period) // Keycode for <period>
var keyaction = UInt16(kUCKeyActionDisplay) // The user is requesting information for key display
var modifierKeyState = UInt32(1 << 17) // Shift
var keyboardType = UInt32(LMGetKbdType())
var keyTranslateOptions = UInt32(1 << kUCKeyTranslateNoDeadKeysBit)
var deadKeyState = UnsafeMutablePointer<UInt32>(bitPattern: 0) // Is 0 the correct value?
var maxStringLength = UniCharCount(4) // uint32
var actualStringLength = UnsafeMutablePointer<UniCharCount>.alloc(1) //
actualStringLength[0]=16
var unicodeString = UnsafeMutablePointer<UniChar>.alloc(255)
unicodeString[0] = 97 // a (this value is meant to be overwritten by UCKeyTranslate)
var str = NSString(characters: unicodeString, length: 1)
var result = UCKeyTranslate(layout, keycode, keyaction, modifierKeyState, keyboardType, keyTranslateOptions,
deadKeyState, maxStringLength, actualStringLength, unicodeString)
// Print the results
print("Status: "); println(result)
var unichar = unicodeString[0];
print("UnicodeString: "); println(String(unichar))
print("String: "); println(str)
println("Done")
编辑
我已按照Ken Thomases的建议重写了代码段。
来自的一些技巧:Graphite还使用了一个使用键码的Swift程序。
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.
let keyboard = TISCopyCurrentKeyboardInputSource().takeRetainedValue()
let rawLayoutData = TISGetInputSourceProperty(keyboard, kTISPropertyUnicodeKeyLayoutData)
print("Keyboard: ") ; println(keyboard)
// Get the layout
var layoutData = unsafeBitCast(rawLayoutData, CFDataRef.self)
var layout: UnsafePointer<UCKeyboardLayout> = unsafeBitCast(CFDataGetBytePtr(layoutData), UnsafePointer<UCKeyboardLayout>.self)
print("Layout: "); println(layout)
print("KbdType "); println(LMGetKbdType()) // Sanity check (prints 44)
var keycode = UInt16(kVK_ANSI_Period) // Keycode for a
var keyaction = UInt16(kUCKeyActionDisplay)
var modifierKeyState = UInt32(1 << 1) // Shift
var keyboardType = UInt32(LMGetKbdType())
var keyTranslateOptions = OptionBits(kUCKeyTranslateNoDeadKeysBit)
var deadKeyState = UInt32(0) // Is 0 the correct value?
var maxStringLength = UniCharCount(4) // uint32
var chars: [UniChar] = [0,0,0,0]
var actualStringLength = UniCharCount(1)
var result = UCKeyTranslate(layout, keycode, keyaction, modifierKeyState, keyboardType, keyTranslateOptions,
&deadKeyState, maxStringLength, &actualStringLength, &chars)
// Print the results
print("Status: "); println(result)
print("Out:"); println(UnicodeScalar(chars[0]))
println("Done")
最佳答案
对于kTISPropertyUnicodeKeyLayoutData
,TISGetInputSourceProperty()
返回CFDataRef
。您需要获取其字节指针,并将其视为UCKeyboardLayout
的指针。我不认为这是您在此行中所做的:
var layout = UnsafeMutablePointer<UCKeyboardLayout>(ptrLayout)
我不太了解Swift,但是它可能会像这样工作:
var layout = UnsafePointer<UCKeyboardLayout>(CFDataGetBytePtr(ptrLayout))
或许:
var layout = CFDataGetBytePtr(ptrLayout) as UnsafePointer<UCKeyboardLayout>
另外,
kUCKeyActionDisplay
几乎没有用。其目的是返回键的标签,但它甚至不能可靠地返回键。您可能要使用kUCKeyActionDown
。对于修饰符,您想使用Carbon时代的
shiftKey
位掩码向右移8位(如UCKeyTranslate()
的文档中所示)。 shiftKey
是1 << 9
,所以shiftKey >> 8
是1 << 1
。对于这些选项,为简单起见,您应该可以使用
kUCKeyTranslateNoDeadKeysMask
。它等效于1 << kUCKeyTranslateNoDeadKeysBit
。是的,对于初始击键或不想应用任何先前的死键状态的
deadKeyState
,0是正确的值。我不确定您为什么用
maxStringLength
注释了uint32
行。该类型与最大字符串长度完全无关。 maxStringLength
是UCKeyTranslate()
应该写入提供的缓冲区的UTF-16代码单元的最大数量(API将其错误地称为“字符”)。它基本上是缓冲区大小,以UniChar
s(不是字节)为单位。在您的情况下,它应该为255。或者,由于您可能不希望从单个击键中获得255个“字符”,因此可以减小缓冲区的大小,并设置maxStringLength
使其匹配。您对
str
的处理很奇怪。您可以在调用unicodeString
之前从UCKeyTranslate()
缓冲区构造它。您是否希望因为UCKeyTranslate()
更改unicodeString
的内容而更改字符串对象的值?它不是。如果确实如此,那将是NSString
中的一个非常糟糕的错误。在 NSString
成功填充该缓冲区之后,您应该从缓冲区构造UCKeyTranslate()
。当然,在构造actualStringLength
时,应将NSString
作为length参数传递。