C # simulate keyboard and mouse events

Source: Internet
Author: User

1. Simulate Keyboard Events
System. Windows. Forms. sendkeys
The following are some special key code tables of sendkeys.
Key code
Backspace {backspace}, {BS}, or {bksp}
Break {break}
Caps Lock {capslock}
Del or delete {Delete} or {del}
Down Arrow (down arrow) {down}
End {end}
Enter {enter} Or ~
ESC {ESC}
Help {help}
Home {home}
INS, insert {insert}, or {ins}
Left arrow (left arrow) {left}
Num Lock {numlock}
Page down {pgdn}
Page up {pgup}
Print Screen {prtsc} (reserved for future use)
Right arrow (right arrow) {right}
Scroll lock {scrolllock}
Tab {tab}
Up Arrow (up arrow) {up}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {f13}
F14 {F14}
F15 {F15}
F16 {F16}
Numeric keypad plus sign {Add}
Keyboard minus {subtract}
Numeric keypad multiplication number {multiply}
Keypad division {divide}

To specify keys that can be used with any combination of shift, Ctrl, and ALT keys, add one or more codes before these key codes:

Key code
Shift + (shift = "+ ")
CTRL ^ (CTRL = "^") if you enter
Alt %
Private void button#click (Object sender, system. eventargs E)

{// English Input
            this.richTextBox1.Focus();                                            
            for(int i=65;i<91;i++)
            {
                char Letter=(char)i;
                SendKeys.Send(Letter.ToString());
                System.Threading.Thread.Sleep(100);        
                SendKeys.Flush();
            }        
            for(int i=97;i<123;i++)
            {
                char Letter=(char)i;
                SendKeys.Send(Letter.ToString());
                System.Threading.Thread.Sleep(100);
                SendKeys.Flush();
            }        
        }
 
        private void button3_Click(object sender, System.EventArgs e)
{// Numeric Input
            this.richTextBox1.Focus();                                            
            for(int i=0;i<10;i++)
            {                
                SendKeys.Send(i.ToString());
                System.Threading.Thread.Sleep(100);        
                SendKeys.Flush();
            }                
        }
 
        private void button4_Click(object sender, System.EventArgs e)
        {//Backspace
            this.richTextBox1.Focus();
            SendKeys.Send("{Backspace}");        
        }
 
        private void button5_Click(object sender, System.EventArgs e)
        {//Home
            this.richTextBox1.Focus();
            SendKeys.Send("{Home}");                
        }
 
        private void button6_Click(object sender, System.EventArgs e)
        {//End
            this.richTextBox1.Focus();
            SendKeys.Send("{End}");        
        }
 
        private void button7_Click(object sender, System.EventArgs e)
        {//Enter
            this.richTextBox1.Focus();
            SendKeys.Send("{Enter}");        
        }
 
        private void button8_Click(object sender, System.EventArgs e)
        {//Delete
            this.richTextBox1.Focus();
            SendKeys.Send("{Delete}");        
        }
 
        private void button2_Click(object sender, System.EventArgs e)
        {//Shift+Home
            this.richTextBox1.Focus();
            SendKeys.Send("+{Home}");                
        }
 
        private void button9_Click(object sender, System.EventArgs e)
        {//Shift+End
            this.richTextBox1.Focus();
            SendKeys.Send("+{End}");                

}

 

See the description of the method.
public class SendKeys : System.Object
System. Windows. Forms Member
 
Abstract:
Provides the method to send a key-click to an application.

 

public static void Send ( System.String keys )
Member of system. Windows. Forms. sendkeys
 
Abstract:
Send a key to the activity application.

 

public static void Sleep ( System.TimeSpan timeout )
System. Threading. Thread Member
 
Abstract:

Blocks the current thread at the specified time.

 

public static void Flush (  )
Member of system. Windows. Forms. sendkeys
 
2. Simulate the mouse
Sometimes, we need to simulate mouse movement, clicks, and other actions in our program. -- For example, a macro that recreates user operations or a demo program that demonstrates operation methods. So how can we implement it in. Net?

. Net does not provide a function to change the mouse pointer position and simulate click operations. However, Windows API provides. One of them is:
[Dllimport ("user32.dll")]
Static extern bool setcursorpos (int x, int y );
This function can change the cursor position. X and Y are absolute relative to the upper left corner of the screen.
Another function is:

[Dllimport ("user32.dll")]
Static extern void mouse_event (mouseeventflag flags, int dx, int dy, uint data, uintptr extrainfo );
This function can not only set the absolute position of the mouse pointer, but also be set with relative coordinates. In addition, this function can simulate left and right mouse clicks, scroll wheel operations, and so on. The mouseeventflag is a uint-type enumeration, which is defined as follows:

[Flags]
Enum mouseeventflag: uint
{
Move = 0x0001,
Leftdown = 0x0002,
Leftup-to-date = 0x0004,
Rightdown = 0x0008,
Rightup = 0x0010,
Middledown = 0x0020,
Middleup = 0x0040,
Xdown = 0x0080,
X up = 0x0100,
Wheel = 0x0800,
Virtualdesk = 0X4000,
Absolute = 0x8000
}
For more information about the two functions, see the msdn library or Windows Platform SDK documentation.
The following demo program (full source code, vs. NET 2005/C #) demonstrates how to use the above function, move the mouse to the taskbar, and click the start button.
(This program uses findjavaswex and other API functions to find the taskbar and start menu)

Click here to download original address: http://www.cnblogs.com/eaglet/archive/2009/07/24/1529920.html

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.