Original
1. Simulate keyboard events
System.Windows.Forms.SendKeys
Here are some special keys for SendKeysCodeTable.
Key code
BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
Break {Break}
CAPS LOCK {CAPSLOCK}
Del or delete {delete} or {DEL}
Down arrow (lower key) {down}
End {END}
Enter {Enter} or ~
ESC {ESC}
Help {Help}
Home {Home}
INS or insert {Insert} or {INS}
Left arrow (right)
NUM LOCK {NumLock}
PAGE Down {PGDN}
PAGE up {PgUp}
PRINT screen {PRTSC} (reserved for future use)
Right arrow (Rightwards key) {R}
SCROLL LOCK {ScrollLock}
tab {TAB}
Up ARROW (upper arrow key) {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}
DigitalKeyboardPlus {ADD}
Numeric keypad minus {SUBTRACT}
Numeric keypad multiplication Sign {MULTIPLY}
DigitalKeyboard division sign {DIVIDE}
To specify the keys to use with any combination of the SHIFT, CTRL, and ALT keys, precede the key codes with one or more of the following code:
Key code
SHIFT + (shift= "+")
CTRL ^ (ctrl= "^") if the input
ALT%
private void Button1_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) {//Digital input This.ri Chtextbox1.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.FOCU S (); 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.f OCUs (); Sendkeys.send ("+{end}"); }
Look at the description of the method
Class SendKeys:System.Object
Members of the System.Windows.Forms
Summary:
Provides methods for sending keystrokes to an application.
void Send (System.String keys)
Members of the System.Windows.Forms.SendKeys
Summary:
Sends keystrokes to the active application.
void Sleep (System.TimeSpan timeout)
Members of the System.Threading.Thread
Summary:
Blocks the current thread for a specified time.
void Flush ( )
Members of the System.Windows.Forms.SendKeys
2. Mouse Simulation
Sometimes, we need to simulate the mouse movement in our program, click And so on. -for example, a macro that reproduces the user's actions, or a demo program that demonstrates how to do it. So, we're in. NET how to implement it?
. NET does not provide a function to change the position of the mouse pointer, simulate the click Operation, but the Windows API provides. One of them is:
[DllImport ("User32.dll")]
static extern bool Setcursorpos (int X, int Y);
This function can change the position of the mouse pointer. where x, y is the absolute position 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 can be set in relative coordinates. In addition, the function can also simulate the mouse button click, mouse wheel operation and so on. The Mouseeventflag is an enumeration based on the uint type, which is defined as follows:
[Flags]
Enum Mouseeventflag:uint
{
Move = 0x0001,
Leftdown = 0x0002,
Leftup = 0x0004,
Rightdown = 0x0008,
Rightup = 0x0010,
Middledown = 0x0020,
Middleup = 0x0040,
Xdown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
Virtualdesk = 0x4000,
Absolute = 0x8000
}
For a detailed description of these two functions, you can view the MSDN Library or the Windows Platform SDK documentation.
The following demo program (full version of source code, Vs.net 2005/c#) demonstrates how to use the above function to control the mouse move to the taskbar and click on the "Start" button.
(The program uses API functions such as FindWindowEx to find the taskbar and Start menu) Click here to download
Go C # simulates keyboard mouse events