Skip to content

Instantly share code, notes, and snippets.

@TorstenC
Last active March 15, 2022 17:41
Show Gist options
  • Save TorstenC/f63703ea01a6d5a9261a6e5b9bfd9df6 to your computer and use it in GitHub Desktop.
Save TorstenC/f63703ea01a6d5a9261a6e5b9bfd9df6 to your computer and use it in GitHub Desktop.
Way to monitor the Clipboard without polling
public MainWindow()
{
InitializeComponent();
SharpClip = new SharpClipboard();
SharpClip.ClipboardChanged += ClipboardChanged;
}
private readonly SharpClipboard SharpClip;
private bool IsTriggered = false;
private async void ClipboardChanged(object sender, ClipboardChangedEventArgs e)
{
if (!IsTriggered)
{
IsTriggered = true;
await DoCheckClipboard();
}
}
private async Task DoCheckClipboard()
{
await Task.Delay(20);
var text = string.Empty;
try
{
if (Clipboard.ContainsText(TextDataFormat.Html))
{
text = Clipboard.GetText(TextDataFormat.Html);
// ^^ URL from M$ Edge / Chromium
}
else if (Clipboard.ContainsText(TextDataFormat.UnicodeText))
{
text = Clipboard.GetText(TextDataFormat.UnicodeText);
// ^^ URL from Firefox
}
}
catch (Exception ex)
{
text = ex.Message;
}
IsTriggered = false;
// undone - check for valid URL
var yesNo = await Task.Run(() => MessageBox.Show(text, "Link übernehmen?", MessageBoxButton.OKCancel, MessageBoxImage.Question));
// undone - sync with Bitly
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment