Like Delete key, detection of Escape key press is also slightly different than detecting general key presses.
if Int(event.keyCode) == kVK_Escape {
print("Escape key pressed!")
}
Below is the full sample code:
NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
if self.keyDown(with: $0) {
return nil // needed to get rid of purr sound
} else {
return $0
}
}
private func keyDown(with event: NSEvent) -> Bool {
if Int(event.keyCode) == kVK_Escape {
print("Escape key pressed!")
return true
} else {
return false
}
}