Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created December 2, 2009 16:27
Show Gist options
  • Save bellbind/247329 to your computer and use it in GitHub Desktop.
Save bellbind/247329 to your computer and use it in GitHub Desktop.
[c#][windows]X-Mouse setting tool on Windows 7/Vista
// [X-Mouse setting tool on Windows 7/Vista]
// build on 64bit .NET 3.5:
// c:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe XMouse.cs
// or on 32bit .NET 3.5:
// c:\Windows\Microsoft.NET\Framework\v3.5\csc.exe XMouse.cs
using Microsoft.Win32;
class XMouse {
static void Main(string[] args) {
if (args.Length == 0 || !(args[0] == "on" || args[0] == "off")) {
ShowHelp();
ShowXMouse();
return;
}
bool followActivation = false;
bool autoRaise = false;
int raiseTime = 0;
if (args[0] == "on") {
followActivation = true;
if (args.Length == 2) {
try {
raiseTime = int.Parse(args[1]);
autoRaise = true;
} catch (System.FormatException ex) {
System.Console.WriteLine(ex);
ShowHelp();
ShowXMouse();
return;
}
}
}
PrintNewXMouse(followActivation, autoRaise, raiseTime);
SetXMouse(followActivation, autoRaise, raiseTime);
System.Console.WriteLine("Setting enabled after re-Logined.");
ShowXMouse();
}
static void PrintNewXMouse(bool followActivation, bool autoRaise, int raiseTime) {
System.Console.WriteLine("New XMouse Settings:");
System.Console.WriteLine(" Follow Activation: {0}", followActivation);
System.Console.WriteLine(" Auto Raise: {0}", autoRaise);
System.Console.WriteLine(" Raise Time (msec): {0}", raiseTime);
}
static void ShowHelp() {
System.Console.WriteLine("Usage: XMouse [on [NUM]|off]");
System.Console.WriteLine("Options: ");
System.Console.WriteLine(" on: follow mouse activation");
System.Console.WriteLine(" on NUM: follow activation then auto raise after NUM msec");
System.Console.WriteLine(" off: disable follow activation/auto raise");
}
static void ShowXMouse() {
using (RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop")) {
byte[] masks = regkey.GetValue("UserPreferencesMask") as byte[];
System.Console.WriteLine("Current XMouse Settings:");
System.Console.WriteLine(" Follow Activation: {0}", (masks[0] & 0x01) != 0);
System.Console.WriteLine(" Auto Raise: {0}", (masks[0] & 0x40) != 0);
System.Console.WriteLine(" Raise Time (msec): {0}", regkey.GetValue("ActiveWndTrkTimeout"));
}
}
static void SetXMouse(bool followActivation, bool autoRaise, int raiseTime) {
using (RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true)) {
byte[] masks = regkey.GetValue("UserPreferencesMask") as byte[];
if (followActivation) {
masks[0] |= 0x01;
} else {
masks[0] = (byte) (masks[0] & ~0x01);
}
if (autoRaise) {
masks[0] |= 0x40;
} else {
masks[0] = (byte) (masks[0] & ~0x40);
}
regkey.SetValue("UserPreferencesMask", masks);
if (autoRaise) {
regkey.SetValue("ActiveWndTrkTimeout", raiseTime);
} else {
regkey.DeleteValue("ActiveWndTrkTimeout", false);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment