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


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

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

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

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

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

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

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

    实现代码如下:

    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

  • 相关阅读:
    iOS 统计项目代码行数
    iOS Xcode 8 快捷键 (注释 失效 处理)
    iOS NSSet 学习 “无序数组” & 去重 案例
    iOS 学习@autoreleasepool{}
    iOS 学习如何声明私有变量和私有方法
    iOS 系统认知 debug distribution release 和 #ifdef DEBUG
    iOS copy 和 mutableCopy 学习
    iOS 关于 Missing iOS Distribution signing identity for.... 等 打包 校验 出现的事故 处理经验
    iOS NSCoding 的学习 和 使用
    TeamViewer被发现用于(检测为)商业用途解决方案(绝对有效)
  • 原文地址:https://www.cnblogs.com/gc2013/p/3833521.html
Copyright © 2020-2023  润新知