How to remove the ability to Paste

  1. 6 days ago

    Bill M

    Feb 12 Sedona, AZ

    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?

    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.

  2. Marc Z

    Feb 12 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Oregon

    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...

  3. Bill M

    Feb 12 Sedona, AZ

    Thanks Mark, I'll give it a try.

  4. Bill M

    Feb 12 Sedona, AZ

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

  5. Marc Z

    Feb 12 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Answer Oregon

    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.

  6. Michel B

    Feb 12 Pre-Release Testers, Xojo Pro
    Edited 6 days ago

    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.

  7. Robert W

    Feb 12 Western Canada

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

  8. Bill M

    Feb 12 Sedona, AZ

    Marc, that worked perfectly. Thanks.

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

  9. 5 days ago

    Norman P

    Feb 13 Pre-Release Testers, Xojo Pro outside listening to the silen...

    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/

  10. 2 days ago

    Arnaud N

    Feb 16 Pre-Release Testers, Xojo Pro

    @Marc Z 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.

    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).

  11. Bill M

    Feb 16 Sedona, AZ

    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.

  12. Emile S

    2 days ago Europe (France, Strasbourg)

    http://docs.xojo.com/TextEdit.ReadOnly ?

  13. Dave S

    2 days ago San Diego, California USA

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

  14. Emile S

    2 days ago Europe (France, Strasbourg)
    If True, the text of the control cannot be modified.

    So this is different than avoid Paste ?

  15. yesterday

    Dave S

    yesterday San Diego, California USA

    @Emile S So this is different than avoid Paste ?

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

or Sign Up to reply!