From 5553729f4333a5f0f98dd82d66989da944381412 Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 28 May 2024 13:43:16 +0200 Subject: [PATCH] Ctrl+V para la correccion ortografica --- App.xaml.cs | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index 5ceb84b..3d32d54 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -9,6 +9,7 @@ using System.Diagnostics; // Asegúrate de incluir esta directiva para usar Stop using System.Threading.Tasks; using System.Globalization; using System.Windows.Data; +using System.Runtime.InteropServices; namespace GTPCorrgir { @@ -26,11 +27,14 @@ namespace GTPCorrgir Stopwatch stopwatch = new Stopwatch(); private notificacion notificationWindow; + KeyboardHelper pasteLogic = new KeyboardHelper(); protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); + pasteLogic.SaveCurrentWindow(); + if (System.Windows.Clipboard.ContainsText()) { GTP.TextoACorregir = System.Windows.Clipboard.GetText(); @@ -59,7 +63,7 @@ namespace GTPCorrgir } finally { - Dispatcher.Invoke(async () => // Nota el 'async' aquí para permitir 'await' + await Dispatcher.Invoke(async () => // Nota el 'async' aquí para permitir 'await' { CorreccionFinalizada = true; DetenerCronometro(); @@ -72,9 +76,11 @@ namespace GTPCorrgir { var resultadoWindow = new VentanaResultado(GTP.TextoCorregido); resultadoWindow.Show(); + await Task.Delay(1000); // Asíncrono espera 5 segundos } + else if (Opciones.Instance.modo == Opciones.modoDeUso.Ortografia) + pasteLogic.RestoreAndSimulatePaste(); - await Task.Delay(5000); // Asíncrono espera 5 segundos Application.Current.Shutdown(); } else @@ -142,4 +148,41 @@ namespace GTPCorrgir } + public class KeyboardHelper + { + [DllImport("user32.dll", SetLastError = true)] + private static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); + + public const int KEYEVENTF_KEYUP = 0x0002; // Key up flag + public const int VK_CONTROL = 0x11; // Control key code + public const int V = 0x56; // V key code + + private IntPtr previousWindow; + + public void SaveCurrentWindow() + { + previousWindow = GetForegroundWindow(); + } + + public void RestoreAndSimulatePaste() + { + // Restore focus to the previous window + SetForegroundWindow(previousWindow); + + // Simulating Ctrl+V + keybd_event((byte)VK_CONTROL, 0x9d, 0, UIntPtr.Zero); // Ctrl Press + keybd_event((byte)V, 0x9e, 0, UIntPtr.Zero); // V Press + keybd_event((byte)V, 0x9e, KEYEVENTF_KEYUP, UIntPtr.Zero); // V Release + keybd_event((byte)VK_CONTROL, 0x9d, KEYEVENTF_KEYUP, UIntPtr.Zero); // Ctrl Release + } + } + + + }