How to do copy paste delete in Swift for macOS

Issue #729

1
2
3
4
5
6
7
8
9
10
11
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBAction func copy(_ sender: Any) {
print("copy", sender)
}


@IBAction func paste(_ sender: Any) {
print("paste", sender)
}
}

For delete, we can listen to keyDown in NSWindow

1
2
3
4
5
6
7
8
9
10
11
12
class MyWindow: NSWindow {
override func keyDown(with event: NSEvent) {
super.keyDown(with: event)

guard
let deleteScalar = UnicodeScalar(NSDeleteCharacter),
event.charactersIgnoringModifiers == String(deleteScalar)
else { return }

NotificationCenter.default.post(Notification(name: .didKeyboardDeleteItem))
}
}

Updated at 2020-12-30 05:48:29

Comments