Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am doing a research project and I use Windows API to detect changes in the clipboard. I override the WndProc and so far it works fine and I can detect whenever copy event is happening. But now the problem is how to detect paste event. WM_PASTE message doesn't work in all the scenarios. What I think to do is :

Global hook Ctrl + v , Ctrl + Shift + v
Hook shell context menu and detect when user clicks on paste
Hook application Edit > Paste

Since different applications can implement this paste function in different ways it is a very tedious task and I don't think that is the best way to do it. I would like to have some suggestions about this.

What I have tried:

I have tried to hook Ctrl + v registry hot key and it works fine. But as stated in here

Quote:
Hooks tend to slow down the system because they increase the amount of processing the system must perform for each message. You should install a hook only when necessary, and remove it as soon as possible.


But, In my application continuously I want to monitor is there any paste event is happening or not.
Posted
Updated 24-May-18 1:38am
Comments
Richard MacCutchan 24-May-18 7:36am    
You cannot guarantee to catch paste events as they will not always use the defaults. For example, I have an application that does pastes without any keyboard or mouse events.
Dilan Shaminda 24-May-18 7:42am    
yes that is the problem of the paste event. Thanks for the comment
Jochen Arndt 24-May-18 7:54am    
There is no reliable way to detect paste events because they are handled by the target applications (as you know already).

So you would have to hook into all possible methods that might be used by applications but will probably still miss some because there are applications that implement their own context menu handling and/or use application defined message codes instead of WM_PASTE.
Dilan Shaminda 24-May-18 7:59am    
"there are applications that implement their own context menu" I didn't think about this scenario. Thank you

There is no such thing as a "global paste event" to subscribe to.

What you suggested is about the best way you're going to do this.

Also, hooks don't slow the system down very much at all, but that depends entirely on your hook code. Just don't do a ton of processing on every key stroke and you'll be fine.
 
Share this answer
 
Comments
Dilan Shaminda 24-May-18 7:43am    
To hook the shell context menu I couldn't find any proper windows api function. It would be great if you could help me a link maybe where to start.
Dave Kreskowiak 24-May-18 8:20am    
You don't "hook" the context menu. You write a shell extension.
https://www.codeproject.com/Articles/441/The-Complete-Idiot-s-Guide-to-Writing-Shell-Extens
BillWoodruff 24-May-18 7:49am    
Hi, Dave, looks like GlobalHooks can affect debugging in VS:

https://www.codeproject.com/Messages/5522581/What-is-going-on-with-VS-solved.aspx
As Dave says, there is no such event, WM_PASTE is an instruction not a notification and applications do not even have to wait for user input in order to paste - there is a Clipboard object in .NET which allows applications to directly manipulate the clipboard content, so you will never be able to detect all paste operations regardless of how you do it.

And your keyboard operation list is incomplete as well: SHIFT+INSERT will paste in most applications; but I know of a couple of apps that use CTRL+V for non-paste operations.
 
Share this answer
 
Comments
Dilan Shaminda 24-May-18 7:46am    
Thank you. I think I have to do more research about this and find a way to detect most of the cases.
OriginalGriff 24-May-18 8:03am    
Why are you trying to do this? Paste has nothing to do with changing the clipboard, quite the reverse!
Dilan Shaminda 24-May-18 8:09am    
Actually, this is related to my master thesis. My topic is about protecting data in windows environment. Like an user can copy some confidential document and paste it to a pen drive. To prevent such things to be happened, I am researching and trying to implement a prototype. So that is why I want to capture the paste event
OriginalGriff 24-May-18 8:16am    
I think you need to think about this a whole load more, protecting things from "leaving" the PC is going to be a whole load more effective that preventing copy and paste. For example, what about drag and drop? That doesn't use the clipboard at all, nor does a CMD shell and the "copy" command. And there is also "open file, save in new location" to worry about.

Even if there was a "paste event" it wouldn't do anything against those!
Dilan Shaminda 25-May-18 1:48am    
Yes, not only copy paste but drag and drop and other possibilities as well. Since this is a research project I am searching about the options we have to prevent these in windows environment and what are the capabilities of windows api. Thanks for the valuable comment :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900