139 lines
3.5 KiB
C#
139 lines
3.5 KiB
C#
using InputInterceptorNS;
|
|
using Roboto.Core;
|
|
using Serilog;
|
|
|
|
namespace Roboto.Input;
|
|
|
|
public sealed class InterceptionInputController : IInputController, IDisposable
|
|
{
|
|
private readonly Humanizer? _humanizer;
|
|
private KeyboardHook? _keyboard;
|
|
private MouseHook? _mouse;
|
|
private bool _disposed;
|
|
|
|
public bool IsInitialized => _keyboard is not null && _mouse is not null;
|
|
|
|
public InterceptionInputController(Humanizer? humanizer = null)
|
|
{
|
|
_humanizer = humanizer;
|
|
}
|
|
|
|
public bool Initialize()
|
|
{
|
|
try
|
|
{
|
|
if (!InputInterceptor.CheckDriverInstalled())
|
|
{
|
|
Log.Warning("Interception driver not installed");
|
|
return false;
|
|
}
|
|
|
|
if (!InputInterceptor.Initialize())
|
|
{
|
|
Log.Warning("Failed to load Interception native library");
|
|
return false;
|
|
}
|
|
|
|
_keyboard = new KeyboardHook();
|
|
_mouse = new MouseHook();
|
|
Log.Information("Interception input controller initialized");
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Failed to initialize Interception hooks");
|
|
_keyboard = null;
|
|
_mouse = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void KeyDown(ushort scanCode)
|
|
{
|
|
_keyboard?.SimulateKeyDown((KeyCode)scanCode);
|
|
}
|
|
|
|
public void KeyUp(ushort scanCode)
|
|
{
|
|
_keyboard?.SimulateKeyUp((KeyCode)scanCode);
|
|
}
|
|
|
|
public void KeyPress(ushort scanCode, int holdMs = 50)
|
|
{
|
|
if (_humanizer is not null)
|
|
{
|
|
if (_humanizer.ShouldThrottle()) return;
|
|
holdMs = _humanizer.GaussianDelay(holdMs);
|
|
_humanizer.RecordAction();
|
|
}
|
|
_keyboard?.SimulateKeyPress((KeyCode)scanCode, holdMs);
|
|
}
|
|
|
|
public void MouseMoveTo(int x, int y)
|
|
{
|
|
_mouse?.SetCursorPosition(x, y, false);
|
|
}
|
|
|
|
public void MouseMoveBy(int dx, int dy)
|
|
{
|
|
_mouse?.MoveCursorBy(dx, dy, false);
|
|
}
|
|
|
|
public void LeftClick(int x, int y)
|
|
{
|
|
if (_humanizer is not null)
|
|
{
|
|
if (_humanizer.ShouldThrottle()) return;
|
|
(x, y) = _humanizer.JitterPosition(x, y);
|
|
Thread.Sleep(_humanizer.GaussianDelay(10));
|
|
_humanizer.RecordAction();
|
|
}
|
|
MouseMoveTo(x, y);
|
|
Thread.Sleep(_humanizer is not null ? _humanizer.GaussianDelay(10) : 10);
|
|
_mouse?.SimulateLeftButtonClick(_humanizer?.GaussianDelay(50) ?? 50);
|
|
}
|
|
|
|
public void RightClick(int x, int y)
|
|
{
|
|
if (_humanizer is not null)
|
|
{
|
|
if (_humanizer.ShouldThrottle()) return;
|
|
(x, y) = _humanizer.JitterPosition(x, y);
|
|
Thread.Sleep(_humanizer.GaussianDelay(10));
|
|
_humanizer.RecordAction();
|
|
}
|
|
MouseMoveTo(x, y);
|
|
Thread.Sleep(_humanizer is not null ? _humanizer.GaussianDelay(10) : 10);
|
|
_mouse?.SimulateRightButtonClick(_humanizer?.GaussianDelay(50) ?? 50);
|
|
}
|
|
|
|
public void LeftDown()
|
|
{
|
|
_mouse?.SimulateLeftButtonDown();
|
|
}
|
|
|
|
public void LeftUp()
|
|
{
|
|
_mouse?.SimulateLeftButtonUp();
|
|
}
|
|
|
|
public void RightDown()
|
|
{
|
|
_mouse?.SimulateRightButtonDown();
|
|
}
|
|
|
|
public void RightUp()
|
|
{
|
|
_mouse?.SimulateRightButtonUp();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
_keyboard?.Dispose();
|
|
_mouse?.Dispose();
|
|
_keyboard = null;
|
|
_mouse = null;
|
|
}
|
|
}
|