-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_toggle.cs
116 lines (104 loc) · 4.01 KB
/
mouse_toggle.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace mouse_switcher
{
class mouse_switcher
{
[DllImport("user32.dll", SetLastError = true)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
[DllImport("user32.dll")]
static extern bool SwapMouseButton(bool fSwap);
static bool SetCursorScheme(string schemeName)
{
bool result = false;
string[] cursor_names =
{
"Arrow", "Help", "AppStarting",
"Wait", "Crosshair", "IBeam",
"NWPen", "No", "SizeNS",
"SizeWE", "SizeNWSE", "SizeNESW",
"SizeAll", "UpArrow", "Hand"
};
string schemes_key = @"Control Panel\Cursors\Schemes";
string current_scheme_key = @"Control Panel\Cursors";
string cursor_set = "";
RegistryKey scheme = Registry.CurrentUser.OpenSubKey(schemes_key, false);
if ( scheme != null )
{
try
{
cursor_set = scheme.GetValue(schemeName).ToString();
}
catch (Exception)
{
Console.WriteLine("{0}: unable to read scheme", schemeName);
}
scheme.Close();
}
string[] filenames = cursor_set.Split(',');
if (filenames.Length != cursor_names.Length)
{
Console.WriteLine("{0}: item count mismatch", schemeName);
}
else
{
Console.WriteLine("{0}: is good", schemeName);
RegistryKey current_scheme = Registry.CurrentUser.OpenSubKey(current_scheme_key, true);
current_scheme.SetValue("", schemeName);
current_scheme.SetValue("Scheme Source", 1);
for (int i = 0; i < cursor_names.Length; i++)
{
Console.WriteLine("{0} -> {1}", cursor_names[i], filenames[i]);
current_scheme.SetValue(cursor_names[i], filenames[i].Trim());
}
current_scheme.Close();
result = true;
}
return result;
}
static bool ApplyCursorScheme()
{
const uint SPI_SETCURSORS = 0x0057;
const uint SPIF_UPDATEINIFILE = 0x01;
const uint SPIF_SENDCHANGE = 0x02;
return SystemParametersInfo(SPI_SETCURSORS, 0, IntPtr.Zero, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
static bool SaveButtonsState(bool lefthanded)
{
bool result = false;
try
{
RegistryKey mouse_key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Mouse", true);
mouse_key.SetValue(@"SwapMouseButtons", lefthanded ? "1" : "0");
result = true;
mouse_key.Close();
}
catch (Exception e)
{
Console.WriteLine("{0}: unable to SaveButtonsState", e.Message);
}
return result;
}
static void Main(string[] args)
{
string predefined_lefthanded_scheme_name = "mouse_toggle_scheme_left";
string predefined_righthanded_scheme_name = "mouse_toggle_scheme_right";
bool leftHandedNow = SwapMouseButton(true);
if (leftHandedNow)
{
SwapMouseButton(false);
SaveButtonsState(false);
SetCursorScheme(predefined_righthanded_scheme_name);
}
else
{
SaveButtonsState(true);
SetCursorScheme(predefined_lefthanded_scheme_name);
}
ApplyCursorScheme();
}
}
}