1.System.Windows.Forms.SendKeys
组合键:Ctrl = ^ 、Shift = + 、Alt = %
模拟按键:A
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
SendKeys.Send("{A}");
}
模拟组合键:CTRL + A
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Focus();
SendKeys.Send("^{A}");
}
SendKeys.Send // 异步模拟按键(不阻塞UI)
SendKeys.SendWait // 同步模拟按键(会阻塞UI直到对方处理完消息后返回)
//这种方式适用于WinForm程序,在Console程序以及WPF程序中不适用
2.keybd_event
DLL引用
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
模拟按键:A
private void button1_Click(object sender, EventArgs e