In C #, is there a way to sequentially retrieve the selected text content of the currently focused window?

In my C # .Net application, I am trying to get the selected text in the current window. (Note that this can be any window open in windows, such as a word or safari).

I can get a handle to the current focused control. (Using multiple interop calls for user32.dll and kernel32.dll).

However, I was not able to consistently return the selected text.

I tried to use SENDMESSAGE and GET_TEXT. However, this only works for some applications (works for simple applications such as wordpad, does not work for more complex applications such as firefox or word).

I tried to use SENDMESSAGE and WM_COPY. However, again, this only looks like some controls. (I would have thought that WM_COPY would lead to the same behavior as manually pressing CTRL-C, but that is not the case).

I tried using SENDMESSAGE and WM_KEYUP + WM_KEYDOWN to manually stimulate the copy command. BUt this does not work either. (Possibly overlapping with the actual hotkey pressed by the user to call my applications).

Any ideas on how to get the current text? (in any application).

+3
source share
7 answers

It turned out that this work is combined with several things. So:

  • Wait until any modifiers are saved at this time.
  • + c ( Trigger OS (ctrl + c Ctrl-x) )

            bool stillHeld = true;
            int timeSlept = 0;
    
            do
            {
                // wait until our hotkey is released
                if ((Keyboard.Modifiers & ModifierKeys.Control) > 0 ||
                    (Keyboard.Modifiers & ModifierKeys.Alt) > 0 ||
                    (Keyboard.Modifiers & ModifierKeys.Shift) > 0)
                {
                    timeSlept += 50;
                    System.Threading.Thread.Sleep(timeSlept);
                }
                else
                {
                    stillHeld = false;
                }
            } while (stillHeld && timeSlept < 1000);
    
            Keyboard.SimulateKeyStroke('c', ctrl: true);
    

WPF, Keyboard.Modifiers - System.Windows.Input.Keyboard, Keyboard.SimulateKeyStroke - .

. timeSlept - , , , .

+4

/ , .

. Reflector, , TextBoxBase.SelectedText.

public static string SelectedText
{
    get
    {
        AutomationElement focusedElement = AutomationElement.FocusedElement;

        object currentPattern = null;

        if (focusedElement.TryGetCurrentPattern(TextPattern.Pattern, out currentPattern))
        {
            TextPattern textPattern = (TextPattern)currentPattern;
            TextPatternRange[] textPatternRanges = textPattern.GetSelection();
            if (textPatternRanges.Length > 0)
            {
                string textSelection = textPatternRanges[0].GetText(-1);
                return textSelection;
            }
        }
        return string.Empty;
    }
    set
    {
        AutomationElement focusedElement = AutomationElement.FocusedElement;
        IntPtr windowHandle = new IntPtr(focusedElement.Current.NativeWindowHandle);
        NativeMethods.SendMessage(windowHandle, NativeMethods.EM_REPLACESEL, true, value);
    }
}
+2

, , - . (, ). .

, , , , .

+1

, , Ctrl + c? , , , ?

SendKeys.SendWait("^c");

, ( ).

+1

, . Klipper ( KDE), , , - , - .

.

0

... , ? ?

SendKeys.SendWait( "^ c" ); :

http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=46203

, . - .

- - ?

0

Try the GetWindowText () API for controls for which other methods do not work.

0
source

Source: https://habr.com/ru/post/1704873/


All Articles