Uso de la rueda del mouse para seleccionar la password

This commit is contained in:
Miguel 2024-06-16 00:27:11 +02:00
parent 6a619872f4
commit 13f78d7723
5 changed files with 161 additions and 47 deletions

View File

@ -1,19 +1,30 @@
using System.Configuration; using System;
using System.Data; using System.Threading;
using System.Windows; using System.Windows;
using Application = System.Windows.Application; using Application = System.Windows.Application;
namespace EscribePassword namespace EscribePassword
{ {
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application public partial class App : Application
{ {
private static Mutex _mutex = null;
public KeyboardHelper PasteLogic { get; } = new KeyboardHelper(); public KeyboardHelper PasteLogic { get; } = new KeyboardHelper();
protected override void OnStartup(StartupEventArgs e) protected override void OnStartup(StartupEventArgs e)
{ {
const string appName = "EscribePasswordApp";
bool createdNew;
_mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
// Hay otra instancia en ejecución, cerramos esta instancia
Application.Current.Shutdown();
return;
}
base.OnStartup(e); base.OnStartup(e);
PasteLogic.SaveCurrentWindow(); PasteLogic.SaveCurrentWindow();
SetInitialWindowPosition(); SetInitialWindowPosition();
@ -26,18 +37,12 @@ namespace EscribePassword
if (mainWindow != null) if (mainWindow != null)
{ {
// Obtener la posición del cursor
var cursorPosition = System.Windows.Forms.Cursor.Position; var cursorPosition = System.Windows.Forms.Cursor.Position;
// Determinar en qué pantalla está el cursor
var screen = Screen.FromPoint(cursorPosition); var screen = Screen.FromPoint(cursorPosition);
// Determinar la posición inicial
double newLeft = screenRect.Right; double newLeft = screenRect.Right;
double newTop = screenRect.Top; double newTop = screenRect.Top;
// Verificar si la ventana se posiciona fuera de la pantalla y ajustarla si es necesario
if (newLeft < screenRect.Left) if (newLeft < screenRect.Left)
{ {
newLeft = screenRect.Left; newLeft = screenRect.Left;
@ -48,11 +53,9 @@ namespace EscribePassword
newLeft = screen.WorkingArea.Right - mainWindow.Width; newLeft = screen.WorkingArea.Right - mainWindow.Width;
} }
// Aplicar la nueva posición
mainWindow.Left = newLeft; mainWindow.Left = newLeft;
mainWindow.Top = newTop; mainWindow.Top = newTop;
} }
} }
} }
} }

View File

@ -13,6 +13,7 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.0" /> <PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.0" />
<PackageReference Include="InputSimulator" Version="1.0.4" /> <PackageReference Include="InputSimulator" Version="1.0.4" />
<PackageReference Include="MouseKeyHook" Version="5.7.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>

View File

@ -2,7 +2,6 @@
using WindowsInput.Native; using WindowsInput.Native;
using WindowsInput; using WindowsInput;
using System.Windows; using System.Windows;
using EscribePassword;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
namespace EscribePassword namespace EscribePassword
@ -15,6 +14,10 @@ namespace EscribePassword
[ObservableProperty] [ObservableProperty]
private string password; private string password;
[ObservableProperty]
private string categoria;
} }
public class KeyboardHelper public class KeyboardHelper

View File

@ -3,7 +3,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:EscribePassword" mc:Ignorable="d" Title="MainWindow" Height="450" Width="300" xmlns:local="clr-namespace:EscribePassword" mc:Ignorable="d" Title="MainWindow" Height="450" Width="300"
AllowsTransparency="True" WindowStyle="None" MouseDown="Window_MouseDown"> AllowsTransparency="True" WindowStyle="None" MouseDown="Window_MouseDown" Topmost="True">
<Window.DataContext> <Window.DataContext>
<local:MView /> <local:MView />
</Window.DataContext> </Window.DataContext>
@ -12,9 +12,31 @@
<RowDefinition Height="*" /> <RowDefinition Height="*" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<DataGrid Grid.Row="0" ItemsSource="{Binding Passwords}" SelectedItem="{Binding SelectedPassword}" <StackPanel Grid.Row="0" Orientation="Vertical" Margin="10,10,10,10">
AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"> <DataGrid ItemsSource="{Binding Top_passwords}" AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False" SelectionMode="Single">
<DataGrid.Columns> <DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Categoria}" Width="Auto" />
<DataGridTextColumn Binding="{Binding Usuario}" Width="*" />
<DataGridTextColumn Binding="{Binding Password}" Width="*" />
<DataGridTemplateColumn Header="Acciones" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Utilizar"
Command="{Binding DataContext.UtilizarCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
CommandParameter="{Binding}" Margin="5,0,0,0" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid ItemsSource="{Binding Passwords}" SelectedItem="{Binding SelectedPassword}"
AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Header="Categoria" Binding="{Binding Categoria}" Width="Auto" />
<DataGridTextColumn Header="Usuario" Binding="{Binding Usuario}" Width="*" /> <DataGridTextColumn Header="Usuario" Binding="{Binding Usuario}" Width="*" />
<DataGridTextColumn Header="Contraseña" Binding="{Binding Password}" Width="*" /> <DataGridTextColumn Header="Contraseña" Binding="{Binding Password}" Width="*" />
<DataGridTemplateColumn Header="Acciones" Width="Auto"> <DataGridTemplateColumn Header="Acciones" Width="Auto">
@ -33,9 +55,13 @@
</DataGridTemplateColumn> </DataGridTemplateColumn>
</DataGrid.Columns> </DataGrid.Columns>
</DataGrid> </DataGrid>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10,10,10,10"> </StackPanel>
<Button Content="Agregar" Command="{Binding AgregarCommand}" Margin="5" /> <StackPanel Grid.Row="1" Orientation="Vertical">
<Button Content="Cerrar" Command="{Binding CerrarCommand}" Margin="5" /> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="1,1,1,1">
<Button Content="Agregar" Command="{Binding AgregarCommand}" Margin="15" />
<Button Content="Cerrar" Command="{Binding CerrarCommand}" Margin="15" />
</StackPanel>
<Label Content="mav - 2024 - v0.1" Opacity="0.3" HorizontalAlignment="Right" />
</StackPanel> </StackPanel>
</Grid> </Grid>
</Window> </Window>

