It is necessary to transfer the selected text from any application by key combination. How did I assume it would look like? I press alt + shift + j. Selected text is buffered. Then the program should get the text out. Question: how to put the text in the buffer? I tried to do this through WM_COPY and through the simulation of pressing Ctrl-C, so that the text got into the clipboard, but all in vain. The program draws what was previously in the buffer. With WinAPI almost not familiar, so I will be glad if you write a detailed solution.
private IntPtr _windowHandle; private HwndSource _source; protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); _windowHandle = new WindowInteropHelper(this).Handle; _source = HwndSource.FromHwnd(_windowHandle); _source.AddHook(HwndHook); RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_ALT + MOD_SHIFT, VK_J); //ALT + SHIFT + J } private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { const int WM_HOTKEY = 0x0312; switch (msg) { case WM_HOTKEY: switch (wParam.ToInt32()) { case HOTKEY_ID: int vkey = (((int)lParam >> 16) & 0xFFFF); if (vkey == VK_J) { string word = GetSelectedText(); MessageBox.Show(word); } handled = true; break; } break; } return IntPtr.Zero; } protected override void OnClosed(EventArgs e) { _source.RemoveHook(HwndHook); UnregisterHotKey(_windowHandle, HOTKEY_ID); base.OnClosed(e); } private string GetSelectedText() { Type whatDoesContainClipboard = null; object clipboardData = null; if (Clipboard.ContainsAudio()) { whatDoesContainClipboard = typeof(Stream); clipboardData = Clipboard.GetAudioStream(); } else if (Clipboard.ContainsImage()) { whatDoesContainClipboard = typeof(BitmapSource); clipboardData = Clipboard.GetImage(); } else if (Clipboard.ContainsText()) { whatDoesContainClipboard = typeof(string); clipboardData = Clipboard.GetText(); } int handle = GetForegroundWindow(); int ProcessID; int SelectedThreadId = GetWindowThreadProcessId(handle, out ProcessID); int CurrentThreadId = GetCurrentThreadId(); Process SelectedProcess = Process.GetProcessById(ProcessID); AttachThreadInput(SelectedThreadId, CurrentThreadId, true); IntPtr FocusedWindowEx = GetFocus(); SendMessage(FocusedWindowEx, WM_COPY, IntPtr.Zero, IntPtr.Zero); IDataObject data = Clipboard.GetDataObject(); string[] formats = data.GetFormats(); var selectedText = ""; if (formats.Contains<string>("System.String")) selectedText = (string)data.GetData("System.String"); AttachThreadInput(SelectedThreadId, CurrentThreadId, false); if (whatDoesContainClipboard != null) { if (whatDoesContainClipboard == typeof(Stream)) Clipboard.SetAudio((Stream)clipboardData); else if (whatDoesContainClipboard == typeof(BitmapSource)) Clipboard.SetImage((BitmapSource)clipboardData); else if (whatDoesContainClipboard == typeof(string)) Clipboard.SetText((string)clipboardData); } else Clipboard.Clear(); return selectedText; } [DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); [DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); [DllImport("user32.dll")] private static extern int GetForegroundWindow(); [DllImport("user32.dll")] private static extern int GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId); [DllImport("User32.dll")] private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll")] private static extern int GetCurrentThreadId(); [DllImport("user32.dll")] private static extern int AttachThreadInput(int idAttach, int idAttachTo, bool fAttach); [DllImport("user32.dll")] private static extern IntPtr GetFocus(); const int WM_COPY = 0x0301; private const int HOTKEY_ID = 9000; //Modifiers: private const uint MOD_NONE = 0x0000; //(none) private const uint MOD_ALT = 0x0001; //ALT private const uint MOD_CONTROL = 0x0002; //CTRL private const uint MOD_SHIFT = 0x0004; //SHIFT private const uint MOD_WIN = 0x0008; //WINDOWS private const uint VK_J = 0x4A;
txtBox.Text = Clipboard.GetText();- eastwing