C# 用全局钩子使鼠标按键左右键互换,鼠标移动反向相反
时间: 2023-07-12 15:16:42 浏览: 153
实现这个需求需要使用 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 函数取消注册。
阅读全文