`

使用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

 

 

 

分享到:
评论
1 楼 wjason 2009-08-03  
重启了一回不好用,
看来还需要修改注册表。
明天补上代码。

相关推荐

Global site tag (gtag.js) - Google Analytics