-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseApp.cs
More file actions
86 lines (71 loc) · 2.66 KB
/
Copy pathMouseApp.cs
File metadata and controls
86 lines (71 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
using MouseToggle.Properties;
namespace MouseToggle
{
public class MouseApp : ApplicationContext
{
private const String SWAP_KEY = "SwapMouseButtons";
private NotifyIcon notifyIcon;
private ContextMenuStrip contextMenu;
private bool leftHanded;
private DateTime lastClick = DateTime.MinValue;
public MouseApp()
{
contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Exit", Resources.exit, handleExitClick);
leftHanded = readLeft();
notifyIcon = new NotifyIcon();
notifyIcon.Text = "Toggle mouse button";
notifyIcon.Icon = leftHanded ? Resources.mouse_right : Resources.mouse_left;
notifyIcon.MouseClick += handleMouseClick;
notifyIcon.Visible = true;
notifyIcon.ContextMenuStrip = contextMenu;
}
private void handleMouseClick(object sender, MouseEventArgs e)
{
DateTime now = DateTime.Now;
TimeSpan lastSpan = now - lastClick;
toggle();
if (lastSpan < TimeSpan.FromSeconds(0.5))
{
// Shows context menu
MethodInfo showContext = notifyIcon.GetType().GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
showContext.Invoke(notifyIcon, null);
lastClick = DateTime.MinValue;
}
else
{
// Makes sure that single click doesn't show context menu
if (contextMenu.Visible)
contextMenu.Visible = false;
lastClick = now;
}
}
private void toggle()
{
leftHanded = !leftHanded;
notifyIcon.Icon = leftHanded ? Resources.mouse_right : Resources.mouse_left;
SwapMouseButton(leftHanded);
}
private void handleExitClick(object sender, EventArgs e)
{
notifyIcon.Visible = false;
Application.Exit();
}
private bool readLeft()
{
using (RegistryKey mouseRegistryKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Mouse", true))
{
String value = mouseRegistryKey.GetValue(SWAP_KEY) as String;
return value == "1";
}
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SwapMouseButton([param: MarshalAs(UnmanagedType.Bool)] bool fSwap);
}
}