Ctrl+V para la correccion ortografica

This commit is contained in:
Miguel 2024-05-28 13:43:16 +02:00
parent 3e2494b5a8
commit 5553729f43
1 changed files with 45 additions and 2 deletions

View File

@ -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
}
}
}