diff --git a/App.xaml.cs b/App.xaml.cs index 34f6ad2..a9dca2e 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -25,7 +25,7 @@ namespace GTPCorrgir KeyboardHelper pasteLogic = new KeyboardHelper(); - protected override void OnStartup(StartupEventArgs e) + protected override async void OnStartup(StartupEventArgs e) { base.OnStartup(e); _cancellationTokenSource = new CancellationTokenSource(); @@ -34,11 +34,22 @@ namespace GTPCorrgir { pasteLogic.SaveCurrentWindow(); - if (System.Windows.Clipboard.ContainsText()) + if (Opciones.Instance.AutoCopy) { - GTP.TextoACorregir = System.Windows.Clipboard.GetText(); + await pasteLogic.AutoCopyFromActiveWindow(); } + if (System.Windows.Clipboard.ContainsText()) + { + GTP.TextoACorregir = await ClipboardHelper.GetText(); + } + if (string.IsNullOrEmpty(GTP.TextoACorregir)) + { + GTP.Log.Log("No hay texto en el portapapeles"); + ShowCustomNotification("Error", "No hay texto para procesar"); + Application.Current.Shutdown(); + return; + } if (Opciones.Instance.modo == Opciones.modoDeUso.Corregir || Opciones.Instance.modo == Opciones.modoDeUso.Ortografia) { @@ -102,7 +113,7 @@ namespace GTPCorrgir if (GTP.TextoCorregido != null) { GTP.Log.Log("Copiando texto corregido al portapapeles"); - System.Windows.Clipboard.SetText(GTP.TextoCorregido); + await ClipboardHelper.SetText(GTP.TextoCorregido); ShowCustomNotification("Se puede pegar", $"Corrección en: {Math.Round(stopwatch.ElapsedMilliseconds / 1000.0, 1)} s"); @@ -116,7 +127,7 @@ namespace GTPCorrgir else if (Opciones.Instance.modo == Opciones.modoDeUso.Ortografia) { GTP.Log.Log("Ejecutando pegado automático"); - pasteLogic.RestoreAndSimulatePaste(); + await pasteLogic.RestoreAndSimulatePaste(); } } else @@ -213,38 +224,4 @@ 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 - } - } } \ No newline at end of file diff --git a/ClipboardHelper.cs b/ClipboardHelper.cs new file mode 100644 index 0000000..551fa67 --- /dev/null +++ b/ClipboardHelper.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GTPCorrgir +{ + public static class ClipboardHelper + { + private static readonly int MaxRetries = 5; + private static readonly int RetryDelay = 100; // milliseconds + + public static async Task SetText(string text) + { + for (int i = 0; i < MaxRetries; i++) + { + try + { + await Task.Delay(RetryDelay); + await System.Windows.Application.Current.Dispatcher.InvokeAsync(() => + { + System.Windows.Clipboard.SetText(text); + }); + return true; + } + catch (Exception) + { + if (i == MaxRetries - 1) throw; + } + } + return false; + } + + public static async Task GetText() + { + string text = null; + for (int i = 0; i < MaxRetries; i++) + { + try + { + await Task.Delay(RetryDelay); + text = await System.Windows.Application.Current.Dispatcher.InvokeAsync(() => + { + return System.Windows.Clipboard.GetText(); + }); + return text; + } + catch (Exception) + { + if (i == MaxRetries - 1) throw; + } + } + return text; + } + } +} diff --git a/KeyboardHelper.cs b/KeyboardHelper.cs new file mode 100644 index 0000000..1558221 --- /dev/null +++ b/KeyboardHelper.cs @@ -0,0 +1,89 @@ +using System; +using System.Runtime.InteropServices; +using System.Threading.Tasks; + +namespace GTPCorrgir +{ + public class KeyboardHelper + { + [DllImport("user32.dll")] + private static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + private static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); + + private const int KEYEVENTF_EXTENDEDKEY = 0x0001; + private const int KEYEVENTF_KEYUP = 0x0002; + private const int VK_CONTROL = 0x11; + private const int VK_A = 0x41; + private const int VK_C = 0x43; + private const int VK_V = 0x56; + + private IntPtr previousWindow; + + public void SaveCurrentWindow() + { + previousWindow = GetForegroundWindow(); + } + + public async Task AutoCopyFromActiveWindow() + { + try + { + // Asegurarse de que la ventana anterior tenga el foco + SetForegroundWindow(previousWindow); + + // Pequeña pausa para asegurar que la ventana tiene el foco + await Task.Delay(100); + + // Simular Ctrl+A + keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero); + keybd_event(VK_A, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero); + keybd_event(VK_A, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero); + + // Pequeña pausa entre comandos + await Task.Delay(50); + + // Simular Ctrl+C + keybd_event(VK_C, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero); + keybd_event(VK_C, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero); + keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero); + + // Dar tiempo para que se complete la operación de copiado + await Task.Delay(100); + } + catch (Exception ex) + { + Console.WriteLine($"Error en AutoCopyFromActiveWindow: {ex.Message}"); + } + } + + public async Task RestoreAndSimulatePaste() + { + try + { + // Asegurarse de que la ventana anterior tenga el foco + SetForegroundWindow(previousWindow); + + // Pequeña pausa para asegurar que la ventana tiene el foco + await Task.Delay(100); + + // Simular Ctrl+V + keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero); + keybd_event(VK_V, 0, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero); + keybd_event(VK_V, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero); + keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero); + + // Dar tiempo para que se complete la operación de pegado + await Task.Delay(100); + } + catch (Exception ex) + { + Console.WriteLine($"Error en RestoreAndSimulatePaste: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index b853f89..bc41913 100644 --- a/Program.cs +++ b/Program.cs @@ -14,8 +14,9 @@ namespace GTPCorrgir OpenAI, Ollama, Groq, - Grok // x.ai + Grok } + public enum modoDeUso { Corregir, @@ -27,16 +28,16 @@ namespace GTPCorrgir } public Dictionary nombreLLM = new Dictionary - { - { Opciones.LLM_a_Usar.Ollama, "Ollama" }, - { Opciones.LLM_a_Usar.Groq, "Groq" }, - { Opciones.LLM_a_Usar.Grok, "Grok" }, - { Opciones.LLM_a_Usar.OpenAI, "OpenAI" }, - - }; + { + { LLM_a_Usar.Ollama, "Ollama" }, + { LLM_a_Usar.Groq, "Groq" }, + { LLM_a_Usar.Grok, "Grok" }, + { LLM_a_Usar.OpenAI, "OpenAI" }, + }; private static Opciones _instance; - + public bool AutoCopy { get; set; } + public static Opciones Instance { get @@ -46,6 +47,7 @@ namespace GTPCorrgir _instance = new Opciones(); _instance.LLM = LLM_a_Usar.Grok; _instance.modo = modoDeUso.Chat; + _instance.AutoCopy = false; } return _instance; } @@ -54,7 +56,8 @@ namespace GTPCorrgir public LLM_a_Usar LLM { get; set; } public modoDeUso modo { get; set; } - public string nombreDeLLM() { + public string nombreDeLLM() + { return nombreLLM[LLM]; } @@ -95,7 +98,8 @@ namespace GTPCorrgir Opciones.Instance.modo = Opciones.modoDeUso.Traducir_a_Italiano; else if (arg.Contains("Traducir_a_Espanol")) Opciones.Instance.modo = Opciones.modoDeUso.Traducir_a_Espanol; - + else if (arg.Contains("AutoCopy")) + Opciones.Instance.AutoCopy = true; } }