How to Intercept Mouse and Keyboard Messages in .NET? Win32NET Helps You

How to Intercept Mouse and Keyboard Messages in .NET? Win32NET Helps You

Win32NET is a .NET wrapper class library for Win32 API

Last updated 1/20/2022 2:12 PM
明月心技术学堂
3 min read
Category
.NET
Tags
.NET C# Intercept Keyboard Intercept Mouse

Win32NET is a .NET class library that wraps Win32 API, including:

  1. .NET wrappers for commonly used Win32 APIs
  2. Mouse, keyboard, and hotkey hook modules
  3. Simulating keyboard input (supports various characters, text in different languages), simulating mouse clicks, movement, scrolling, etc.
  4. System hardware information query

How to use the Win32Net library? You can search for Win32Net in the NuGet Package Manager.

Install-Package Win32Net -Version 1.2.0

Or add a direct reference:

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

How to use the mouse hook:

First, instantiate a mouse hook object, then define the mouse event callback method, and start listening. When you no longer need to listen to mouse events, you can stop listening.

win32.Hooks.MouseHook mouseHook = new Hooks.MouseHook(); // instantiate the mouse hook object
mouseHook.LeftDown += MouseHook_LeftDown; // event listener callback method for left mouse button press
mouseHook.Start(); // start listening
mouseHook.Stop(); // stop listening

How to use the keyboard hook:

The keyboard hook works similarly to the mouse hook. First, instantiate a keyboard hook object, then define the keyboard event callback method, and start listening. When you no longer need to listen to keyboard events, you can stop listening.

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

How to register a global hotkey

// WPF window handle
// IntPtr hwnd = new WindowInteropHelper(WPFWindowObject).Handle;
// WinForm window handle
IntPtr hwnd = this.Handle;
// Instantiate the hotkey object, requires a handle to receive messages
Win32.Hooks.SystemHotKey systemHotKey = new Win32.Hooks.SystemHotKey(hwnd);
// Hotkey ID, must be unique
int hotKeyId = 5000;
// Register the Alt+Q hotkey
systemHotKey.AddHotKey(hotKeyId, Win32.KeyModifiers.Alt, Keys.Q,
 () =>
 {
     MessageBox.Show("You pressed the Alt+Q hotkey");
 }
);
// Register the ESC hotkey
systemHotKey.AddHotKey(hotKeyId + 1, Win32.KeyModifiers.None, Keys.Escape,
 () =>
 {
     this.Close();
 }
);

How to get system hardware information

SystemInfo systemInfo = new SystemInfo();
richTextBox1.AppendText("Operating system: " + systemInfo.operatingSystem.Caption + "\n");
richTextBox1.AppendText("System ID: " + systemInfo.operatingSystem.SerialNumber + "\n");
richTextBox1.AppendText("OS platform: " + systemInfo.operatingSystem.OSLevel + "\n");
richTextBox1.AppendText("System installation time: " + systemInfo.operatingSystem.InstallDate + "\n");
richTextBox1.AppendText("Last system boot time: " + systemInfo.operatingSystem.LastBootUpTime + "\n");
richTextBox1.AppendText("System time: " + systemInfo.operatingSystem.LocalDateTime + "\n");
richTextBox1.AppendText("CPU: " + systemInfo.processor.Name + "\n");
richTextBox1.AppendText("CPU vendor: " + systemInfo.processor.Manufacturer + "\n");
richTextBox1.AppendText("CPU serial number: " + systemInfo.processor.SerialNumber + "\n");
richTextBox1.AppendText("Physical memory: " + systemInfo.memory.TotalPhysicalMemory + "\n");
Keep Exploring

Related Reading

More Articles