diff --git a/App.xaml.cs b/App.xaml.cs
index 16c17c8..1d4c64b 100644
--- a/App.xaml.cs
+++ b/App.xaml.cs
@@ -1,6 +1,7 @@
using System.Configuration;
using System.Data;
using System.Windows;
+using Application = System.Windows.Application;
namespace EscribePassword
{
@@ -9,6 +10,49 @@ namespace EscribePassword
///
public partial class App : Application
{
+ public KeyboardHelper PasteLogic { get; } = new KeyboardHelper();
+
+ protected override void OnStartup(StartupEventArgs e)
+ {
+ base.OnStartup(e);
+ PasteLogic.SaveCurrentWindow();
+ SetInitialWindowPosition();
+ }
+
+ public void SetInitialWindowPosition()
+ {
+ var screenRect = PasteLogic.GetScreenOfPreviousWindow();
+ var mainWindow = Application.Current.MainWindow;
+
+ if (mainWindow != null)
+ {
+
+ // Obtener la posición del cursor
+ var cursorPosition = System.Windows.Forms.Cursor.Position;
+
+ // Determinar en qué pantalla está el cursor
+ var screen = Screen.FromPoint(cursorPosition);
+
+ // Determinar la posición inicial
+ double newLeft = screenRect.Right;
+ double newTop = screenRect.Top;
+
+ // Verificar si la ventana se posiciona fuera de la pantalla y ajustarla si es necesario
+ if (newLeft < screenRect.Left)
+ {
+ newLeft = screenRect.Left;
+ }
+
+ if (newLeft + mainWindow.Width > screen.WorkingArea.Right)
+ {
+ newLeft = screen.WorkingArea.Right - mainWindow.Width;
+ }
+
+ // Aplicar la nueva posición
+ mainWindow.Left = newLeft;
+ mainWindow.Top = newTop;
+ }
+ }
}
}
diff --git a/EscribePassword.csproj b/EscribePassword.csproj
index 0d0a51b..8460c40 100644
--- a/EscribePassword.csproj
+++ b/EscribePassword.csproj
@@ -6,11 +6,13 @@
enable
enable
true
+ True
+
diff --git a/KeyboardHelper.cs b/KeyboardHelper.cs
new file mode 100644
index 0000000..59581aa
--- /dev/null
+++ b/KeyboardHelper.cs
@@ -0,0 +1,85 @@
+using System.Runtime.InteropServices;
+using WindowsInput.Native;
+using WindowsInput;
+using System.Windows;
+using EscribePassword;
+using CommunityToolkit.Mvvm.ComponentModel;
+
+namespace EscribePassword
+{
+
+ public partial class Passwords : ObservableObject
+ {
+ [ObservableProperty]
+ private string usuario;
+
+ [ObservableProperty]
+ private string password;
+ }
+
+ 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 bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
+
+ [StructLayout(LayoutKind.Sequential)]
+ private struct RECT
+ {
+ public int Left;
+ public int Top;
+ public int Right;
+ public int Bottom;
+ }
+
+ private IntPtr previousWindow;
+
+ public void SaveCurrentWindow()
+ {
+ previousWindow = GetForegroundWindow();
+ }
+
+ public Rect GetScreenOfPreviousWindow()
+ {
+ if (previousWindow != IntPtr.Zero)
+ {
+ var rect = new RECT();
+ if (GetWindowRect(previousWindow, ref rect))
+ {
+ return new Rect(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
+ }
+ }
+ return SystemParameters.WorkArea;
+ }
+
+ public void RestoreAndSimulatePaste(Passwords password)
+ {
+ if (previousWindow != IntPtr.Zero)
+ {
+ // Restore focus to the previous window
+ SetForegroundWindow(previousWindow);
+
+ var sim = new InputSimulator();
+
+ if (password.Usuario != null && password.Usuario.Length > 0)
+ {
+ // Simulate typing the username
+ sim.Keyboard.TextEntry(password.Usuario);
+ sim.Keyboard.KeyPress(VirtualKeyCode.RETURN);
+ sim.Keyboard.KeyPress(VirtualKeyCode.TAB);
+ }
+ // Simulate typing the password
+ sim.Keyboard.TextEntry(password.Password);
+ sim.Keyboard.KeyPress(VirtualKeyCode.RETURN);
+ sim.Keyboard.KeyPress(VirtualKeyCode.TAB);
+ }
+ }
+ }
+}
+
+
diff --git a/MainWindow.xaml b/MainWindow.xaml
index 3f30836..88c7933 100644
--- a/MainWindow.xaml
+++ b/MainWindow.xaml
@@ -2,38 +2,40 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:EscribePassword" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">
+ xmlns:local="clr-namespace:EscribePassword" mc:Ignorable="d" Title="MainWindow" Height="450" Width="300"
+ AllowsTransparency="True" WindowStyle="None" MouseDown="Window_MouseDown">
-
-
-
-
-
+
-
+
+
+
+
-
+
+
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index 4e3c6a2..4c9b70c 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -5,9 +5,43 @@ using System.Collections.ObjectModel;
using System.Text.Json;
using System.IO;
using System.Windows;
+using WindowsInput;
+using WindowsInput.Native;
+using Application = System.Windows.Application;
+using System.Windows.Input;
+using MouseButton = System.Windows.Input.MouseButton;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+using ListBox = System.Windows.Controls.ListBox;
namespace EscribePassword
{
+
+
+ ///
+ /// Interaction logic for MainWindow.xaml
+ ///
+ public partial class MainWindow : Window
+ {
+ public MainWindow()
+ {
+ InitializeComponent();
+ this.Loaded += MainWindow_Loaded;
+ }
+ private void MainWindow_Loaded(object sender, RoutedEventArgs e)
+ {
+ ((App)Application.Current).SetInitialWindowPosition();
+ }
+
+ private void Window_MouseDown(object sender, MouseButtonEventArgs e)
+ {
+ if (e.ChangedButton == MouseButton.Left)
+ {
+ this.DragMove();
+ }
+ }
+ }
public partial class MView : ObservableObject
{
[ObservableProperty]
@@ -16,18 +50,12 @@ namespace EscribePassword
[ObservableProperty]
private Passwords selectedPassword;
- public RelayCommand AgregarCommand { get; }
- public RelayCommand EliminarCommand { get; }
-
public MView()
{
// Suscribirse al evento de cierre de la aplicación
Application.Current.Exit += OnApplicationExit;
passwords = new ObservableCollection(EstadoPersistente.Instance.Passwords);
-
- AgregarCommand = new RelayCommand(Agregar);
- EliminarCommand = new RelayCommand(Eliminar);
}
private void OnApplicationExit(object sender, ExitEventArgs e)
@@ -36,6 +64,7 @@ namespace EscribePassword
EstadoPersistente.Instance.GuardarEstado();
}
+ [RelayCommand]
private void Agregar()
{
Passwords newPassword = new Passwords { Usuario = "NuevoUsuario", Password = "NuevaContraseña" };
@@ -43,6 +72,7 @@ namespace EscribePassword
SelectedPassword = newPassword;
}
+ [RelayCommand]
private void Eliminar(Passwords password)
{
if (password != null)
@@ -51,9 +81,29 @@ namespace EscribePassword
SelectedPassword = null;
}
}
+
+ [RelayCommand]
+ private void Utilizar(Passwords password)
+ {
+ if (password != null)
+ {
+ ((App)Application.Current).PasteLogic.RestoreAndSimulatePaste(password);
+
+ // Cerrar la aplicación después de la acción
+ Application.Current.Shutdown();
+ }
+ }
+
+ [RelayCommand]
+ private void Cerrar()
+ {
+ // Cerrar la aplicación después de la acción
+ Application.Current.Shutdown();
+ }
+
}
- public class EstadoPersistente
+ internal class EstadoPersistente
{
// Ruta donde se guardará el estado
private static readonly string _filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "estado.json");
@@ -124,13 +174,4 @@ namespace EscribePassword
}
}
}
-
- public partial class Passwords : ObservableObject
- {
- [ObservableProperty]
- private string usuario;
-
- [ObservableProperty]
- private string password;
- }
}