Prevent paste in textfield

Hello,

Is there a way to allow text input via keyboard only and disable paste and DragText in a TextField?

If IsContextualClick Then
return true
End If

The above will prevent paste via right-click, but not using Command-V or DragText.

Thanks.

Lennox

In keydown,

if keyboard.commandkey and key = “V” then Return True End If

[quote=402133:@Greg O’Lone]if keyboard.commandkey and key = “V” then
Return True
End If[/quote]
didn’t stop it for me… those key strokes never even get to keydown

Interesting.

Well the only other thing I can think of is to subclass TextField and implement the EditPaste menu handler with the code:

Return True

I know that works, but you’ll still have to deal with the contextual menu.

or put this code in the keydown event of the window ( and not the control)

Thanks Greg, Jean and Dave,

Does not work in control or window.

Any other suggestions?

I saw a this in Stackoverflow, how could I get it to work in Xojo?
https://stackoverflow.com/questions/24424214/disable-copy-or-paste-action-for-text-box

Thanks again.

Lennox

Deleting the “Paste” menu item from the “Edit” menu does it but all controls are affected, any better solution?
But text can still be dragged into the textField successfully.

Thanks.

Lennox

How about disabling the “Paste” menu item if the text field has the focus?

Hi Thomas, I dont quite get that, could you explain a little more? Would that stop dragging text to the TextField?

Thanks again.

Lennox

[quote=402133:@Greg O’Lone]In keydown,

if keyboard.commandkey and key = “V” then Return True End If[/quote]

Btw doesn’t work on MacOS either …

Also tried a small “v” just in case …

I hate it when basic things like this don’t work …

[quote=402193:@Markus Winter]Btw doesn’t work on MacOS either …

Also tried a small “v” just in case …

I hate it when basic things like this don’t work …[/quote]
That’s what I get for answering from in front of the TV. :stuck_out_tongue:

KeyDown and KeyUp are not even triggered when you paste into a TextField using the keyboard …

[quote=402192:@Lennox Jacob]Hi Thomas, I dont quite get that, could you explain a little more? Would that stop dragging text to the TextField?

Thanks again.

Lennox[/quote]
I think that Thomas was answering to this with other option (disable and not delete the “Paste”)

And for the dragging issue:
TextField1 - Open event - me.AcceptTextDrop
TextField1 - DropObject event - Return

  • at least it works on macOS

[quote=402200:@Alberto De Poo]And for the dragging issue:
TextField1 - Open event - me.AcceptTextDrop
TextField1 - DropObject event - Return

  • at least it works on macOS[/quote]

Funny, was going to post the same, but it also raises a question:

If you DO NOT have me.AcceptTextDrop in the open event, the TextField and TextArea will still accept a Text drop. Maybe THAT should be considered a bug?

I did just a test and it does not work. It seems so that the Edit menu items are handled from the TextField class already. So my idea does not work (or t least not in a simple way).

[quote=402201:@Markus Winter]Funny, was going to post the same, but it also raises a question:

If you DO NOT have me.AcceptTextDrop in the open event, the TextField and TextArea will still accept a Text drop. Maybe THAT should be considered a bug?[/quote]
Exactly, I was trying different things to make it work. Because I don’t know Xojo enough I tried me.AcceptTextDrop = False and of course it doesn’t work that way.

So I guessed, well maybe the drag is a system event that Xojo can’t catch (like the firing of command V), then me.AcceptTextDrop maybe will make Xojo ‘aware’ that a drag is happening and I put MsgBox obj.text in DropObject. I saw that I get the MsgBox and no text on the TextField1, then changed MsgBox to Return.

Well, Alberto’s method works on Mac. Not sure about Windows.

I think Thomas is talking about the Paste menu item

What about setting the TextField as Read-Only ?

I just set a TextField to ReadOnly, and drop a text file is rejected, Paste text data too, and of course typing text too, but you do not wanted that (you specifically say you want to be able to type text from the Keyboard.

Sorry.

Tested on Windows, dragging is not possible by default, you need to add the me.AcceptTextDrop and then you need to handle the drop (add some code to DropObject)