Accessing Clipboard History in UWP

Windows 10 has an extremely useful but not yet too known feature – clipboard history. Invoked by Win + V keyboard shortcut, you can view your recent clipboard content history. Gone are the times when we copied something only to realize it overwrote a precious snippet of code we wanted to paste somewhere else!

To provide users with easier access to their clipboard history right from within our application, UWP provides a set of convenient APIs and that’s what we will look into in this article.

Checking if history is enabled

To check if the user has enabled clipboard history on the device, use the IsHistoryEnabled() API:

https://gist.github.com/MartinZikmund/7a009ea2c9cd071c16e0598b51a23b48

If you want to prompt the user to enable clipboard history, you can navigate to the appropriate section of system settings with the special ms-settings:clipboard URI:

https://gist.github.com/MartinZikmund/cd095e3aada9fb0b24aa716ecf5f0fe8

In addition to that, we can observe any changes to the history enabling using HistoryEnabledChanged event:

https://gist.github.com/MartinZikmund/a02b54a293b9db35f965375e4e38f08f

Retrieving history items

We can retrieve chipboard history using the GetHistoryItemsAsync method:

https://gist.github.com/MartinZikmund/36e1a109662f5a8475c759d67ab165b6

We must always first check the Status property to ensure history was successfully retrieved, and then we can dive into the history items through the Items property. Each ClipboardHistoryItem has the following properties:

  • Id – an identifier of the item
  • Content – an instance of DataPackageView through which we can get various formats stored in the item
  • Timestamp – date and time when the item was copied into the clipboard

The most interesting is definitely the Content property, which we access the same way as normal Clipboard content. In my sample code, I retrieve bitmap and text items as follows:

https://gist.github.com/MartinZikmund/6a9b3d0d3fdd242b6bcb5cb97c70d888

Note: The GetHistoryItemsAsync method can be called only when the application is in the foreground, otherwise, it fails with AccessDenied status.

Other history APIs

There are some additional clipboard history APIs we leverage for even more flexibility:

  • ClearHistory – call this method to remove all historical items
  • DeleteItemFromHistory – removes a specific item from history
  • SetHistoryItemAsContent – “restores” a history item as the current clipboard content.

Uno Platform support

Uno Platform includes full support read/write access to the current contents of the system Clipboard on iOS, macOS, Android, and WebAssembly.

Unfortunately, clipboard history is currently only supported on Windows, so Uno Platform can’t add support for the APIs mentioned in this article yet. Hey Apple, Google, and W3C, time to code 😀 !

Sample project

I have prepared a sample project demonstrating clipboard history retrieval with full source code available on GitHub.

UWP Clipboard History
UWP Clipboard History
Buy me a coffeeBuy me a coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.