View File

@ -1,19 +1,16 @@
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Text.Json; using System.Text.Json;
using System.IO; using System.IO;
using System.Windows; using System.Windows;
using WindowsInput;
using WindowsInput.Native;
using Application = System.Windows.Application; using Application = System.Windows.Application;
using System.Windows.Input; using System.Windows.Input;
using MouseButton = System.Windows.Input.MouseButton; using MouseButton = System.Windows.Input.MouseButton;
using System.Windows; using Gma.System.MouseKeyHook;
using System.Windows.Controls; using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
using System.Windows.Input;
using ListBox = System.Windows.Controls.ListBox;
namespace EscribePassword namespace EscribePassword
{ {
@ -24,38 +21,122 @@ namespace EscribePassword
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private IKeyboardMouseEvents _globalHook;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
this.Loaded += MainWindow_Loaded; this.Loaded += MainWindow_Loaded;
this.MouseWheel += MainWindow_Wheel;
HookManager();
} }
private void MainWindow_Loaded(object sender, RoutedEventArgs e) private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{ {
((App)Application.Current).SetInitialWindowPosition(); ((App)Application.Current).SetInitialWindowPosition();
} }
private void MainWindow_Wheel(object sender, MouseWheelEventArgs e)
{
if (DataContext is MView mv)
mv.OnWheel(sender, e);
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e) private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{ {
if (e.ChangedButton == MouseButton.Left) if (e.ChangedButton == MouseButton.Left)
{ {
this.DragMove(); this.DragMove();
} }
else if (e.ChangedButton == MouseButton.Middle)
{
if (DataContext is MView mv && mv.SelectedPassword != null)
{
mv.UtilizarCommand.Execute(mv.SelectedPassword);
e.Handled = true; // Suprimir el evento
} }
} }
}
private void HookManager()
{
_globalHook = Hook.GlobalEvents();
_globalHook.MouseWheel += GlobalHookMouseWheel;
_globalHook.MouseDownExt += GlobalHookMouseDown;
}
private void GlobalHookMouseDown(object sender, MouseEventExtArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Middle)
{
var mouseButtonEventArgs = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Middle)
{
RoutedEvent = UIElement.MouseDownEvent
};
Window_MouseDown(sender, mouseButtonEventArgs);
}
}
private void GlobalHookMouseWheel(object sender, MouseEventArgs e)
{
// Convierte MouseEventArgs a MouseWheelEventArgs
var wheelArgs = new MouseWheelEventArgs(Mouse.PrimaryDevice, 0, e.Delta)
{
RoutedEvent = UIElement.MouseWheelEvent
};
MainWindow_Wheel(sender, wheelArgs);
}
protected override void OnClosed(EventArgs e)
{
_globalHook.MouseWheel -= GlobalHookMouseWheel;
_globalHook.MouseDownExt -= GlobalHookMouseDown;
_globalHook.Dispose();
base.OnClosed(e);
}
}
public partial class MView : ObservableObject public partial class MView : ObservableObject
{ {
[ObservableProperty] [ObservableProperty]
private ObservableCollection<Passwords> passwords; private ObservableCollection<Passwords> passwords;
[ObservableProperty]
private ObservableCollection<Passwords> top_passwords;
[ObservableProperty] [ObservableProperty]
private Passwords selectedPassword; private Passwords selectedPassword;
public void OnWheel(object sender, MouseWheelEventArgs e)
{
if (Passwords.Count == 0) return;
var index = Passwords.IndexOf(SelectedPassword);
var max = Passwords.Count;
if (e.Delta > 0)
index++;
else index--;
if (index < 0) index = max-1;
if (index >= max) index = 0;
SelectedPassword = Passwords[index];
}
partial void OnSelectedPasswordChanged(Passwords value)
{
Top_passwords.Clear();
Top_passwords.Add(value);
}
public MView() public MView()
{ {
// Suscribirse al evento de cierre de la aplicación // Suscribirse al evento de cierre de la aplicación
Application.Current.Exit += OnApplicationExit; Application.Current.Exit += OnApplicationExit;
top_passwords = new ObservableCollection<Passwords>();
passwords = new ObservableCollection<Passwords>(EstadoPersistente.Instance.Passwords); passwords = new ObservableCollection<Passwords>(EstadoPersistente.Instance.Passwords);
SelectedPassword = passwords.FirstOrDefault();
} }
private void OnApplicationExit(object sender, ExitEventArgs e) private void OnApplicationExit(object sender, ExitEventArgs e)