EscribePassword/KeyboardHelper.cs

134 lines
4.1 KiB
C#
Raw Normal View History

2024-06-15 14:33:52 -03:00
using System.Runtime.InteropServices;
using WindowsInput.Native;
using WindowsInput;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
namespace EscribePassword
{
public partial class Passwords : ObservableObject
{
[ObservableProperty]
private string usuario;
[ObservableProperty]
private string password;
[ObservableProperty]
private string categoria;
2024-06-16 12:39:45 -03:00
public static List<Passwords> ConvertArrayToPasswordsList(string[,] tableArray)
{
var passwordsList = new List<Passwords>();
for (int i = 0; i < tableArray.GetLength(0); i++)
{
var passwords = new Passwords();
if (tableArray.GetLength(1) >= 1)
passwords.Categoria = tableArray[i, 0];
if (tableArray.GetLength(1) >= 2)
passwords.Usuario = tableArray[i, 1];
if (tableArray.GetLength(1) >= 3)
passwords.Password = tableArray[i, 2];
passwordsList.Add(passwords);
}
return passwordsList;
}
public static string[,] ConvertPasswordsListToArray(List<Passwords> passwordsList)
{
if (passwordsList == null || passwordsList.Count == 0)
return new string[0, 0];
int rows = passwordsList.Count;
int columns = 3; // Asumimos que siempre hay tres columnas: Usuario, Password, Categoria
string[,] tableArray = new string[rows + 1, columns];
// Fill header
tableArray[0, 0] = "Class";
tableArray[0, 1] = "User";
tableArray[0, 2] = "Password";
// Fill data
for (int i = 0; i < rows; i++)
{
tableArray[i + 1, 0] = passwordsList[i].Usuario;
tableArray[i + 1, 1] = passwordsList[i].Password;
tableArray[i + 1, 2] = passwordsList[i].Categoria;
}
return tableArray;
}
2024-06-15 14:33:52 -03:00
}
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);
}
}
}
}