0

I've implemented copying tableView selected cell value by shortcut cmd+C. Now I was trying to implement the pasting cmd+V. When I click on tableView cell, it gets selected. Another click, I'm editing it. While editing, I press cmd+V and nothing happens. I can write text but can't insert by copy-pasting.

Tried to implement it manually via Edit menu -> Paste Cmd+V.

    @IBAction func onPaste(_ sender: Any) {
      let pasteBoard = NSPasteboard.general
      pasteBoard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
      tableView.selectedCell().set??? = pasteBoard.string(forType: NSPasteboard.PasteboardType.string)
    }

But don't know how to set a value on NSCell.

Vito Valov
  • 1,765
  • 1
  • 19
  • 37
  • Consider to use a view based table view. It's much more convenient to handle. Or just update the data model and reload the row. – vadian Jan 29 '18 at 12:01
  • Is the table view cell based or view based? Why don't you use the build in copy and paste? – Willeke Jan 29 '18 at 12:02
  • It's ViewBased table view. I don't know how to use the built in c-p. But I needed to implement a custom copy. So when cmd+c is pressed with row x selected, a specific column y value is copied to pasteboard. – Vito Valov Jan 29 '18 at 12:13
  • And when row x is selected the pasteboard should replace the value of column y? Change the data and reload the row. – Willeke Jan 29 '18 at 12:41
  • yes that would work, but I was wondering what preventing a common text input field from receiving paste command. I thought by default it should work.. – Vito Valov Jan 29 '18 at 13:33
  • It should work, how do you put the string on the pasteboard? – Willeke Jan 29 '18 at 16:01
  • Like this. https://pastebin.com/PUhXJzW0 – Vito Valov Jan 29 '18 at 16:38

3 Answers3

1

My mistake. I haven't payed attention that when I change the Edit Menu action, it removes the default behavior. Since I changed the default action to call my custom method, the :paste action was disconnected from cmd+v shortcut.

Vito Valov
  • 1,765
  • 1
  • 19
  • 37
0

I had the same problem with my app and found, it´s pretty easy to solve.

It is always possible to copy and paste to a NSTextField using the mouse context menu. Copy and paste using the keyboard with cmd-c and cmd-v is automatically available when an edit menu is available.

I added an Edit Menu item which I had removed before and copy paste was immediately available.

Ronald Hofmann
  • 1,390
  • 2
  • 15
  • 26
-1

To allow paste on TextField, create custom TextField class by extending NSTextField :

class EditingTextField: 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)
  }
}
Clegrand
  • 82
  • 4
  • This is very cumbersome and *objective-c-ish* code. Why all the `rawValue`s? `commandKey` and `commandShiftKey ` is actually not needed. Rather than `if (event.modifierFlags.rawValue & NSEventModifierFlags.deviceIndependentFlagsMask.rawValue) == commandKey {` you can simply write `if event.modifierFlags.contains(.command) {` – vadian Jan 29 '18 at 14:51
  • And then what, populate all the table view with this custom class? Expanding the Table View in Document Outline I see it's a NSTextFieldCell actually. Should I extend that instead? – Vito Valov Jan 29 '18 at 15:25