C #/winform automatically triggers mouse and keyboard events,
To trigger mouse and keyboard events in a C # program, you must call windows functions.
I. Trigger of mouse events
1. reference the windows function mouse_event
/// <Summary> /// mouse event /// </summary> /// <param name = "flags"> event type </param> /// <param name = "dx"> x coordinate value (0 ~ 65535) </param> // <param name = "dy"> y coordinate value (0 ~ 65535) </param> // <param name = "data"> scroll value (120 in one unit) </param> // <param name = "extraInfo"> not supported </param> [DllImport ("user32.dll")] static extern void mouse_event (MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo );
MouseEventFlag indicates the type of the mouse event. Multiple enumerated values can be combined. Note that the dx and dy parameters must be absolute coordinates (0, 0 )~ (65535,65535.
/// <Summary> /// set of mouse operation flag /// </summary> [Flags] enum MouseEventFlag: uint {// <summary> /// Move the mouse event // </summary> Move = 0x0001, /// <summary> /// click the left mouse button to press the event /// </summary> LeftDown = 0x0002, LeftUp = 0x0004, RightDown = 0x0008, rightUp = 0x0010, MiddleDown = 0x0020, MiddleUp = 0x0040, XDown = 0x0080, XUp = 0x0100, Wheel = 0x0800, virtualDesk = 0x4000, /// <summary> // you can specify the absolute position (dx, dy) for the mouse coordinates ), otherwise, it is the relative location triggered by the last event. // </summary> Absolute = 0x8000}
2. Call the mouse_event function to trigger a mouse event.
/// <Summary> /// trigger the mouse event /// </summary> /// <param name = "x"> </param> /// <param name = "y"> </param> private static void DoMouseClick (int x, int y) {int dx = (int) (double) x/Screen. primaryScreen. bounds. width * 65535); // screen resolution ing to 0 ~ Int dy = (int) (double) y/Screen between 65535 (0 xffff, that is, 16 bits. primaryScreen. bounds. height * 0 xffff); // convert to double type. Otherwise, the values are 0 and 1 mouse_event (MouseEventFlag. move | MouseEventFlag. leftDown | MouseEventFlag. leftUp | MouseEventFlag. absolute, dx, dy, 0, new UIntPtr (0); // click}
Ii. Triggering of Keyboard Events
1. reference the windows function keybd_event
/// <Summary> // keyboard event // </summary> /// <param name = "bVk"> virtual-key code </param> /// <param name = "bScan"> hardware scan code </param> // <param name = "dwFlags"> flags specifying various function options </param> /// <param name = "dwExtraInfo"> additional data associated with keystroke </param> [DllImport ("user32.dll")] public static extern void keybd_event (byte bVk, byte bScan, int dwFlags, int dwExtraInfo );
Bvk is the key value. For example, press ENTER 13, bScan is set to 0, dwFlags is set to 0, and press 2 to raise; dwExtraInfo is also set to 0.
2. Call the keybd_event function to trigger a keyboard event.
The following code triggers the Ctrl + C key combination to perform the copy operation.
keybd_event((byte)Keys.ControlKey, 0, 0, 0); keybd_event((byte)Keys.C, 0, 0, 0); keybd_event((byte)Keys.ControlKey, 0, 2, 0); keybd_event((byte)Keys.C, 0, 2, 0);
Symbol in C Language <Yes
Left shift operator (<)
Removes all the binary bits of an operation object from the left and adds 0 to the right ).
For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,
Move 1 to the left and then a = a * 2;
If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)
Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.
The operand shifts one digit to the right, which is equivalent to dividing the number by 2.
For example, a = a> 2 shifts the binary bit of a two places to the right,
0 or 1 to see whether the number is positive or negative.
Symbol in C Language <Yes
Left shift operator (<)
Removes all the binary bits of an operation object from the left and adds 0 to the right ).
For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,
Move 1 to the left and then a = a * 2;
If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)
Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.
The operand shifts one digit to the right, which is equivalent to dividing the number by 2.
For example, a = a> 2 shifts the binary bit of a two places to the right,
0 or 1 to see whether the number is positive or negative.