home ~ projects ~ socials

Get KeyPress Event Data In Swift

Put this in var body: some View { ... }

let x = NSEvent.addLocalMonitorForEvents(matching: .keyDown, handler: {
    event in
    if Int(event.keyCode) == 0x121 {
        
            print(event)
    } else {
        print(event)
    }
        return event
}) ?? "default value"

It's kinda gross, but it prints out the details. For reference, here's an example:

NSEvent: type=KeyDown loc=(149.664,61.125) 
time=426773.7 flags=0x100 win=0x13b978510 
winNum=24270 ctxt=0x0 chars="5" 
unmodchars="5" repeat=0 keyCode=23
-- end of line --