Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there anyway to use the Clipboard class to copy the selected text from the currently active process? I have a keyboard hook set up and I do catch the key press.

Update: Well, I haven't tried anything because I dont know what to try! Is there a way for me to return to my program the selected text from notepad basically.
Posted
Updated 2-Jun-11 22:55pm
v2
Comments
RakeshMeena 3-Jun-11 4:49am    
What have you tried till now? Please post!
Ciumac Sergiu 3-Jun-11 5:12am    
You would like to get the selected text, or copied text? If selected, then it wont be a simple task at all.

It's not very nice! But it works:
C#
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
    {
    foreach (Process proc in Process.GetProcesses())
        {
        if (proc.MainWindowTitle.Contains("Notepad"))
            {
            SetForegroundWindow(proc.MainWindowHandle);
            Thread.Sleep(1000);
            SendKeys.SendWait("^C");
            //IDataObject idata = Clipboard.GetDataObject();
            //Console.WriteLine(idata.ToString());
            if (Clipboard.ContainsText())
                {
                string text = Clipboard.GetText(TextDataFormat.UnicodeText);
                Console.WriteLine(text);
                }
            else
                {
                Console.WriteLine("No Text");
                }
            }
        }
    }
You can cut down the sleep value, but you do need to suspend the task briefly to allow the set foreground window to work.
 
Share this answer
 
As an alternative have you considered embedding a notepad clone into your application that way you have full control over how it behaves. I created an exact clone of notepad exactly for that purpose. You can find it here: http://www.simplygoodcode.com/2012/04/notepad-clone-in-net-winforms.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900