Clear Filters
Clear Filters

Trigger event when content on system clipbooard changes

5 views (last 30 days)
I would like to trigger an event and code when the content of the clipboard changes.
I am creating polygons in a GIS-software by using a built in function of that software which captures X and Y coordinates when clicking in a map and sends these coordinates in a standadized manner to the system clipboard. Currently I'm manually pasting these coordinates into an excel sheet column and then use a script to create a SQL-string which is re-imported into the GIS-software.
I would like to initiate a MATLAB listener which triggers when the content of the system cliipboard (i.e. the clipboartd('paste') ) changes so I can automate the whole process and create the SQL-string when e.g. pressing TAB three times in a row. That bit with the TAB I've got nailed down - but the part where an event triggers when the system cliupboard changes has got me stumped.
I believe that I have to create a custom class and create a observable property for the clipboard. I can then listen for changes on that property. But how would I make the property dynamically/automatically update when the system clipboard changes? Is there a way to do this?
I guess one way would be to create a timer and every say two seconds get the value of the clipboard and update the property - but I would very much prefer not to do it this way.
My difficulty may also in no small part be that I'm really a Naval Architect and in all honesty have very little experience in programming with classes. =)
Anyone got some ideas to do this? Or is it impossible and should I just go for the option with the timer instead?
Best regards

Accepted Answer

chrisw23 on 24 Oct 2023
asm = NET.addAssembly("System.Windows.Forms");
% Includes clipboard-class.
fCol = System.Collections.Specialized.StringCollection();
fInfo = System.IO.FileInfo("C:\Temp\dummy.txt");
fClipboard = System.Windows.Forms.Clipboard;
% Static methods:
% Clear ContainsFileDropList GetAudioStream GetFileDropList ReferenceEquals SetDataObject SetText
% ContainsAudio ContainsImage GetData GetImage SetAudio SetFileDropList
% ContainsData ContainsText GetDataObject GetText SetData SetImage
Here's an example how to access the clipboard class in Matlab. Unfortunately there's no event suited for your needs but a bunch of methods you could poll periodically. Check options by methodsview or details cmd.
  1 Comment
Martin on 24 Oct 2023
Well - thank you. This will come in handy - but not for this need. All the "contains..." methods may well come to really good use. I can see a whole sleathe of great things to do with this actually.
I ended up with the "running a timer option" and triggering comparsion with the last unique captured data on the clipboard- That worked out great, and I could also have all the code that detects and validates the data I'm looking for on the clipboard using the same trigger function from the timer.
I could also make a small GUI to start and stiop the timer, and this could also be used for listening for key presses to end the capture of dat afrom the clipboard I wanted to.But I ended up using a button for this instead, since I anyhow had to make the GUI to allow for the key press listener.
It actually seems there is no way to make a listener for changes to the clipboard - at least I have not found a trace of a solution on how to do this.... Which is a shame since this would probably be less use of CPU and system resources. But I suppose you'll just get a Core i9 computer and more ram instead... ;)

Sign in to comment.

More Answers (0)


Find more on Environment and Settings in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!