-
-
Save TorstenC/f63703ea01a6d5a9261a6e5b9bfd9df6 to your computer and use it in GitHub Desktop.
Way to monitor the Clipboard without polling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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