.NET下如何拦截鼠标、键盘消息?Win32NET来帮你

人工智能85

.NET下如何拦截鼠标、键盘消息?Win32NET来帮你

Win32NET是一个Win32API的.NET下封装的类库,包含:

1: 常用win32的API的net封装

2:鼠标、键盘、热键hook钩子模块,

3:模拟键盘输入文字(支持各种字符文字、不同语言的文字)、模拟鼠标点击移动滚动等操作

4.系统硬件信息查询

如何使用该Win32Net库呢?可以在nuget包管理搜索Win32Net,

```csharp;gutter:true;
Install-Package Win32Net -Version 1.2.0


或者直接添加引用,

"Win32Net" Version="1.2.0" /


**如何使用鼠标钩子:**
首先实例化鼠标钩子对象,然后定义鼠标事件回调方法以开始侦听。当您不需要继续监控鼠标消息时,可以取消监控。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>First instantiate a mouse hook object, and then define a mouse event callback method to start listening. When you do not need to continue to monitor mouse messages, you can cancel the monitoring.</font>*</details>

win32.Hooks.MouseHook mouseHook = new Hooks.MouseHook();//实例化鼠标钩子对象
mouseHook.LeftDown += MouseHook_LeftDown;//鼠标左键按下的事件监听回调方法
mouseHook.Start();//开始监听
mouseHook.Stop();//停止监听


**如何使用键盘钩子:**

键盘挂钩类似于鼠标挂钩,方法是首先实例化键盘挂钩对象,然后定义键盘事件回调方法以开始侦听。当您不需要继续监控键盘消息时,可以取消监控。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Keyboard hooks are similar to mouse hooks by first instantiating a keyboard hook object and then defining a keyboard event callback method to start listening. When you do not need to continue to monitor keyboard messages, you can cancel the monitoring.</font>*</details>

Win32.Hooks.KeyboardHook keyboardHook = new Win32.Hooks.KeyboardHook();
keyboardHook.OnKeyUp += KeyboardHook_OnKeyUp;
keyboardHook.Start();
keyboardHook.Stop();


**如何注册全局快捷键**

//WPF 窗口句柄
//IntPtr hwnd = new WindowInteropHelper(WPFWindow对象).Handle;
//winform 窗口句柄
IntPtr hwnd = this.Handle;
//实例化热键对象,需要一个句柄,用于接收消息
Win32.Hooks.SystemHotKey systemHotKey = new Win32.Hooks.SystemHotKey(hwnd);
//热键id,要求唯一
int hotKeyId = 5000;
//注册Alt+Q快捷键
systemHotKey.AddHotKey(hotKeyId, Win32.KeyModifiers.Alt, Keys.Q,
() =>
{
MessageBox.Show("你按了Alt+Q快捷键");
}
);
//注册ESC快捷键
systemHotKey.AddHotKey(hotKeyId + 1, Win32.KeyModifiers.None, Keys.Escape,
() =>
{
this.Close();
}
);


**如何获取系统硬件信息**

Win32.SystemInfo systemInfo = new Win32.SystemInfo();
richTextBox1.AppendText("操作系统:" + systemInfo.operatingSystem.Caption + "\n");
richTextBox1.AppendText("系统ID:" + systemInfo.operatingSystem.SerialNumber + "\n");
richTextBox1.AppendText("操作系统平台:" + systemInfo.operatingSystem.OSLevel + "\n");
richTextBox1.AppendText("系统安装时间:" + systemInfo.operatingSystem.InstallDate + "\n");
richTextBox1.AppendText("系统最近启动时间:" + systemInfo.operatingSystem.LastBootUpTime + "\n");
richTextBox1.AppendText("系统时间:" + systemInfo.operatingSystem.LocalDateTime + "\n");
richTextBox1.AppendText("CPU:" + systemInfo.processor.Name + "\n");
richTextBox1.AppendText("CPU厂商:" + systemInfo.processor.Manufacturer + "\n");
richTextBox1.AppendText("CPU序列号:" + systemInfo.processor.SerialNumber + "\n");
richTextBox1.AppendText("物理内存:" + systemInfo.memory.TotalPhysicalMemory + "\n");
```

喜欢的小伙伴们可以关注学堂公众号,更多内容等你来

项目地址:

码云: https://gitee.com/raoyutian/win32

GitHub: https://gitee.com/raoyutian/win32

Original: https://www.cnblogs.com/raoyutian/p/15815616.html
Author: 明月心技术学堂
Title: .NET下如何拦截鼠标、键盘消息?Win32NET来帮你