mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-03-30 10:19:51 +08:00
93 lines
2.7 KiB
C#
93 lines
2.7 KiB
C#
using System;
|
|
using System.Threading;
|
|
using Vanara.PInvoke;
|
|
|
|
namespace BetterGenshinImpact.Core.Simulator;
|
|
|
|
/// <summary>
|
|
/// 虚拟键代码
|
|
/// https://learn.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes
|
|
/// User32.VK.VK_SPACE 键盘空格键
|
|
/// </summary>
|
|
public class PostMessageSimulator
|
|
{
|
|
public static readonly uint WM_LBUTTONDOWN = 0x201; //按下鼠标左键
|
|
|
|
public static readonly uint WM_LBUTTONUP = 0x202; //释放鼠标左键
|
|
|
|
|
|
private readonly IntPtr _hWnd;
|
|
|
|
public PostMessageSimulator(IntPtr hWnd)
|
|
{
|
|
this._hWnd = hWnd;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 指定位置并按下左键
|
|
/// </summary>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
public PostMessageSimulator LeftButtonClick(int x, int y)
|
|
{
|
|
IntPtr p = (y << 16) | x;
|
|
User32.PostMessage(_hWnd, WM_LBUTTONDOWN, IntPtr.Zero, p);
|
|
Thread.Sleep(100);
|
|
User32.PostMessage(_hWnd, WM_LBUTTONUP, IntPtr.Zero, p);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 默认位置左键按下
|
|
/// </summary>
|
|
public PostMessageSimulator LeftButtonDown()
|
|
{
|
|
User32.PostMessage(_hWnd, WM_LBUTTONDOWN, IntPtr.Zero);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 默认位置左键释放
|
|
/// </summary>
|
|
public PostMessageSimulator LeftButtonUp()
|
|
{
|
|
User32.PostMessage(_hWnd, WM_LBUTTONUP, IntPtr.Zero);
|
|
return this;
|
|
}
|
|
|
|
public PostMessageSimulator KeyPress(User32.VK vk)
|
|
{
|
|
//User32.PostMessage(_hWnd, User32.WindowMessage.WM_ACTIVATE, 1, 0);
|
|
User32.PostMessage(_hWnd, User32.WindowMessage.WM_KEYDOWN, (nint)vk, 0x1e0001);
|
|
User32.PostMessage(_hWnd, User32.WindowMessage.WM_CHAR, (nint)vk, 0x1e0001);
|
|
User32.PostMessage(_hWnd, User32.WindowMessage.WM_KEYUP, (nint)vk, (nint)0xc01e0001);
|
|
return this;
|
|
}
|
|
|
|
public PostMessageSimulator LongKeyPress(User32.VK vk)
|
|
{
|
|
User32.PostMessage(_hWnd, User32.WindowMessage.WM_KEYDOWN, (nint)vk, 0x1e0001);
|
|
Thread.Sleep(1000);
|
|
User32.PostMessage(_hWnd, User32.WindowMessage.WM_CHAR, (nint)vk, 0x1e0001);
|
|
User32.PostMessage(_hWnd, User32.WindowMessage.WM_KEYUP, (nint)vk, (nint)0xc01e0001);
|
|
return this;
|
|
}
|
|
|
|
public PostMessageSimulator KeyDown(User32.VK vk)
|
|
{
|
|
User32.PostMessage(_hWnd, User32.WindowMessage.WM_KEYDOWN, (nint)vk, 0x1e0001);
|
|
return this;
|
|
}
|
|
|
|
public PostMessageSimulator KeyUp(User32.VK vk)
|
|
{
|
|
User32.PostMessage(_hWnd, User32.WindowMessage.WM_KEYUP, (nint)vk, (nint)0xc01e0001);
|
|
return this;
|
|
}
|
|
|
|
public PostMessageSimulator Sleep(int ms)
|
|
{
|
|
Thread.Sleep(ms);
|
|
return this;
|
|
}
|
|
} |