See the following code. Everytime i press a key together with the control key, keyboardGetUnicodeString will return '\r' only... How can i make the keyboardGetUnicodeString return the correct String?
import Foundation
func eventCallback(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, context: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
var length = 0
event.keyboardGetUnicodeString(maxStringLength: 0, actualStringLength: &length, unicodeString: nil)
var chars = [UniChar](repeating: 0, count: length)
event.keyboardGetUnicodeString(maxStringLength: chars.count, actualStringLength: &length, unicodeString: &chars)
print("key: \(String(utf16CodeUnits: chars, count: length).uppercased())")
return Unmanaged.passRetained(event)
}
let runLoop = CFRunLoopGetCurrent()
let eventMask = CGEventMask(1 << CGEventType.keyDown.rawValue)
let eventTap = CGEvent.tapCreate(tap: .cgSessionEventTap, place: .headInsertEventTap, options: .listenOnly, eventsOfInterest: eventMask, callback: eventCallback, userInfo: nil)
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0)
CFRunLoopAddSource(runLoop, runLoopSource, .commonModes)
CGEvent.tapEnable(tap: eventTap!, enable: true)
CFRunLoopRun()