Simulate keyboard and mouse operations

Source: Internet
Author: User

Use the code to simulate keyboard and mouse operations. The keybd_event/mouse_event or sendinput API functions are mainly used. The usage example is as follows (with comments ):

/*
* Simulate keyboard input. Press the window key (win key) as an example.
*/

// Keybd_event analog keyboard input example
: Keybd_event (vk_lwin, 0, 0, 0); // press the win key
: Keybd_event (vk_lwin, 0, keyeventf_keyup, 0); // The win key is displayed.

// Sendinput analog keyboard input example
Input keyinput [2];
Memset (keyinput, 0, sizeof (keyinput ));
Keyinput [0]. type = keyinput [1]. type = input_keyboard; // set it to keyboard input
Keyinput [0]. Ki. wvk = keyinput [1]. Ki. wvk = vk_lwin; // operate the win key
Keyinput [1]. Ki. dwflags = keyeventf_keyup; //
: Sendinput (_ countof (keyinput), keyinput, sizeof (input ));

/*
* Simulate the mouse input. Take the Start menu as an example.
* Because my machine resolution is 1440*900, the mouse position (0,880) is just at the Start Menu position.
*/

// Example of mouse input simulation using mouse_event
: Setcursorpos (0,880); // set the current mouse position to the Start Menu position
: Mouse_event (mouseeventf_absolute | mouseeventf_leftdown, 0, 0, 0, null); // press the simulated left button.
: Mouse_event (mouseeventf_absolute | mouseeventf_leftup, 0, 0, 0, null); // simulate left-click pop-up

// Example of sendinput simulating mouse input
: Setcursorpos (0,880); // set the current mouse position to the Start Menu position
Input mouseinput [2];
Memset (mouseinput, 0, sizeof (mouseinput ));
Mouseinput [0]. type = mouseinput [1]. type = input_mouse; // set it to mouse input
Mouseinput [0]. Mi. dwflags = mouseeventf_absolute | mouseeventf_leftdown; // press the left button
Mouseinput [1]. Mi. dwflags = mouseeventf_absolute | mouseeventf_leftup; // The left button pops up.
: Sendinput (_ countof (mouseinput), mouseinput, sizeof (input ));

 

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.