Exemplo n.º 1
0
        private string FetchData()
        {
            string text = Guid.NewGuid() + "";
            Clipboard.SetText(text);

            Assert.AreEqual(text, Clipboard.GetText());

            ClipboardSnapshot beforeSnapshot = ClipboardSnapshot.CreateSnapshot(IntPtr.Zero);
            Assert.IsTrue(beforeSnapshot.ContainsData);
            Assert.IsTrue(beforeSnapshot.HasFormat(KnownClipboardFormats.CF_UNICODETEXT));

            using (var session = new ClipboardSession(IntPtr.Zero))
            {
                session.ClearClipboard();
            }

            IntPtr dataPointer = beforeSnapshot.FetchDataPointer(KnownClipboardFormats.CF_UNICODETEXT);
            string clipboardText = Marshal.PtrToStringAuto(dataPointer);

            Assert.AreEqual(text, clipboardText);

            return clipboardText;
        }
        public void RemoveClipboardData(int index)
        {
            IgnoreClipboardActivity = true;

            if (index > -1 && index < _clipboardItems.Count)
            {
                ClipboardItem item = _clipboardItems[index];
                _clipboardItems.Remove(item);

                if (ClipboardDataRemoved != null)
                {
                    ClipboardDataRemoved(index);
                }

                IgnoreClipboardActivity = false;

                if (_clipboardItems.Count == 0)
                {
                    //no items left, clear the clipboard

                    //let's not set the session owner to anything else than the already used owner at the time.
                    using (var session = new ClipboardSession(WindowHandle))
                    {
                        if (session.IsOpen)
                        {
                            session.ClearClipboard();
                        }
                    }
                }
                else
                {
                    //the thing we just removed - was it selected?
                    if (index == _selectedClipboardItemIndex && _clipboardItems.Count > 0)
                    {
                        SelectedClipboardItemIndex = 0;
                    }
                }
            }
            else
            {
                IgnoreClipboardActivity = false;
            }
        }
Exemplo n.º 3
0
        private string ClearClipboard()
        {
            string text = Guid.NewGuid() + "";
            Clipboard.SetText(text);

            Assert.AreEqual(text, Clipboard.GetText());

            ClipboardSnapshot beforeSnapshot = ClipboardSnapshot.CreateSnapshot(IntPtr.Zero);
            Assert.IsTrue(beforeSnapshot.ContainsData);

            using (var session = new ClipboardSession(IntPtr.Zero))
            {
                session.ClearClipboard();
            }

            ClipboardSnapshot afterSnapshot = ClipboardSnapshot.CreateSnapshot(IntPtr.Zero);
            Assert.IsFalse(afterSnapshot.ContainsData);
            Assert.IsFalse(afterSnapshot.HasFormat(KnownClipboardFormats.CF_TEXT));

            Assert.AreNotEqual(text, Clipboard.GetText());

            return Clipboard.GetText();
        }
        private void KeyboardHookKeyUp(object sender, RawKeyEventArgs args, ref bool handled)
        {
            _wasVDownOnKeyUp = _isVDown;
            _wasRightControlDownOnKeyUp = _isRightControlDown;
            _wasLeftControlDownOnKeyUp = _isLeftControlDown;

            if (args.Key == Key.V)
            {
                _isVDown = false;
            }
            else if (args.Key == Key.LeftCtrl)
            {
                _isLeftControlDown = false;
            }
            else if (args.Key == Key.RightCtrl)
            {
                _isRightControlDown = false;
            }

            if (!_messageHook.IgnoreClipboardActivity)
            {

                var previousHandled = (_wasLeftControlDownOnKeyUp || _wasRightControlDownOnKeyUp) && _wasVDownOnKeyUp; ;
                CheckHook();
                handled = _handled;

                if (handled && args.Key == Key.Escape)
                {

                    KillActiveWindowShowThresholdThreads();

                    if (IsVisible)
                    {

                        Hide();
                    }

                    using (var session = new ClipboardSession(_messageHook.WindowHandle))
                    {
                        if (session.IsOpen)
                        {
                            session.ClearClipboard();
                        }
                    }

                }

                if (!handled && previousHandled)
                {
                    //we need to paste - there was some kind of release.

                    _messageHook.IgnoreClipboardActivity = true;

                    KillActiveWindowShowThresholdThreads();

                    if (IsVisible)
                    {
                        Hide();

                        _messageHook.SwapClipboardItemPositions(lstClipboardContents.SelectedIndex, 0);
                    }

                    var sendPasteThread = new Thread(delegate()
                                                         {

                                                             Thread.Sleep(50);

                                                             //paste data out
                                                             SendPasteCombination();

                                                             _wasLeftControlDownOnKeyDown
                                                                 = false;
                                                             _wasLeftControlDownOnKeyUp
                                                                 = false;
                                                             _wasRightControlDownOnKeyDown
                                                                 = false;
                                                             _wasRightControlDownOnKeyUp
                                                                 = false;
                                                             _wasVDownOnKeyDown = false;
                                                             _wasVDownOnKeyUp = false;

                                                             _isVDown = false;

                                                             var returnIgnoreThread = new Thread(delegate()
                                                             {

                                                                 Thread.Sleep(50);
                                                                 _messageHook.IgnoreClipboardActivity = false;

                                                             });
                                                             returnIgnoreThread.IsBackground = true;
                                                             returnIgnoreThread.Start();

                                                         });
                    sendPasteThread.IsBackground = true;
                    sendPasteThread.Start();

                }

            }
        }