Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Windows forms applications, I want to send Ctrl+C to active window to get the selected text to translate it when Win+C is pressed.

What I have tried:

C#
                    Process p = Process.GetProcessesByName(GetActiveProcessFileName()).FirstOrDefault();
                    if (p != null)
                    {
                        IntPtr h = p.MainWindowHandle;
                        SetForegroundWindow(h);

                        SendKeys.SendWait("^C");

                    }




[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);




[DllImport("user32.dll")]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);


static string GetActiveProcessFileName()
{
    IntPtr hwnd = GetForegroundWindow();
    uint pid;
    GetWindowThreadProcessId(hwnd, out pid);
    Process p = Process.GetProcessById((int)pid);
    return p.MainModule.FileName;
}
Posted
Updated 20-Jan-18 4:30am
v2
Comments
Richard MacCutchan 20-Jan-18 10:11am    
Do you have a question?
john1990_1 20-Jan-18 10:17am    
The code works only on "notepad" application, in the other cases it doesn't enter the:

if(p != null)...

p remains null when the active process is not Notepad.
john1990_1 20-Jan-18 10:20am    
I now encountered this about not being able to retrieve 64 bit apps (when i tried to retrieve name of Chrome, please give me a new code that really send Ctrl+C to active window to retrieve selected text and put it in clipboard.
Richard MacCutchan 20-Jan-18 10:21am    
Sorry, but I do not understand what you mean, or what this has to do with notepad.
john1990_1 20-Jan-18 10:24am    
I need a code that sends Ctrl+C to the active window of Windows to put selected text in clipboard.

1 solution

p is null because Process.GetProcessesByName expects the "friendly name" of a process, not the full file path that appears to be returned by GetActiveProcessFileName.

But here's some good news: you don't even need to use GetProcessesByName to get the process you want: in GetActiveProcessFileName, you have a line Process p = Process.GetProcessById((int)pid) and this p is exactly the process you wish, so you don't need to return this process's file name to retrieve the Process again later: just return p already. Here is a GetActiveProcess method that returns the desired process:
C#
static Process GetActiveProcess()
{
    IntPtr hwnd = GetForegroundWindow();
    uint pid;
    GetWindowThreadProcessId(hwnd, out pid);
    return Process.GetProcessById((int)pid);
}
And then you can replace the Process p = Process.GetProcessesByName ... line with this:
C#
Process p = GetActiveProcess();


[Edit]

It appears that "^C" doesn't always make the active application copy the selected text, but I've had better results with "^c" (that is, a lowercase c).
 
Share this answer
 
v2
Comments
john1990_1 20-Jan-18 10:39am    
Thx, now how to send Ctrl+C to the active application you found?
Thomas Daniels 20-Jan-18 10:50am    
Just like you did it. The only difference is that you use GetActiveProcess instead of Process.GetProcessesByName.
john1990_1 20-Jan-18 10:50am    
but it doesnt work.
Thomas Daniels 20-Jan-18 10:51am    
Explain "doesn't work". Does it give an error? Is it the wrong process? Or does it just not copy the text? When I tried it, it worked fine.
john1990_1 20-Jan-18 10:57am    
When I run this on F7 keyup, it copies the text only if Notepad is the active process:
Process p = GetActiveProcess();
if (p != null)
{
IntPtr h = p.MainWindowHandle;
SetForegroundWindow(h);
SendKeys.SendWait("^C");
}

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