91 lines
2.3 KiB
C#
91 lines
2.3 KiB
C#
|
using System;
|
||
|
using System.Runtime.InteropServices;
|
||
|
using System.Threading;
|
||
|
|
||
|
namespace Debug;
|
||
|
public class zidonghua
|
||
|
{
|
||
|
[StructLayout(LayoutKind.Sequential)]
|
||
|
struct INPUT
|
||
|
{
|
||
|
public int type;
|
||
|
public InputUnion u;
|
||
|
}
|
||
|
|
||
|
[StructLayout(LayoutKind.Explicit)]
|
||
|
struct InputUnion
|
||
|
{
|
||
|
[FieldOffset(0)] public MOUSEINPUT mi;
|
||
|
[FieldOffset(0)] public KEYBDINPUT ki;
|
||
|
}
|
||
|
|
||
|
[StructLayout(LayoutKind.Sequential)]
|
||
|
struct MOUSEINPUT
|
||
|
{
|
||
|
public int dx;
|
||
|
public int dy;
|
||
|
public int mouseData;
|
||
|
public int dwFlags;
|
||
|
public int time;
|
||
|
public IntPtr dwExtraInfo;
|
||
|
}
|
||
|
|
||
|
[StructLayout(LayoutKind.Sequential)]
|
||
|
struct KEYBDINPUT
|
||
|
{
|
||
|
public short wVk;
|
||
|
public short wScan;
|
||
|
public int dwFlags;
|
||
|
public int time;
|
||
|
public IntPtr dwExtraInfo;
|
||
|
}
|
||
|
|
||
|
[DllImport("user32.dll", SetLastError = true)]
|
||
|
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
|
||
|
|
||
|
const int INPUT_MOUSE = 0;
|
||
|
const int INPUT_KEYBOARD = 1;
|
||
|
const int KEYEVENTF_KEYUP = 0x0002;
|
||
|
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
|
||
|
const int MOUSEEVENTF_LEFTUP = 0x0004;
|
||
|
|
||
|
static void SimulateKeyPress(short keyCode)
|
||
|
{
|
||
|
INPUT[] inputs = new INPUT[2];
|
||
|
inputs[0].type = INPUT_KEYBOARD;
|
||
|
inputs[0].u.ki.wVk = keyCode;
|
||
|
inputs[0].u.ki.dwFlags = 0;
|
||
|
|
||
|
inputs[1].type = INPUT_KEYBOARD;
|
||
|
inputs[1].u.ki.wVk = keyCode;
|
||
|
inputs[1].u.ki.dwFlags = KEYEVENTF_KEYUP;
|
||
|
|
||
|
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
|
||
|
}
|
||
|
|
||
|
static void SimulateMouseClick(int x, int y)
|
||
|
{
|
||
|
INPUT[] inputs = new INPUT[2];
|
||
|
inputs[0].type = INPUT_MOUSE;
|
||
|
inputs[0].u.mi.dx = x;
|
||
|
inputs[0].u.mi.dy = y;
|
||
|
inputs[0].u.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
|
||
|
|
||
|
inputs[1].type = INPUT_MOUSE;
|
||
|
inputs[1].u.mi.dx = x;
|
||
|
inputs[1].u.mi.dy = y;
|
||
|
inputs[1].u.mi.dwFlags = MOUSEEVENTF_LEFTUP;
|
||
|
|
||
|
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
|
||
|
}
|
||
|
[DllImport("user32.dll")]
|
||
|
static extern short VkKeyScan(char ch);
|
||
|
public zidonghua()
|
||
|
{
|
||
|
Thread.Sleep(3000);
|
||
|
SimulateKeyPress(VkKeyScan('A'));
|
||
|
Thread.Sleep(1000);
|
||
|
SimulateMouseClick(500, 500);
|
||
|
}
|
||
|
}
|