Beispiel #1
0
        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);
        }
        private async void UpdateClipboard()
        {
            Indicator.IsBusy = true;
            if (ProfileComboBox.IsLoaded && ProfileComboBox.SelectedValue != null && ProfileComboBox.SelectedValue as string != "Default")
            {
                if (ProfileComboBox.Items.Contains("Default"))
                {
                    UpdateProfileComboBox(false, ProfileComboBox.SelectedValue as string);
                }

                Clipboard.ClearHistory();

                using (LiteDatabase db = new LiteDatabase($"Filename={Path.Combine(documents, "Auto Paste Clipboard", "data.db")}; Connection=shared"))
                {
                    ILiteCollection <ClipboardProfile> collection = db.GetCollection <ClipboardProfile>("clipboard");

                    ClipboardProfile clipboardProfile = collection.FindOne(x => x.Profile == ProfileComboBox.SelectedValue as string);

                    foreach (string item in clipboardProfile.Clipboard)
                    {
                        DataPackage data = new DataPackage();
                        data.SetText(item);
                        Clipboard.SetContent(data);
                        await Task.Delay(450);
                    }

                    DelimiterComboBox.SelectedIndex = clipboardProfile.Delimeter;
                    DelayUpDownBox.Value            = clipboardProfile.Delay;
                    ProfileNameTextBox.Text         = ProfileComboBox.SelectedValue as string;
                }
            }
            UndoChangesBtn.IsEnabled = false;
            Indicator.IsBusy         = false;
        }
Beispiel #3
0
        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;
            });
        }
        private async void UpdateClipboardListView()
        {
            clipboardHistoryItems.Clear();
            List <ListViewItem>         clipboardTexts = new List <ListViewItem>();
            ClipboardHistoryItemsResult items          = await Clipboard.GetHistoryItemsAsync();

            foreach (ClipboardHistoryItem item in items.Items.Reverse())
            {
                if (item.Content.Contains(DataFormats.Text))
                {
                    string data = await item.Content.GetTextAsync();

                    clipboardTexts.Add(new ListViewItem()
                    {
                        Content = data
                    });
                    clipboardHistoryItems.Add(item);
                }
            }

            ClipboardListView.ItemsSource = clipboardTexts;

            foreach (object item in ClipboardListView.Items)
            {
                ((ListViewItem)item).Style = (Style)FindResource(resourceKey: "ListViewItemStyle");
            }
        }
        private void SetHotkey(object sender, RoutedEventArgs e)
        {
            if (Clipboard.IsHistoryEnabled() && HkTextBox.Hotkey != null)
            {
                string[] modifiers = HkTextBox.Hotkey.Modifiers.ToString().Split(',').OrderByDescending(d => d == "Control").ThenBy(a => a).ToArray(); //Following Ctrl + Shift + Alt Ordering Convention
                string   hotkey    = $"{ (modifiers[0] == "None" ? "" : string.Join(" + ", modifiers) + " + ") } {HkTextBox.Hotkey.Key}";

                try
                {
                    using (LiteDatabase db = new LiteDatabase(Path.Combine(documents, "Auto Paste Clipboard", "data.db")))
                    {
                        ILiteCollection <Hotkey> collection = db.GetCollection <Hotkey>("hotkey");

                        if (collection.Count() > 0)
                        {
                            collection.DeleteAll();
                        }

                        collection.Insert(HkTextBox.Hotkey);
                    }

                    HotkeyManager.Current.AddOrReplace("Paste", HkTextBox.Hotkey.Key, HkTextBox.Hotkey.Modifiers, AutoPaste);
                    MessageBox.Show($"{hotkey} has been successfully set as the hotkey.", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
                    WindowState = WindowState.Minimized;
                }
                catch (HotkeyAlreadyRegisteredException)
                {
                    MessageBox.Show($"{hotkey} is being used by another application.", "Alert", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("You must enable clipboard history on windows settings and set a hotkey.", "Alert", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }
Beispiel #6
0
        static Task <string> PlatformGetTextAsync()
        {
            var clipboardContent = WindowsClipboard.GetContent();

            return(clipboardContent.Contains(StandardDataFormats.Text)
                ? clipboardContent.GetTextAsync().AsTask()
                : Task.FromResult <string>(null));
        }
Beispiel #7
0
        static Task PlatformSetTextAsync(string text)
        {
            var dataPackage = new DataPackage();

            dataPackage.SetText(text);
            WindowsClipboard.SetContent(dataPackage);
            return(Task.CompletedTask);
        }
Beispiel #8
0
 public void Copy(DataPackage data, bool flush = true)
 {
     data.RequestedOperation = DataPackageOperation.Copy;
     Clip.SetContent(data);
     if (flush)
     {
         Clip.Flush();
     }
 }
Beispiel #9
0
 static Func <string, CancellationToken, Task> CreateAsyncSet()
 {
     return((text, cancellation) =>
     {
         var dataPackage = new DataPackage();
         dataPackage.SetText(text);
         var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
         return dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UapClipboard.SetContent(dataPackage))
         .AsTask(cancellation);
     });
 }
Beispiel #10
0
        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!"); };
        }
Beispiel #11
0
 /// <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();
     }
 }
        private async void AutoPaste(object sender, HotkeyEventArgs e)
        {
            try
            {
                CTS = new CancellationTokenSource();
                CancellationToken cancelToken = CTS.Token;

                if (WindowState != WindowState.Minimized)
                {
                    WindowState = WindowState.Minimized;
                    await Task.Delay(TimeSpan.FromSeconds(1), cancelToken);
                }

                InputSimulator input = new InputSimulator();

                for (int i = 0; i < clipboardHistoryItems.Count; i++)
                {
                    Clipboard.SetHistoryItemAsContent(clipboardHistoryItems[i]);
                    await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                    input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V);

                    switch (DelimiterComboBox.SelectedIndex)
                    {
                    case 1:
                        await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                        input.Keyboard.KeyPress(VirtualKeyCode.TAB);
                        break;

                    case 2:
                        await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                        input.Keyboard.KeyPress(VirtualKeyCode.RETURN);
                        break;

                    case 3:
                        await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                        input.Keyboard.KeyPress(VirtualKeyCode.SPACE);
                        break;

                    case 4:
                        await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                        input.Keyboard.KeyPress(VirtualKeyCode.OEM_COMMA);
                        break;

                    case 5:
                        await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                        input.Keyboard.KeyPress(VirtualKeyCode.OEM_PERIOD);
                        break;

                    case 6:
                        await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                        input.Keyboard.KeyPress(VirtualKeyCode.OEM_COMMA);
                        await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                        input.Keyboard.KeyPress(VirtualKeyCode.SPACE);
                        break;

                    case 7:
                        await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                        input.Keyboard.KeyPress(VirtualKeyCode.OEM_PERIOD);
                        await Task.Delay((int)DelayUpDownBox.Value, cancelToken);

                        input.Keyboard.KeyPress(VirtualKeyCode.SPACE);
                        break;

                    default:
                        break;
                    }
                }
                e.Handled = true;
            }
            catch (OperationCanceledException) { }
            catch (Exception ex) //handles async exceptions
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (CTS != null)
                {
                    CTS.Dispose();
                    CTS = null;
                }
            }
        }
 private void ClearClipboard(object sender, RoutedEventArgs e)
 {
     Clipboard.ClearHistory(); UndoChangesBtn.IsEnabled = false;
 }