·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 使用C#:自动切换鼠标的左右手习惯

使用C#:自动切换鼠标的左右手习惯

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

不知道我得的是鼠标手,还是肩周炎。

长时间右手(或者左手)使用鼠标的话,那只胳膊便会不自在。

于是便有了切换鼠标主次要键的需求。

 

【控制面板->鼠标】有更改它的设置,可点来点去让我觉得不够方便。

我需要的是“一个命令就能搞定它”,这样我就可以在命令行,或者程序加载器里面方便的运行他。

 

下面的代码便是要实现这一需求:

他是一个命令行程序。如果当前鼠标是右手习惯,则将鼠标习惯设置为左手,反之设置成右手习惯。

 

实现代码如下:

 

C#代码  收藏代码
  1. using System;  
  2. using System.Runtime.InteropServices;  
  3. using Microsoft.Win32;  
  4.   
  5. namespace SwapMouseModel  
  6. {  
  7.     class PRogram  
  8.     {  
  9.         [DllImport("user32")]  
  10.         public static extern int SwapMouseButton(int bSwap);  
  11.   
  12.         [DllImport("user32")]  
  13.         public static extern int GetSystemMetrics(int nIndex);  
  14.   
  15.         //public readonly static int SM_SWAPBUTTON = 23;  
  16.         public const int SM_SWAPBUTTON = 23;  
  17.   
  18.         public static void Main(string[] args)  
  19.         {  
  20.   
  21.             var key = Registry.CurrentUser.CreateSubKey("Control Panel\\Mouse\\");  
  22.               
  23.   
  24.             if (GetSystemMetrics(SM_SWAPBUTTON) == 0)  
  25.             {  
  26.                 //case: right hand model, change to left hand model.  
  27.                 SwapMouseButton(1);  
  28.                 key.SetValue("SwapMouseButtons", "1", RegistryValueKind.String);  
  29.             }  
  30.             else  
  31.             {  
  32.                 //case: left hand model, change to right hand model.  
  33.                 SwapMouseButton(0);  
  34.                 key.SetValue("SwapMouseButtons", 0, RegistryValueKind.String);  
  35.             }  
  36.             Console.WriteLine("end");  
  37.             //Console.ReadLine();  
  38.         }  
  39.     }  
  40. }  

 

 

 

总结下对C#新认识:

 

1. static与const不能同时修饰一个变量

    类成员是const就自动是static。因此或者只用const, 或者可以用readonly static

2. SwapMouseButton Function

    通过该链接可以展开查看“windows关于mouse”的api。

    另外注意,该方法不会修改注册表。所以为了重启后修改依然有效,需要另行保存注册表设置。

3. GetSystemMetrics Function

    通过该链接可以展开查看如何获得“其他类似的属性”

4. C#中可以使用var。

 

Google到的参考链接:

http://www.theeldergeek.com/forum/lofiversion/index.php?t10400.html

http://stackoverflow.com/questions/653911/swapping-left-and-right-mouse-button-in-net