public void TextKeyWizardFromHotKey() { // Determine the currently active window fgWin = WinApi.GetForegroundWindow(); // Require it to be Visual Studio, otherwise do nothing more if (App.Settings.Wizard.HotkeyInVisualStudioOnly) { StringBuilder sb = new StringBuilder(1000); WinApi.GetWindowText(fgWin, sb, 1000); if (!sb.ToString().EndsWith(" - Microsoft Visual Studio")) return; } // Backup current clipboard content clipboardBackup = ClipboardHelper.GetDataObject(); // Send Ctrl+C keys to the active window to copy the selected text // (First send events to release the still-pressed hot key buttons Ctrl and Shift) WinApi.INPUT[] inputs = new WinApi.INPUT[] { new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.VK.CONTROL, dwFlags = WinApi.KEYEVENTF_KEYUP } }, new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.VK.SHIFT, dwFlags = WinApi.KEYEVENTF_KEYUP } }, new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.VK.CONTROL } }, new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.KeyToVk(System.Windows.Forms.Keys.C) } }, new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.KeyToVk(System.Windows.Forms.Keys.C), dwFlags = WinApi.KEYEVENTF_KEYUP } }, new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.VK.CONTROL, dwFlags = WinApi.KEYEVENTF_KEYUP } }, }; uint ret = WinApi.SendInput((uint) inputs.Length, inputs, System.Runtime.InteropServices.Marshal.SizeOf(typeof(WinApi.INPUT))); //System.Diagnostics.Debug.WriteLine(ret + " inputs sent"); DelayedCall.Start(TextKeyWizardFromHotKey2, 50); }
private void TextKeyWizardFromHotKey2() { // Create the wizard window TextKeyWizardWindow win = new TextKeyWizardWindow(); //win.Owner = MainWindow.Instance; win.ShowInTaskbar = true; win.ClipboardBackup = clipboardBackup; MainWindow.Instance.Hide(); bool ok = false; if (win.ShowDialog() == true) { ok = HandleWizardInput(win.TextKeyText.Text, win.TranslationText.Text); } MainWindow.Instance.Show(); // Activate the window we're initially coming from WinApi.SetForegroundWindow(fgWin); if (ok) { // Send Ctrl+V keys to paste the new Tx call with the text key, // directly replacing the literal string that was selected before WinApi.INPUT[] inputs = new WinApi.INPUT[] { new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.VK.CONTROL } }, new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.KeyToVk(System.Windows.Forms.Keys.V) } }, new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.KeyToVk(System.Windows.Forms.Keys.V), dwFlags = WinApi.KEYEVENTF_KEYUP } }, new WinApi.INPUT() { type = WinApi.INPUT_KEYBOARD, ki = new WinApi.KEYBDINPUT() { wVk = (short) WinApi.VK.CONTROL, dwFlags = WinApi.KEYEVENTF_KEYUP } }, }; uint ret = WinApi.SendInput((uint) inputs.Length, inputs, System.Runtime.InteropServices.Marshal.SizeOf(typeof(WinApi.INPUT))); } clipboardBackup = win.ClipboardBackup; if (clipboardBackup != null) { DelayedCall.Start(TextKeyWizardFromHotKey3, 200); } }