I've already realized application that can getting selected text by emulating ctr+c keypress.
So, now I have 3 working methods:
// Using InputSimulator library
private static void CtrlC_inputSimulator()
{
var hWnd = GetWindowUnderCursor();
SetActiveWindow(hWnd);
InputSimulator i = new InputSimulator();
i.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
}
// Using SendKey
private static void CtrlC_SendKey()
{
try
{
SendKeys.Send("^(c)");
}
catch(Exception ex) { }
}
// Using native user32 keybd_event directly
private void CtrlC_KeybdEvent_User32()
{
uint KEYBD_EVENT_KEYUP = 2;
var hWnd = GetWindowUnderCursor();
SetForegroundWindow(hWnd);
// Ctrl-C Down
keybd_event(KEY_CONTROL, 0, 0, 0);
keybd_event(KEY_C, 0, 0, 0);
// Ctrl-C Up
keybd_event(KEY_C, 0, KEYBD_EVENT_KEYUP, 0);
keybd_event(KEY_CONTROL, 0, KEYBD_EVENT_KEYUP, 0);
}
But in practice it's not stable way to use keypress simulation.
And I thought about how to get the selected text using system messages such as WM_GETTEXT, but as far as I know, most of these things are only working with rich text box, and will not work in programs such as Chrome or AcrobatReader.
Tell me please, is there a function or some librery than can do it?