Skip to content

Commit

Permalink
Add more UI to explain functionality, switch to the left mouse button…
Browse files Browse the repository at this point in the history
… down instead of the right one.
  • Loading branch information
Casilio committed May 22, 2024
1 parent b758c69 commit 2af31ae
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
13 changes: 9 additions & 4 deletions Clicker/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="About" Click="MenuItem_Click"></MenuItem>
</Menu>
<Grid VerticalAlignment="Center" Focusable="True" Name="grid">
<Grid VerticalAlignment="Bottom" Focusable="false" Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="78"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0">RMB Down:</Label>
<TextBox Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" KeyUp="TextBox_KeyUp" KeyDown="TextBox_KeyDown" IsReadOnly="true" Name="shorcut" GotFocus="Shorcut_GotFocus" LostFocus="Shorcut_LostFocus"/>
<Label Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Content="Register a shortcut below." HorizontalContentAlignment="Center" FontSize="12"/>
<Label Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Content="Press choosen shortcut to emulate 'mouse button down' event" HorizontalContentAlignment="Center" FontSize="12"/>
<Label Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Content="Press it again to emulate 'mouse button up' event (or just click your physical mouse button)" HorizontalContentAlignment="Center" FontSize="12"/>
<Label Grid.Column="0" Grid.Row="3">LMB Down:</Label>
<TextBox Grid.Column="1" Grid.Row="3" VerticalAlignment="Center" KeyUp="TextBox_KeyUp" KeyDown="TextBox_KeyDown" IsReadOnly="true" Name="shortcut" GotFocus="Shorcut_GotFocus" LostFocus="Shorcut_LostFocus"/>
</Grid>
</DockPanel>
</Window>
25 changes: 13 additions & 12 deletions Clicker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public MainWindow()

private IntPtr _windowHandle;
private HwndSource _source;

protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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;
}

}
Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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)
Expand Down
30 changes: 22 additions & 8 deletions Clicker/MouseOperations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Windows.Input;

public class MouseOperations
{
Expand All @@ -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);
Expand All @@ -27,25 +35,31 @@ 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);
if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
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);
Expand Down

0 comments on commit 2af31ae

Please sign in to comment.