1.system.windows.forms.sendkeys
Key combinations: Ctrl = ^, Shift = +, Alt =%
Analog keys: A
private void button1_Click(object sender, EventArgs e) { textBox1.Focus(); SendKeys.Send("{A}"); }
Analog key combinations: CTRL + A
private void button1_Click(object sender, EventArgs e) { webBrowser1.Focus(); SendKeys.Send("^{A}"); }
Sendkeys.send//asynchronous analog keys (without blocking UI)
sendkeys.sendwait//Sync analog button (will block UI until the other party finishes processing the message and returns)
This applies to the WinForm program, which is not applicable in the console program and in the WPF program
2.keybd_event
DLL reference
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)] public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
Analog keys: A
private void button1_Click(object sender, EventArgs e) { textBox1.Focus(); keybd_event(Keys.A, 0, 0, 0); }
Analog key combinations: CTRL + A
public const int KEYEVENTF_KEYUP = 2; private void button1_Click(object sender, EventArgs e) { webBrowser1.Focus(); keybd_event(Keys.ControlKey, 0, 0, 0); keybd_event(Keys.A, 0, 0, 0); keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0); }
3.PostMessage
Both of these are global scopes, and now we'll show you how to simulate a single window.
Analog keys: A/two times
[DllImport ( "user32.dll", EntryPoint = "Postmessagea", SetLastError = true)] public static extern int postmessage (IntPtr hWnd, int Msg, Keys wParam, int LParam); public const int WM_CHAR = 256; private void button1_Click (object sender, EventArgs e) {textbox1.focus (); PostMessage (Textbox1.handle, 256, KEYS.A, 2);}
Analog key combinations: CTRL + A
如下方式可能会失效,所以最好采用上述两种方式
1
Publicconst int wm_keydown = 256; public const int WM_KEYUP = 257; private void button1_Click (object sender, EventArgs e) {webbrowser1.focus (); keybd_event (Keys.controlkey, 0, 0, 0); keybd_event (KEYS.A, 0, 0, 0); PostMessage (Webbrowser1.handle, WM_KEYDOWN, KEYS.A, 0); keybd_event (Keys.controlkey, Span class= "Hljs-number" >0, Keyeventf_keyup, 0); }
C # Three ways to implement analog keyboard keys