C # simulate mouse. Simulate Keyboard 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 (1, 100); sendkeys. flush () ;}for (INT I = 97; I <123; I ++) {char letter = (char) I; sendkeys. send (letter. tostring (); system. threading. thread. sleep (1, 100); sendkeys. flush () ;}} private void button3_click (Object sender, system. eventargs e) {// enter this. richtextbox1.focus (); For (INT I = 0; I <10; I ++) {sendkeys. send (I. tostring (); system. threading. thread. sleep (1, 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 }");}

Take a look at the description of the method. Public class sendkeys: system. Object System. Windows. Forms member Summary: provides a method to send a key-click to the application. Public static void send (system. String keys) member Summary of system. Windows. Forms. sendkeys: Send a key to an active application. Public static void sleep (system. timespan timeout) system. Threading. Thread member Summary: blocks the current thread for the specified time.

Public static void flush () system. Windows. Forms. sendkeys Member 2. Simulate the mouse sometimes, we need to simulate the mouse movement, click 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 = 0x0004, rightdown = 0x0008, rightup = 0x0010, middledown = 0x0020, middleup = 0x0040, xdown = 0x0080, xup = 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. (The program uses findjavaswex and other API functions to find the taskbar and start menu) Click here to download the original address: http://www.cnblogs.com/eaglet/archive/2009/07/24/1529920.html

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fanweiwei/archive/2009/07/24/4376967.aspx

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.