Delete key press detection is slightly different than other keys. It uses NSDeleteCharacter
like below:
event.charactersIgnoringModifiers == String(UnicodeScalar(NSDeleteCharacter)!)
So, the complete code may look like:
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 event.charactersIgnoringModifiers == String(UnicodeScalar(NSDeleteCharacter)!) {
print("Delete key presses!")
return true
} else {
return false
}
}