Accessing Clipboard History in UWP

WinUI

4 years ago

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:

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:

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

Retrieving history items

We can retrieve chipboard history using the GetHistoryItemsAsync method:

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:

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