intercept a paste action (CTRL-V or Shift-INS) on Windows

Hi all, how can I detect a Paste action? That can be CTRL-V or Shift-INS.
I want to manipulate the text before it is shown in the TextArea.


Implement a menu handler on the control.

Tim, thanks for the tip, but I don’t get it.
Is there an example app that contains this mechanism and that I could learn from?


(doing this from memory, but it’s something like the following steps)

  • Click on the control in question
  • click the + button in the IDE and choose “Add Menu Handler”
  • pick the menu handler as needed (there should be a default one called EditPaste)
  • your control should now have an EditPaste menu handler that you can put code into - use this to access the ClipBoard
  • be sure to return True to tell the OS you handled the event

Michael, when I can pick the menu handler, the list is empty. not one single handler is there to pick from.
So I get stuck on your third bullet.
When I try this on an example application, I do see the EditPaste…
Any idea what might be missing or wrong with my project?


Sounds like your app is missing a menubar? A new project should come with MainMenuBar which has FileMenu and EditMenus as default. Perhaps your project lost these somehow?

That’s correct…

I tried to copy the MainMenuBar from an empty project to my project, and I could not fix the problem.
Then I tried to export and import it. Failed again.
Finally I copied all my Window and properties to an empty project. All is still working but I don’t get to see the EditPaste when I add a menu handler to a TextArea.

Is there a fix for this?


OK, I could fix it by setting the MainMenuBar as the menu bar on the window. Pretty stupid, I know, but…
Now I have another problem.
When I create a menu handler on the TextArea, it appeats on the same level as “Controls” inside the MainWindow. So I think it works for all TextAreas. Is that normal?
Another issue: when I want to modify the EditPaste in MenuHandlers, it does not seem to have any influence. Before Return True I put a MsgBox and that never shows up when I do a paste…

I have the feeling I’m missing something obvious. I checked the PDF manuals, but there is not a lot of info on Menu Handlers.

Does someone see what I’m doing wrong?



I’m still stuck with this – I can intercept a CTRL-V on a full window but not on one specific item (for instance a TextArea).
I tried to identify the active control with GotFocus, Me, Self…
I simply don’t manage to alter the CTRL-V behavior when the cursor is in one specific TextArea.
Does anyone know how to do this?



You may want to look into

Michel, I think that the part that I don’t get is the subclass part.
I’ll try to read up on that first.



[quote=163344:@Gert Van Assche]Michel, I think that the part that I don’t get is the subclass part.
I’ll try to read up on that first.



What subclass ?

Short recipe, follow step by step :

  • In the menu bar, inspect the EditPaste menuitem (in the Edit Menu of the MainMenuBar)
  • Rename it to EditPaste1
  • Add the EditPaste1 menu handler to your window

Whatever you put in the menu handler, like a msgbox “Pasted” for instance, will now happen when you go Ctrl-V or right click/Paste.

The only little thing is now what the system did for you, you have to do yourself. Basically, you got to get what is on the clipboard and paste it in the textarea with SelText

Thanks Michel.

Note that the new menu handler applies to all fields on the window. If you want to isolate the action to a single field or group of fields and leave the default action on all others, make a subclass.

Alternatively, you can duplicate MainMenuBar, and modify EditPaste to EditPaste1 in MainMenuBar1.

Then in the TextArea events :

Sub GotFocus() self.MenuBar = MainMenuBar1 End Sub

Sub LostFocus() self.MenuBar = MainMenuBar End Sub

That way the modified menu will be used only when the TextArea has the focus, and paste will work as default in all other controls.