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:

if (Clipboard.IsHistoryEnabled())
{
// Retrieve history items
}

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:

await Launcher.LaunchUriAsync("ms-settings:clipboard");

view raw
Launcher.cs
hosted with ❤ by GitHub

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

Clipboard.HistoryEnabledChanged += (S,e) =>
{
if (Clipboard.IsHistoryEnabled())
{
// Retrieve history items
}
}

Retrieving history items

We can retrieve chipboard history using the GetHistoryItemsAsync method:

var historyItems = await Clipboard.GetHistoryItemsAsync();
if (historyItems.Status == ClipboardHistoryItemsResultStatus.Success)
{
foreach (var item in historyItems.Items)
{
//
}
}

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:

var view = item.Content;
if (view.Contains(StandardDataFormats.Bitmap))
{
var bitmapReference = await view.GetBitmapAsync();
if (bitmapReference != null)
{
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await bitmapReference.OpenReadAsync());
viewer.ContentGrid.Children.Add(new Image() { Width = 300, Height = 300, Stretch = Stretch.None, Source = bitmap });
}
}
else if (view.Contains(StandardDataFormats.Text))
{
var text = await view.GetTextAsync();
if (text != null)
{
viewer.ContentGrid.Children.Add(new TextBlock() { Text = text });
}
}

view raw
GetContent.cs
hosted with ❤ by GitHub

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.