Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is what I'm trying to do,

When user select any text of any running application by left mouse button and CRTL key, the selected text should be copied into Clipboard.

So far I have implemented the logic using Global Keystroke to trigger CRT+ C keyboard key combination to copy the selected word into clipboard. This code works properly for Google Chrome but I'm facing with a problem with notepad.

What I have tried:

C#
private static void OnMouseLeftButtonUp(object sender, MouseEventArgs e)
    {
        if ((ModifierKeys & Keys.Control) == Keys.Control)
        {
            Thread.Sleep(1500);
            IntPtr hWnd = GetWindowUnderCursor();
            SetForegroundWindow(hWnd);
            Thread.Sleep(500);

            //Ctrl + C
            new InputSimulator().Keyboard. 
                .ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);

            Thread.Sleep(500);
            MessageBox.Show(Clipboard.GetText());
        }
    }


What's going on here?
Posted
Updated 6-Aug-19 1:32am
v5
Comments
Dave Kreskowiak 1-Aug-19 18:19pm    
Ummm.... I see a major design flaw. When the MouseButtonUp event fires, you call SendCTRL_C, in which the FIRST THING it does is get the window under the mouse cursor, which is going to be YOUR APP, not the sending app.
Ayub Kokabi 2-Aug-19 3:10am    
I've improved code. please note, my application is minimized when started. Before sending Ctrl + C I get handle of windows which cursor is on it. (GetWinowUnderCursor()). this windows may be notepad or chrome or edge, etc. Then setforgroundwindow() set focus on previous hWnd (handle of window).
Dave Kreskowiak 2-Aug-19 9:12am    
Assuming the user is releasing the mouse button while it's on your window, what is the first thing that's going to happen?

AGAIN, you get the window under the mouse cursor, which will be YOUR WINDOW!!

THINK!
Ayub Kokabi 2-Aug-19 9:37am    
I intentionally make the code simpler. I'll handle another problems later. I want you to focus on the main problem.

Follow these steps:
1- Run the app,
2- Make it minimize,
3- Open Google Chrome,
4- Hold Ctrl key,
5- Drag some text in a web page,
6- Now, release drag (LeftMouseButtonUp),
Result: It shows a MessageBox contains your selected text properly.
Again, run these steps in notepad, But now It doesn't show correct selected text.

This is my main problem.
please focus on it.
Dave Kreskowiak 2-Aug-19 10:45am    
OK, now I see what's going on. You're using the wrong terms. "Dragging" normally refers to moving an object. In the case of "drag and drop", you would be dragging a SELECTED object over another window and dropping it on that window.

You should be replacing the word "dragging" with "selecting".

So, that's where the confusion is.

So you want to tell the target window to copy the selected text to the clipboard.

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