title: How to get selected text from other applications and keeping clipboard text at the same time
body: <p>Iam trying to get selected text from other applications using the folowing four lines and i have not problem with this:</p>
  
  <pre><code>Thread.Sleep(20);
  SendCtrlC(GetWindowUnderCursor());
  Thread.Sleep(30);
  label1.Text = Clipboard.GetText();
  </code></pre>
  
  <p>My problem is that at the same time i want to keep clipboard text because <code>SendCtrlC(GetWindowUnderCursor());</code> changes the clipboard text, to do that i have used a variable <code>before</code> like the following code:</p>
  
  <pre><code>string before = ""; //use before to keep clipboard text
  
  //keep clipboard text
  IDataObject iData = Clipboard.GetDataObject();
  if (iData.GetDataPresent(DataFormats.Text))
  before = ((String)iData.GetData(DataFormats.Text));
  
  //get selected text
  Thread.Sleep(20);
  SendCtrlC(GetWindowUnderCursor());
  Thread.Sleep(30);
  label1.Text = Clipboard.GetText();
  
  //return clipboard text back
  Clipboard.SetText(before);
  </code></pre>
  
  <p>But it doesn't keep the text and clipboard data still lose...<br>
  someone know what is the reason?
  <br><br>
  here is all of the code:</p>
  
  <pre><code>using MouseKeyboardActivityMonitor;
  using MouseKeyboardActivityMonitor.WinApi;
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Runtime.InteropServices;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
          private readonly KeyboardHookListener m_KeyboardHookManager;
          private readonly MouseHookListener m_MouseHookManager;
          private bool ctrlHeld;
          string pressedKey;
          public Form1()
          {
              InitializeComponent();
  
              m_KeyboardHookManager = new KeyboardHookListener(new GlobalHooker());
              m_KeyboardHookManager.Enabled = true;
              m_KeyboardHookManager.KeyDown += HookManager_KeyDown;
              m_KeyboardHookManager.KeyUp += HookManager_KeyUp;
  
              m_MouseHookManager = new MouseHookListener(new GlobalHooker());
              m_MouseHookManager.Enabled = true;
              m_MouseHookManager.MouseDown += HookManager_MouseDown;
              m_MouseHookManager.MouseUp += HookManager_MouseUp;
  
          }
  
          private void HookManager_KeyDown(object sender, KeyEventArgs e)
          {
              pressedKey = e.KeyCode + "";
              if (pressedKey == "LControlKey")
              {
                  ctrlHeld = true;
              }
          }
  
          private void HookManager_KeyUp(object sender, KeyEventArgs e)
          {
              pressedKey = e.KeyCode + "";
              if (pressedKey == "LControlKey")
              {
                  ctrlHeld = false;
              }
          }
  
          private void HookManager_MouseUp(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left &amp;&amp; (ctrlHeld))
              {
                  string before = "";
  
                  IDataObject iData = Clipboard.GetDataObject();
                  if (iData.GetDataPresent(DataFormats.Text))
                      before = ((String)iData.GetData(DataFormats.Text));
  
                  Thread.Sleep(20);
                  SendCtrlC(GetWindowUnderCursor());
                  Thread.Sleep(30);
                  label1.Text = Clipboard.GetText();
  
                  Clipboard.SetText(before);
  
              }
          }
  
  
          private void HookManager_MouseDown(object sender, MouseEventArgs e)
          {
  
          }
  
  
          [DllImport("user32.dll")]
          public static extern IntPtr WindowFromPoint(Point lpPoint);
  
          [DllImport("user32.dll")]
          public static extern bool GetCursorPos(out Point lpPoint);
  
          public static IntPtr GetWindowUnderCursor()
          {
              Point ptCursor = new Point();
  
              if (!(Form1.GetCursorPos(out ptCursor)))
                  return IntPtr.Zero;
  
              return WindowFromPoint(ptCursor);
          }
  
          /////////////////////////////////////////////////////////
  
          [DllImport("User32.dll")]
          private static extern bool SetForegroundWindow(IntPtr hWnd);
  
          [DllImport("user32.dll", CharSet = CharSet.Auto)]
          static public extern IntPtr GetForegroundWindow();
  
          [DllImport("user32.dll")]
          static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
  
          private void SendCtrlC(IntPtr hWnd)
          {
              uint KEYEVENTF_KEYUP = 2;
              byte VK_CONTROL = 0x11;
              SetForegroundWindow(hWnd);
              keybd_event(VK_CONTROL, 0, 0, 0);
              keybd_event(0x43, 0, 0, 0); //Send the C key (43 is "C")
              keybd_event(0x43, 0, KEYEVENTF_KEYUP, 0);
              keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);// 'Left Control Up
  
          }
      }
  }
  </code></pre>