static Func <CancellationToken, Task <string?> > CreateAsyncGet() { return(async cancellation => { using ManualResetEvent resetEvent = new(false); var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher; string?value = null; await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { var dataPackageView = UapClipboard.GetContent(); if (dataPackageView.Contains(StandardDataFormats.Text)) { value = await dataPackageView.GetTextAsync() .AsTask(cancellation); } resetEvent.Set(); }) .AsTask(cancellation); resetEvent.WaitOne(); return value; }); }
public async Task <string> CopyTextAsync(int timeout = -1, CancellationToken token = default) { var task = TaskExt.FromEvent <object>() .Start(h => Cp.ContentChanged += h, h => Cp.ContentChanged -= h, token == default ? CancellationToken.None : token); _keyboard.Type(Ctrl + C); try { var o = await task.TimeoutAfter(timeout); } catch (TimeoutException) { Console.WriteLine("timeout of copy text"); return(""); } var dataPackageView = Cp.GetContent(); // foreach (var availableFormat in dataPackageView.AvailableFormats) // { // Console.WriteLine(availableFormat); // } if (dataPackageView.Contains(StandardDataFormats.Text)) { var text = await dataPackageView.GetTextAsync(); Console.WriteLine(text); return(text); } return(string.Empty); }
static Task <string> PlatformGetTextAsync() { var clipboardContent = WindowsClipboard.GetContent(); return(clipboardContent.Contains(StandardDataFormats.Text) ? clipboardContent.GetTextAsync().AsTask() : Task.FromResult <string>(null)); }
public Clipboard() { if (!Cp.IsHistoryEnabled()) { Console.WriteLine("[+] Turning on clipboard history feature..."); try { var rk = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Clipboard", true); if (rk == null) { Console.WriteLine( "[!] Clipboard history feature not available on target! Target needs to be at least Win10 Build 1809.\n[!] Exiting...\n"); return; } rk.SetValue("EnableClipboardHistory", "1", RegistryValueKind.DWord); } catch (Exception ex) { Console.WriteLine(ex); } } Cp.ContentChanged += async(s, e) => { DataPackageView dataPackageView = Cp.GetContent(); foreach (var availableFormat in dataPackageView.AvailableFormats) { Console.WriteLine(availableFormat); } var a = await Cp.GetHistoryItemsAsync(); var b = a.Items; foreach (var it in b) { Console.WriteLine(it.Timestamp); } if (dataPackageView.Contains(StandardDataFormats.Text)) { string text = await dataPackageView.GetTextAsync(); Console.WriteLine(text); } }; Cp.HistoryChanged += async(s, e) => { Console.WriteLine("History Changed!"); }; }
/// <summary> /// This is horribly ghetto but you can only get the clipboard content from certain events. /// Get from button clicks or mouse moves or manipulation started events. /// If you try to do this from the wrong event you will get an AccessDenied exception... but only if /// the debugger is not attached. You have to get all of the content before the DataRequested event /// is fired, so we cache it all into the values dictionary. /// </summary> private async Task SetContent() { this.ErrorText.Text = ""; values.Clear(); try { var content = SystemClipboard.GetContent(); await content.CopyTo(values); } catch (Exception ex) { this.SetErrorMessage(ex.Message); this.copied = false; values.Clear(); } }