23

For some reason, an NSTextField isn't allowing me to paste anything into it using Command+V, though I can paste into it if I right-click and click 'Paste'. Why is this happening and how do I fix it?

Chetan
  • 46,743
  • 31
  • 106
  • 145

4 Answers4

10

I fixed it by linking the "Paste" button in my NSMenu Main Menu to the First Responder's paste: command.

Chetan
  • 46,743
  • 31
  • 106
  • 145
  • 1
    That's a menu item (NSMenuItem), not a button (NSButton), and an action message selector, not a command (HICommand). – Peter Hosey Jan 16 '10 at 05:21
  • 2
    There must me more to it than this... I've re-added the IB's standard "Menu Item - Edit" and all the commands in there are correctly linked to the First Responder. But still not getting any keyboard commands. Am I not linking up the "Menu Item - Edit" correctly? – ck_ Nov 29 '11 at 01:39
  • do you have exampe of how you did this? – UKDataGeek Apr 01 '16 at 17:27
  • I had accidentally deleted my NSEdit menu, adding back in via Interface Builder fixed the problem. Thanks. – Supertecnoboff Jul 13 '17 at 16:25
7

Thanks to this Making copy & paste work with NSTextField

final class EditableNSTextField: NSTextField {

private let commandKey = NSEvent.ModifierFlags.command.rawValue
private let commandShiftKey = NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.shift.rawValue

override func performKeyEquivalent(with event: NSEvent) -> Bool {
    if event.type == NSEvent.EventType.keyDown {
        if (event.modifierFlags.rawValue & NSEvent.ModifierFlags.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 & NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue) == commandShiftKey {
            if event.charactersIgnoringModifiers == "Z" {
                if NSApp.sendAction(Selector(("redo:")), to: nil, from: self) { return true }
            }
        }
    }
    return super.performKeyEquivalent(with: event)
}

}

Awais Mobeen
  • 733
  • 11
  • 19
  • This should be the accepted answer. If you've removed the "Edit" menu (for example when making a menu-app) you need to re-implement this back. I have used `extension` of NSTextField instead to allow SwiftUIs TextField and TextEditor to work out of hte box, but this is sufficient too. – norwegiandev Feb 10 '22 at 03:49
  • Can you please tell me how this works and where can I place this code – MacDeveloper Jul 25 '22 at 16:30
  • I implemented tableview programatically. I am not sure about this edit Menu – MacDeveloper Jul 25 '22 at 16:31
  • just add this class to your code, select the NSTextFiled, in the identity inspector assign this class – Awais Mobeen Aug 21 '23 at 10:26
4

What happens when you select "Edit > Paste" from your application's main menu?

Darren
  • 25,520
  • 5
  • 61
  • 71
  • My application sits in the menu bar, it doesn't have a main menu. Could that be the reason? If so, how do I fix it? – Chetan Jan 13 '10 at 08:34
  • 3
    Yes, you need something map your keyboard commands to actions (like paste:), and to map the actions to the First Responder. Setting up a menu is the easiest way to do that. Even if you app's Dock icon and menu bar are hidden, you main menu will still receive keyboard commands. – Darren Jan 13 '10 at 08:51
  • I made a main menu for my application now, but now when I click "Edit", the "Paste" button and all others are grayed out even why my cursor is in a text field. How do I fix this? – Chetan Jan 14 '10 at 06:51
  • 1
    Chetan: You need to hook up the menu items to First Responder. – Peter Hosey Jan 16 '10 at 05:21
3

Did you:

  • Assign a key equivalent to any of your controls (NSButtons, NSMenuItems, ...) that are responding before the Main Menu

-- or --

  • Delete the whole Menu
  • the Edit NSMenuItem
  • the Paste NSMenuItem The "Paste" Shortcut is a key equivalent of the "Paste" NSMenuItem
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • I have that problem too. Initially I have removed the Edit menu (because no need for it in the first), but later I had to reimplement it fro IB: all is working, everything works except 'paste'. the relative action in first responder is there, what can be the problem? – Mike97 Oct 19 '15 at 11:06