-1

I'm developing my App over XCode on macOS

I don't have a storyboard and I deleted the menu.

I'm using only AppDelegate.

NSTextField without menu doesn't permit shortcuts [CMD+C] [CMD+V].

I want to enable it without enabling the menu but i don't know how.

Edit:

I'm trying to use this:

final class EditableNSTextField: NSTextField {
    private let commandKey = NSEventModifierFlags.command.rawValue
    private let commandShiftKey = NSEventModifierFlags.command.rawValue | NSEventModifierFlags.shift.rawValue
    override func performKeyEquivalent(with event: NSEvent) -> Bool {
        if event.type == NSEventType.keyDown {
            if (event.modifierFlags.rawValue & NSEventModifierFlags.deviceIndependentFlagsMask.rawValue) == commandKey {
                switch event.charactersIgnoringModifiers! {
                case "x":
                    if NSApp.sendAction(#selector(NSText.cut(_:)), to:nil, from:self) { return true }
                case "c":
                    if NSApp.sendAction(#selector(NSText.copy(_:)), to:nil, from:self) { return true }
                case "v":
                    if NSApp.sendAction(#selector(NSText.paste(_:)), to:nil, from:self) { return true }
                case "z":
                    if NSApp.sendAction(Selector(("undo:")), to:nil, from:self) { return true }
                case "a":
                    if NSApp.sendAction(#selector(NSResponder.selectAll(_:)), to:nil, from:self) { return true }
                default:
                    break
                }
            }
            else if (event.modifierFlags.rawValue & NSEventModifierFlags.deviceIndependentFlagsMask.rawValue) == commandShiftKey {
                if event.charactersIgnoringModifiers == "Z" {
                    if NSApp.sendAction(Selector(("redo:")), to:nil, from:self) { return true }
                }
            }
        }
        return super.performKeyEquivalent(with: event)
    }
}
  • That’s going to be awfully confusing; keyboard shortcuts are part of the menu, and shortcuts without any menu don’t make much sense. – Caleb Mar 08 '21 at 04:50
  • 1
    @Caleb "That’s going to be awfully confusing; keyboard shortcuts are part of the menu, and shortcuts without any menu don’t make much sense". ???? I'm not a menu lover. I'm making an app without menu. Has sense to have CMD+C CMD+V option? Please respond to answer. Going out from argument it's helpful like beating salumi. – Santoo Death XXXXXXXXXXXXXX Mar 08 '21 at 05:14
  • `keyDown(with: event)` is a method. Do you have to check `event.type` in `performKeyEquivalent`? – Willeke Mar 08 '21 at 08:57
  • 1
    Duplicate of [Cocoa Keyboard Shortcuts in Dialog without an Edit Menu](https://stackoverflow.com/questions/970707/cocoa-keyboard-shortcuts-in-dialog-without-an-edit-menu) – Willeke Mar 08 '21 at 09:12
  • 1
    @Willeke - "keyDown(with: event) is a method. Do you have to check event.type in performKeyEquivalent?" I'm not a dog of this language, but it's what i'm searching. I don't have the key and I don't know how to implement it. I've declared `@IBOutlet var txFileURL: EditableNSTextField!` but don't wooooooooorksss. How to do CMD+C CMD+V? – Santoo Death XXXXXXXXXXXXXX Mar 08 '21 at 09:45
  • See [Cocoa Keyboard Shortcuts in Dialog without an Edit Menu](https://stackoverflow.com/a/52521538) and don't forget to set the class of the text field in the xib. – Willeke Mar 08 '21 at 11:22
  • 1
    @Willeke - I had forgotten to set up the class of the text field in the xib file...I really didn't know https://stackoverflow.com/a/44364889/2321140 Now i have my keyboard shortcut working, not only my mouse . I didn't have a keyboard before. In my mind only oxygen . Tnkz 4 ya comments – Santoo Death XXXXXXXXXXXXXX Mar 08 '21 at 13:10

1 Answers1

0

if event.type == keyDown(with: event) {

The compiler is complaining here because you’re comparing event.type to the result of a function whose return type is void. I suspect you mean to compare to NSEvent.EventType.keyDown, or just .keyDown for short.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    "The compiler is complaining here because you’re comparing 'event.type' to the result of a function whose return type is 'void'. I suspect you mean to compare to 'NSEvent.EventType.keyDown', or just '.keyDown' for short." you can also delete this code modified. Tnks tha same – Santoo Death XXXXXXXXXXXXXX Mar 08 '21 at 13:18