I have a textfield in accessory view of NSSavePanel. For user convenience there are default actions supported natively by macOS (such as pressing Enter, keyEquivalent). However this doesn't work for enter under Sonoma. Escape key works. Is enter keypress dangerous for malicious actors so it's not supported? I have workaround below but I am not confident if I am not violating sandbox (future proof).
Original code demonstrating the issue:
class ViewController: NSViewController, NSTextFieldDelegate, NSControlTextEditingDelegate {
let savePanel = NSSavePanel()
override func viewDidLoad() {
super.viewDidLoad()
let customView = NSView()
let textField = NSTextField(string: "11111111")
textField.delegate = self // to get focus using tab keypress
savePanel.accessoryView = textField
}
override func viewWillAppear() {
savePanel.runModal()
}
}
Workaround:
// variable set to true in delegate method controlTextDidEndEditing
var didUseTextFieldWithEnterPressed = false
override func performKeyEquivalent(with event: NSEvent) -> Bool {
if #unavailable(macOS 14) {
return super.performKeyEquivalent(with: event)
}
guard let panel, didUseTextFieldWithEnterPressed == true, event.type == .keyDown &&
(event.keyCode == UInt16(kVK_Return) || event.keyCode == UInt16(kVK_ANSI_KeypadEnter)) else {
return super.performKeyEquivalent(with: event)
}
return panel.performKeyEquivalent(with: event)
}