1024programmer Asp.Net Simple implementation of .NET Hook and event simulation

Simple implementation of .NET Hook and event simulation

Simple implementation of .NET Hook and event simulation

I have recently become addicted to playing “Stardew Valley”. It was originally a casual game, but now I have played it as an immortal game. I have been playing it for hundreds of hours, and my habit of taking a lunch break has been forced to change.

Although it’s interesting, it’s too outrageous, so please be careful before jumping into the trap. To add, this game should be written based on XNA/MonoGame.

This game will automatically pause when it loses focus and cannot advance automatically. I think the efficiency is too low. For Mr. Qi’s sake, I have to build something that automatically clicks.

If you don’t want to download software, just write one yourself, because to ensure that the game is always in front, the switch must enable global shortcut keys. After searching, I found that there is a SharpHook (tolik.io) for . NET which is quite simple. It took me a few minutes to write one and realize the function.

SharpHook is a cross-platform Hook component, the core is based on libuiohook

Listening event

The documentation on the official website is very concise and easy to understand. I set up shortcut key monitoring to monitor key presses.

var hook = new TaskPoolGlobalHook();
 hook.HookEnabled += OnHookEnabled; // EventHandler
 hook.HookDisabled += OnHookDisabled; // EventHandler

 hook.KeyTyped += OnKeyTyped; // EventHandler

 hook.Run();
 

Then set the timer to start and stop in the event. My goal is to press the left mouse button every two seconds.

private static void OnKeyTyped(object? sender, KeyboardHookEventArgs e)
 {
     if (e.SuppressEvent) return;
     if (e.Data.KeyCode.HasFlag(KeyCode.VcSemicolon)) // ; key
     {
         Console.WriteLine("start timer");
         _timer.Start();
     }
     else if (e.Data.KeyCode.HasFlag(KeyCode.VcL)) //L key
     {
         Console.WriteLine("stop timer");
         _timer.Stop();
     }
     e.SuppressEvent = true;
 }
 

Simulation event

This library can also simulate mouse and keyboard operations. I operate the mouse here and the keyboard is the same.

static EventSimulator _simulator = new EventSimulator();
 private static void _timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
 {
 _simulator.SimulateMousePress(MouseButton.Button1);
 Thread.Sleep(100);
 _simulator.SimulateMouseRelease(MouseButton.Button1);
 }
 

Note that many games monitor the press and release of the mouse separately, which counts as a complete click. If there is no delay set in the middle, the mouse action may not be recognized. That’s how this game works, it wouldn’t work without delay.

Okay, put it in the background after running. When the game gets focus, press “;” to start it. Then hover the mouse over the item and it will automatically pick up/put down. Press “L” to exit.

image

Keys and Others

Refer to Key Code Mappings | SharpHook (tolik.io) to see key names and Key Code. When debugging, if you set a breakpoint in the OnKeyTyped event, it will be very stuck. Just be patient and it will be normal.

In addition, I have been unsuccessful in hooking the F function key above, but ESC can be responded normally. I have not studied the reason in depth. I will check it out in the future if it is really necessary.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/simple-implementation-of-net-hook-and-event-simulation/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索