How to remove the ability to Paste

Trying to stop people from being able to paste into my apps textarea

Is there a command I can invoke, or something else that will stop them from being able to use either ctrl-p or the paste from the edit menu?

It’s been a while, but I think you can add a menu handler for the paste menu item and return true (meaning you’ll handle it, not the OS) and then don’t actually do anything inside the handler…

Thanks Mark, I’ll give it a try.

Nope, didn’t work. Still able to paste in with ctrl-v (or using the menu item paste).

Weird. I played around this and you’re right, it’s not working.

I then added an EnableMenuItems event and put in editPaste.enabled = false there, but even that doesn’t get honored: the paste command is still available. Bizarre!

Seems to me the paste command is so low-level it’s overriding what you do in Xojo.

I thought of a hack way to get around it:

  • add mouseEnter and mouseExit events to your textarea.
  • add a string property called oldClipboard.
  • in mouseEnter put this code:

dim c as new clipboard oldClipboard = c.text c.text = ""

  • in mouseExit put the reverse:

dim c as new clipboard c.text = oldClipboard

I tested this and it worked. The paste command still fires, but since there’s nothing to paste, nothing happens. And since you restore the old clipboard when the mouse leaves the field, the user shouldn’t notice anything amiss. There’s probably a more elegant way, but this might get you by.

Change the name of the Edit menu Paste from “EditPaste” to “Edpaste”. And add an EdPaste menu handler on App.

That will disable people doing Ctrl-V /Cmd-V. As well as Edit menu Paste.

But you still need to disable Right Click Paste (Ctrl-Click on Mac).

You can do that by using ConstructContextualMenu/ContextualMenuAction.

Has anyone checked to see what happens if you subclass the textarea, and add an EditPaste menu handler to the subclass?

Marc, that worked perfectly. Thanks.

I would have tried MIchel’s solution, but I don’t want to mess it up.

I’d subclass the text area and deal with it in that subclass enable menu items and menu handlers
https://www.great-white-software.com/blog/2020/01/17/subclasses-are-your-friends/

[quote=475187:@Marc Zeedar]I thought of a hack way to get around it:

  • add mouseEnter and mouseExit events to your textarea.
  • add a string property called oldClipboard.
  • in mouseEnter put this code:

dim c as new clipboard oldClipboard = c.text c.text = ""

  • in mouseExit put the reverse:

dim c as new clipboard c.text = oldClipboard

I tested this and it worked. The paste command still fires, but since there’s nothing to paste, nothing happens.[/quote]
I’m afraid you’re not quite right, some things may happen:
1: (not a big problem) The user sees the Edit menu flickering and thinks something happened (which isn’t the case)
2: The Undo menu may appear enabled even though nothing changed (not sure about that one; it differs between platforms)
3: (one important thing) If there’s some text selected in the field, it would get replaced by “” (thus deleting the selection).

I pulled the paste menu item, along with Marc’s advice and have stopped all paste into my program. No flicker, no paste menu item.

http://documentation.xojo.com/api/deprecated/textedit.html#textedit-readonly ?

Emile… Readonly is probably NOT what the OP wanted… He indicated “disable PASTE”, not disable User entry… there is a big difference

If True, the text of the control cannot be modified.

So this is different than avoid Paste ?

It is if the intent is to let the user TYPE, but not PASTE