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

2018-06-17 20:32:41来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

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

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

 

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

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

 

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

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

 

实现代码如下:

 

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:利用 Windows API Code Pack 修改音乐的 ID3 信息

下一篇:怎么样去理解抽象类