How To Emulate A Mouse

Table of contents:

How To Emulate A Mouse
How To Emulate A Mouse

Video: How To Emulate A Mouse

Video: How To Emulate A Mouse
Video: How to EMULATE STM32F103 as a MOUSE || CubeIDE || USB HID DEVICE 2024, November
Anonim

A manipulator of the "mouse" type is a necessary attribute of any computer. Without it, it is impossible to imagine control or navigation through applications. This manipulator actively interacts with the user by displaying his actions (movements, clicks, scrolling) on the screen. For some programming tasks, software mouse emulation may be required.

How to emulate a mouse
How to emulate a mouse

It is necessary

Microsoft Visual C ++. Net development environment (not lower than 2003)

Instructions

Step 1

Install Microsoft Visual C ++. Net (2003 or later). There are several versions of this development environment - paid and free. They can be downloaded from the developer's website. After installation, create a new project and connect System. Windows. Forms and System. Drawing as shown below: using System. Windows. Forms; using System. Drawing;

Step 2

To move the mouse around the screen in Windows operating systems, use functions that directly set the position of the cursor on the screen. Use, for example, the following code: Cursor. Position = new Point (x, y); This line will move the cursor to the position specified in the constructor of the Point (x, y) class (where x and y are the coordinates of the position where the cursor should be placed) … If you need to set the cursor position repeatedly, create one instance of the Point class and use the method for changing coordinates for it. This will save memory: Point point = new Point (0, 0); Cursor. Position = point. Offset (20, 100); Cursor. Position = point. Offset (40, -20); This code creates an object of the Point () class with coordinates 0, 0. The second line displaces the Point by 20 pixels in X and 100 pixels in Y. The current coordinate pointed to by the point object is 20, 100. In the third line, it happens again Offset Point by the specified number of pixels (40 and -20, respectively). The current coordinate is 60 (20 + 40) in X and 80 (100-20) in Y.

Step 3

Use the Win32 SendInput () or mouse_event () functions to emulate a mouse click. For example, to programmatically simulate a right click, use the following code: // import mouse_event (): [DllImport ("User32.dll")] static extern void mouse_event (MouseFlags dwFlags, int dx, int dy, int dwData, UIntPtr dwExtraInfo); // for ease of use, create an enumeration with the necessary constants (flags) // that define mouse actions: [Flags] enum MouseFlags {Move = 0x0001, LeftDown = 0x0002, LeftUp = 0x0004, RightDown = 0x0008, RightUp = 0x0010, Absolute = 0x8000 }; // use - click on the coordinates set below: const int x = 39000; // coordinates by Xconst int y = 12000; // coordinates by Ymouse_event (MouseFlags. Absolute | MouseFlags. Move, x, y, 0, UIntPtr. Zero); mouse_event (MouseFlags. Absolute | MouseFlags. RightDown, x, y, 0, UIntPtr. Zero); mouse_event (MouseFlags. Absolute | MouseFlags. RightUp, x, y, 0, UIntPtr. Zero);

Step 4

Emulate a mouse click by sending WM_LBUTTONDOWN and WM_LBUTTONUP messages using the Win API SendMessage () function. For example, as shown below: void OnBtPerformClick (object sender, EventArgs e) {SendMessage (btDemo. Handle, Messages. WM_LBUTTONDOWN, MK_LBUTTON, IntPtr. Zero); SendMessage (btDemo. Handle, Messages. WM_LBUTTONr.up, MKPTONE; }

Recommended: