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.Data;
using System;
using System.Threading;
using System.Windows;
using Application = System.Windows.Application;
namespace EscribePassword
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static Mutex _mutex = null;
public KeyboardHelper PasteLogic { get; } = new KeyboardHelper();
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);
PasteLogic.SaveCurrentWindow();
SetInitialWindowPosition();
@ -26,18 +37,12 @@ namespace EscribePassword
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;
@ -48,11 +53,9 @@ namespace EscribePassword
newLeft = screen.WorkingArea.Right - mainWindow.Width;
}
// Aplicar la nueva posición
mainWindow.Left = newLeft;
mainWindow.Top = newTop;
}
}
}
}

View File

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

View File

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

View File

@ -3,7 +3,7 @@
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="300"
AllowsTransparency="True" WindowStyle="None" MouseDown="Window_MouseDown">
AllowsTransparency="True" WindowStyle="None" MouseDown="Window_MouseDown" Topmost="True">
<Window.DataContext>
<local:MView />
</Window.DataContext>
@ -12,30 +12,56 @@
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" ItemsSource="{Binding Passwords}" SelectedItem="{Binding SelectedPassword}"
AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Usuario" Binding="{Binding Usuario}" Width="*" />
<DataGridTextColumn Header="Contraseña" Binding="{Binding Password}" Width="*" />
<DataGridTemplateColumn Header="Acciones" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Eliminar"
Command="{Binding DataContext.EliminarCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
CommandParameter="{Binding}" />
<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>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10,10,10,10">
<Button Content="Agregar" Command="{Binding AgregarCommand}" Margin="5" />
<Button Content="Cerrar" Command="{Binding CerrarCommand}" Margin="5" />
<StackPanel Grid.Row="0" Orientation="Vertical" Margin="10,10,10,10">
<DataGrid ItemsSource="{Binding Top_passwords}" AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False" SelectionMode="Single">
<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="Contraseña" Binding="{Binding Password}" Width="*" />
<DataGridTemplateColumn Header="Acciones" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Eliminar"
Command="{Binding DataContext.EliminarCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
CommandParameter="{Binding}" />
<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>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Vertical">
<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>
</Grid>
</Window>

View File

@ -1,19 +1,16 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.Generic;
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;
using Gma.System.MouseKeyHook;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
namespace EscribePassword
{
@ -24,38 +21,122 @@ namespace EscribePassword
/// </summary>
public partial class MainWindow : Window
{
private IKeyboardMouseEvents _globalHook;
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
this.MouseWheel += MainWindow_Wheel;
HookManager();
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
((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)
{
if (e.ChangedButton == MouseButton.Left)
{
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
{
[ObservableProperty]
private ObservableCollection<Passwords> passwords;
[ObservableProperty]
private ObservableCollection<Passwords> top_passwords;
[ObservableProperty]
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()
{
// Suscribirse al evento de cierre de la aplicación
Application.Current.Exit += OnApplicationExit;
top_passwords = new ObservableCollection<Passwords>();
passwords = new ObservableCollection<Passwords>(EstadoPersistente.Instance.Passwords);
SelectedPassword = passwords.FirstOrDefault();
}
private void OnApplicationExit(object sender, ExitEventArgs e)