Link to home
Start Free TrialLog in
Avatar of Estate_Master
Estate_Master

asked on

How to capture and override Ctrl V (Paste) command in VB.Net

I have a VB.Net application and i need some way of capturing and overriding the Ctrl-V (Paste) command so that i can determine what occurs on those key events. I want to my own custom procedure to run on Ctrl-V, not paste.

In my code, i have first captured when the Control key is pressed, then if the V key is pressed. If these are both true, i then run my custom command. I also try to clear the clipboard (because a user would have put something in the clipboard to begin with if they tried to paste something), but what seems to happen is it runs my custom command, tried to empty the clipboard, but then continues to Paste the 'empty' data into the field on my form, thus clearing the field completely! The line Clipboard.SetDataObject(New DataObject) doesnt seem to actually clear the clipboard.

Does anyone know:
1) How i can completely override the Ctrl-V command in my application, so it doesnt paste and it only runs my defined code.
2) How to clear the clipboard, so when ctrl-V is pressed there is nothing to paste. Something similar to Excel's "Application.CutCopyMode = Fales" would be perfect.

Thanks
If e.Control Then
        If e.KeyCode = Keys.V Then
            ***RUN CUSTOM COMMAND HERE****
            Clipboard.SetDataObject(New DataObject)
        End If
    End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial