how to identify whether the clipboard content changed from our app or from another app in UWP?

Prem Kumar S 0 Reputation points
2023-09-05T12:27:37.3166667+00:00

I want to do different action while the Clipboard Content is changed from our app or from another App. Clipboard_ContentChanged event is fired whenever the Clipboard content is changed.If I copy some text from other app , i want to do some different action .how to do it ? How to differentiate the clipboard content changed form our app or from another app in UWP ?

My Code :

public MainPage()
    {
        this.InitializeComponent();
        Clipboard.ContentChanged += Clipboard_ContentChanged;
    }

    private async void Clipboard_ContentChanged(object sender, object e)
    {
        string str = "";
        if (/*ContentChanged fired due to clipboard content changed from our app*/)
        {
            str = "Text Copied from Our App";
            //Action - 1
        }
        else//ContentChanged fired due to clipboard content changed from outside app
        {
            str = "Text Copied from other app";
            //Action - 2
        }
        DispalyText.Text = str + await Clipboard.GetContent().GetTextAsync();
    }

    private void CopyToClipBoard_Click(object sender, RoutedEventArgs e)
    {
        DataPackage dataPackage = new DataPackage();
        dataPackage.SetText("Hello Clipboard !!");
        Clipboard.SetContent(dataPackage);
    }
<Grid>
    <StackPanel>
        <Button x:Name="CopyToClipBoard" Click="CopyToClipBoard_Click">Copy To ClipBoard</Button>
        <TextBlock x:Name="CopiedFrom"> Clipboard Content Changed From : </TextBlock>
        <TextBlock x:Name="DispalyText" FontWeight="Bold"/>
    </StackPanel>
</Grid>

Developer technologies | Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-09-06T02:06:44.1333333+00:00

    Hello,

    Welcome to Microsoft Q&A!

    How to differentiate the clipboard content changed form our app or from another app in UWP ?

    Just talk from the UWP side, there is no UWP Api could achieve what you want. The Clipboard.ContentChanged Event tracks the changes of the data stored in the Clipboard from the system but it won't know the source of the clip data.

    If this feature is very important, you submit a feature request about this feature in the Feedback Hub. You could find the Feedback Hub in the Start Menu. Please select Developer Platform->API Feedback as the category when you submit your request. The related team will check the request.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.