Three ways to implement analog keyboard keys in C #

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.