From 2af31aee57ba9e61fd5d6f259d2a09c24ce524dd Mon Sep 17 00:00:00 2001 From: Pavel Savchuk Date: Wed, 22 May 2024 18:07:32 +0300 Subject: [PATCH] Add more UI to explain functionality, switch to the left mouse button down instead of the right one. --- Clicker/MainWindow.xaml | 13 +++++++++---- Clicker/MainWindow.xaml.cs | 25 +++++++++++++------------ Clicker/MouseOperations.cs | 30 ++++++++++++++++++++++-------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/Clicker/MainWindow.xaml b/Clicker/MainWindow.xaml index 7070e5b..fb632a0 100644 --- a/Clicker/MainWindow.xaml +++ b/Clicker/MainWindow.xaml @@ -7,22 +7,27 @@ mc:Ignorable="d" WindowStartupLocation="CenterScreen" Closing="Window_Closing" - Title="Hold my mouse" Height="177.037" Width="476.908"> + Title="Hold my mouse" Height="161" Width="520"> - + + + - - + diff --git a/Clicker/MainWindow.xaml.cs b/Clicker/MainWindow.xaml.cs index be5b9b8..7954955 100644 --- a/Clicker/MainWindow.xaml.cs +++ b/Clicker/MainWindow.xaml.cs @@ -36,6 +36,7 @@ public MainWindow() private IntPtr _windowHandle; private HwndSource _source; + protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); @@ -44,9 +45,9 @@ protected override void OnSourceInitialized(EventArgs e) _source = HwndSource.FromHwnd(_windowHandle); _source.AddHook(HwndHook); - shorcut.Text = Properties.Settings.Default["Text"].ToString(); + shortcut.Text = Properties.Settings.Default["Text"].ToString(); - map_buttons(); + registerShortcut(); } private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) @@ -61,7 +62,7 @@ private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref int vkey = (((int)lParam >> 16) & 0xFFFF); if (vkey == KeyInterop.VirtualKeyFromKey((Key)Properties.Settings.Default.Key)) { - MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightDown); + MouseOperations.ToggleButton(); } handled = true; break; @@ -83,14 +84,14 @@ private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) if (!this.modifiers.Contains(e.Key)) { Properties.Settings.Default["Modifiers"] = e.KeyboardDevice.Modifiers.GetHashCode(); Properties.Settings.Default["Key"] = e.Key.GetHashCode(); - Properties.Settings.Default["Text"] = build_text(e); + Properties.Settings.Default["Text"] = keysToText(e); Properties.Settings.Default.Save(); - shorcut.Text = build_text(e); + shortcut.Text = keysToText(e); - grid.Focus(); + registerShortcut(); - map_buttons(); + shortcut.Background = Brushes.White; } } @@ -99,11 +100,11 @@ private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (!this.modifiers.Contains(e.Key)) { - shorcut.Text = build_text(e); + shortcut.Text = keysToText(e); } } - private String build_text(KeyEventArgs e) + private String keysToText(KeyEventArgs e) { StringBuilder builder = new StringBuilder(); @@ -114,7 +115,7 @@ private String build_text(KeyEventArgs e) return builder.ToString(); } - private void map_buttons() + private void registerShortcut() { uint modifiers = (uint)Properties.Settings.Default.Modifiers; uint key = (uint)KeyInterop.VirtualKeyFromKey((Key)Properties.Settings.Default.Key); @@ -124,12 +125,12 @@ private void map_buttons() private void Shorcut_GotFocus(object sender, RoutedEventArgs e) { - shorcut.Background = Brushes.LightYellow; + shortcut.Background = Brushes.LightYellow; } private void Shorcut_LostFocus(object sender, RoutedEventArgs e) { - shorcut.Background = Brushes.White; + shortcut.Background = Brushes.White; } private void MenuItem_Click(object sender, RoutedEventArgs e) diff --git a/Clicker/MouseOperations.cs b/Clicker/MouseOperations.cs index 258e070..f9c5670 100644 --- a/Clicker/MouseOperations.cs +++ b/Clicker/MouseOperations.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using System.Windows.Input; public class MouseOperations { @@ -16,6 +17,13 @@ public enum MouseEventFlags RightUp = 0x00000010 } + [Flags] + public enum MouseButtons + { + VK_LBUTTON = 0x01, + VK_RBUTTON = 0x02 + } + [DllImport("user32.dll", EntryPoint = "SetCursorPos")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetCursorPos(int x, int y); @@ -27,17 +35,23 @@ public enum MouseEventFlags [DllImport("user32.dll")] private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); - public static void SetCursorPosition(int x, int y) - { - SetCursorPos(x, y); - } + [DllImport("user32.dll")] + private static extern short GetAsyncKeyState(int vKey); - public static void SetCursorPosition(MousePoint point) + public static void ToggleButton() { - SetCursorPos(point.X, point.Y); + var currentState = GetAsyncKeyState((int)MouseButtons.VK_LBUTTON); + if (currentState != 0) + { + MouseEvent(MouseEventFlags.LeftUp); + } + else + { + MouseEvent(MouseEventFlags.LeftDown); + } } - public static MousePoint GetCursorPosition() + private static MousePoint GetCursorPosition() { MousePoint currentMousePoint; var gotPoint = GetCursorPos(out currentMousePoint); @@ -45,7 +59,7 @@ public static MousePoint GetCursorPosition() return currentMousePoint; } - public static void MouseEvent(MouseEventFlags value) + private static void MouseEvent(MouseEventFlags value) { MousePoint position = GetCursorPosition(); mouse_event((int)value, position.X, position.Y, 0, 0);