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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

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

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

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

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

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

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

实现代码如下:

C#代码

using System; using System.Runtime.InteropServices; using Microsoft.Win32; namespace SwapMouseModel { class PRogram { [DllImport("user32")] public static extern int SwapMouseButton(int bSwap); [DllImport("user32")] public static extern int GetSystemMetrics(int nIndex); //public readonly static int SM_SWAPBUTTON = 23; public const int SM_SWAPBUTTON = 23; public static void Main(string[] args) { var key = Registry.CurrentUser.CreateSubKey("Control Panel\\Mouse\\"); if (GetSystemMetrics(SM_SWAPBUTTON) == 0) { //case: right hand model, change to left hand model. SwapMouseButton(1); key.SetValue("SwapMouseButtons", "1", RegistryValueKind.String); } else { //case: left hand model, change to right hand model. SwapMouseButton(0); key.SetValue("SwapMouseButtons", 0, RegistryValueKind.String); } Console.WriteLine("end"); //Console.ReadLine(); } } }

总结下对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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航