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

本文介绍了一个简单的C#程序,用于一键切换鼠标的主次要键设置,适用于鼠标手或肩周炎患者需要频繁切换鼠标操作习惯的情况。通过调用Windows API函数实现设置的即时生效,并更新注册表以保持设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

 

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

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

 

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

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

 

实现代码如下:

 

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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值