How to intercept mouse and keyboard messages under. NET? Win32NET to help you

How to intercept mouse and keyboard messages under. NET? Win32NET to help you

Win32NET is a class library encapsulated under the Win32API. NET

最后更新 1/20/2022 2:12 PM
明月心技术学堂
预计阅读 3 分钟
分类
.NET
标签
.NET C# intercept keyboard Block mouse

Win32NET is a class library encapsulated under the. NET of the Win32API, including:

  1. Net encapsulation of commonly used Win32 APIs
  2. Mouse, keyboard, hotkey hook module,
  3. Simulate keyboard input of text (support various characters and text in different languages), simulate mouse click, move, scroll and other operations
  4. Query system hardware information

How to use this Win32Net library? You can search Win32Net in nuget package management,

Install-Package Win32Net -Version 1.2.0

Or add a quote directly,

<PackageReference Include="Win32Net" Version="1.2.0" /

** How to use the mouse hook: **

First instantiate a mouse hook object, then define a mouse event callback method, and start monitoring. When you no longer need to continue monitoring mouse information, you can cancel monitoring.

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

** How to use keyboard hooks: **

Keyboard hooks are used similar to mouse hooks. First, instantiate a keyboard hook object, then define a keyboard event callback method, and start monitoring. When you no longer need to continue monitoring keyboard information, you can cancel monitoring.

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

** How to register global shortcut keys **

//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();
 }
 );

How to obtain system hardware information

SystemInfo systemInfo = new 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");
Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读