linux 鼠标 反向,linux 鼠标左右键互换实现代码

linux鼠标左右键互换脚本,作者今天, 使用 GNOME, 发现 Linux 也需要这些工具之一, 所以有了这个脚本。非常简单好用。

lr="l"

for id in `xinput list | grep "pointer" | sed -r 's/.*id=([0-9]+).*/\1/'`;

do

if [[ $lr == "r" ]]

then

xinput --set-button-map $id 1 2 3 2>/dev/null

else

xinput --set-button-map $id 3 2 1 2>/dev/null

fi

done

filepath=$0

if [[ $lr == "r" ]]

then

echo "now is right hand."

sed -i '1s/lr="r"/lr="l"/' $filepath

else

echo "now is left hand."

sed -i '1s/lr="l"/lr="r"/' $filepath

fi

ubuntu命令切换鼠标左右键

话不多说,让我们开始切换:

首先让我们把鼠标切换到反手(这里之所以用反手一词,是因为有人可能是左撇子,有人是右撇子「还有右撇子?」):

xmodmap -e 'pointer = 3 2 1'

请拿出你的小鼠标摇一摇点一点,

是不是已经切换到反手了捏?!

好的,那让我们再切换回来:

xmodmap -e 'pointer = 1 2 3'

请再次猛烈的点击一次!

是不是又肥来了?!

不过每次切换都要输入29个字符,雅蠛蝶!

那就给他们起个小昵称嘛!

# filename .bashrc

alias mr="xmodmap -e 'pointer = 3 2 1'"

alias ml="xmodmap -e 'pointer = 1 2 3'"

好了,这篇文章就介绍到这了,希望大家以后多多支持我们。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这个需求需要使用 C# 中的钩子函数来实现全局鼠标事件监听。具体步骤如下: 1. 引入 System.Runtime.InteropServices 命名空间。 2. 定义鼠标事件枚举类型: ```csharp public enum MouseEventFlags { LEFTDOWN = 0x00000002, LEFTUP = 0x00000004, MIDDLEDOWN = 0x00000020, MIDDLEUP = 0x00000040, MOVE = 0x00000001, ABSOLUTE = 0x00008000, RIGHTDOWN = 0x00000008, RIGHTUP = 0x00000010, WHEEL = 0x00000800, XDOWN = 0x00000080, XUP = 0x00000100 } ``` 3. 定义 MouseHookStruct 结构体: ```csharp [StructLayout(LayoutKind.Sequential)] public struct MouseHookStruct { public POINT pt; public int hwnd; public int wHitTestCode; public int dwExtraInfo; } ``` 4. 定义全局鼠标事件委托类型: ```csharp public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam); ``` 5. 定义全局变量和函数: ```csharp private static int hHook = 0; private static HookProc HookProcedure; [DllImport("user32.dll")] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); [DllImport("user32.dll")] public static extern int CallNextHookEx(int hHook, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] public static extern int UnhookWindowsHookEx(int hHook); ``` 6. 实现钩子函数: ```csharp private static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { MouseHookStruct mhs = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct)); if (wParam == (IntPtr)MouseEventFlags.LEFTDOWN) { MouseEventFlags button = MouseEventFlags.LEFTDOWN; MouseEventFlags swappedButton = MouseEventFlags.RIGHTDOWN; SwapMouseButton(button, swappedButton); } else if (wParam == (IntPtr)MouseEventFlags.RIGHTDOWN) { MouseEventFlags button = MouseEventFlags.RIGHTDOWN; MouseEventFlags swappedButton = MouseEventFlags.LEFTDOWN; SwapMouseButton(button, swappedButton); } else if (wParam == (IntPtr)MouseEventFlags.MOVE) { int x = mhs.pt.X; int y = mhs.pt.Y; SwapMouseMovement(ref x, ref y); mhs.pt.X = x; mhs.pt.Y = y; Marshal.StructureToPtr(mhs, lParam, true); } } return CallNextHookEx(hHook, nCode, wParam, lParam); } ``` 7. 实现鼠标按键互换鼠标移动反向相反的方法: ```csharp private static void SwapMouseButton(MouseEventFlags button, MouseEventFlags swappedButton) { int flags = GetSystemMetrics(0x0010); if ((flags & 0x0001) != 0) { // Primary mouse button is currently left button if ((button == MouseEventFlags.LEFTDOWN) || (button == MouseEventFlags.LEFTUP)) { mouse_event((int)swappedButton, 0, 0, 0, 0); } else if ((button == MouseEventFlags.RIGHTDOWN) || (button == MouseEventFlags.RIGHTUP)) { mouse_event((int)MouseEventFlags.LEFTDOWN, 0, 0, 0, 0); mouse_event((int)MouseEventFlags.LEFTUP, 0, 0, 0, 0); } } else { // Primary mouse button is currently right button if ((button == MouseEventFlags.RIGHTDOWN) || (button == MouseEventFlags.RIGHTUP)) { mouse_event((int)swappedButton, 0, 0, 0, 0); } else if ((button == MouseEventFlags.LEFTDOWN) || (button == MouseEventFlags.LEFTUP)) { mouse_event((int)MouseEventFlags.RIGHTDOWN, 0, 0, 0, 0); mouse_event((int)MouseEventFlags.RIGHTUP, 0, 0, 0, 0); } } } private static void SwapMouseMovement(ref int x, ref int y) { int width = Screen.PrimaryScreen.Bounds.Width; int height = Screen.PrimaryScreen.Bounds.Height; x = width - x; y = height - y; } ``` 8. 注册全局钩子并启动: ```csharp HookProcedure = new HookProc(MouseHookProc); hHook = SetWindowsHookEx(14, HookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); ``` 需要注意的是,这里使用了 user32.dll 中的一些函数,需要在代码中进行引用。同时,这里使用了 SetWindowsHookEx 函数注册了一个全局鼠标钩子,需要在程序关闭时调用 UnhookWindowsHookEx 函数取消注册。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值