C winform automatic trigger mouse keyboard events method

  • 2020-08-22 22:39:08
  • OfStack

Triggering mouse and keyboard events by program is a common function in C# programming. This paper shows the method of winform in C# to automatically trigger mouse and keyboard events, which has a good practical value. The details are as follows:

The windows function must be called to trigger mouse and keyboard events in the C# program.

1. Mouse event trigger

1. Reference the windows function mouse_event


/// <summary>
///  Mouse events 
/// </summary>
/// <param name="flags"> The event type </param>
/// <param name="dx">x Coordinate values (0~65535)</param>
/// <param name="dy">y Coordinate values (0~65535)</param>
/// <param name="data"> The scroll values (1201 A unit of )</param>
/// <param name="extraInfo"> Does not support </param>
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

MouseEventFlag represents the type of mouse event and can be combined with multiple enumerated values. Note that the dx,dy parameter, must be a point in the absolute coordinates (0,0) ~ (65535,65535).


/// <summary>
///  Mouse operation flag bit set 
/// </summary>
[Flags]
enum MouseEventFlag : uint
{
/// <summary>
///  Mouse movement event 
/// </summary>
Move = 0x0001,

/// <summary>
///  Left mouse button press event 
/// </summary>
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
/// <summary>
///  Set the mouse coordinate to the absolute position ( dx,dy ) , Otherwise, the distance is the last 1 The relative position of the secondary event trigger 
/// </summary>
Absolute = 0x8000
}

2. Call the mouse_event function to trigger the mouse event


/// <summary>
///  Trigger 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 mapped to 0~65535(0xffff, namely 16 position ) between 
  int dy = (int)((double)y / Screen.PrimaryScreen.Bounds.Height * 0xffff); // convert double Type operation, otherwise the value is 0 , 1
  mouse_event(MouseEventFlag.Move | MouseEventFlag.LeftDown | MouseEventFlag.LeftUp | MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); // Click on the 
}

2. Trigger of keyboard events

1. Reference windows function keybd_event


/// <summary>
///  Keyboard events 
/// </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, such as enter 13, bScan set to 0, dwFlags set to 0 means press, 2 means lift; dwExtraInfo is also set to 0.

2. Call the keybd_event function to trigger the keyboard event

The following code demonstrates that the Ctrl+C key combination is triggered to perform a 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);

It is hoped that the examples described in this article will be helpful to your C# programming.


Related articles: