Simulate keyboard and mouse buttons with applications

Source: Internet
Author: User

This article supporting source code

The Windows interface program is popular with many users today. The operation of these programs is only two kinds, keyboard input control and mouse input control. Sometimes, for complex, or repetitive input operations, we can be programmed to replace the manual input, and the program to simulate the keyboard and mouse input? The answer is yes. This is done primarily through the two Windows API functions. Let's take VC + + as an example to introduce how to achieve these two functions.

Analog keyboard We use the Keybd_event API function to simulate the mouse button with the Mouse_event function. It is simple and convenient to call API functions in VC.

First, introduce the keybd_event function. Keybd_event can trigger a keystroke event, which means that a wm_keydown or WM_KEYUP message is generated back. Of course, you can also use to generate these two messages to simulate the key, but not directly with this function convenient. Keybd_event has four parameters, the first is the key value of the virtual key, such as the return key to Vk_return,tab for Vk_tab. The second parameter is scan code, generally do not need to set, with 0 instead on the line the third parameter is the option flag, if the KeyDown 0 can be, if the KEYUP is set to "Keyeventf_keyup", the fourth parameter is generally 0 can be. The following code can be implemented to simulate press the key, which XX represents the virtual key value of XX key, here is the key corresponding key code, such as ' A ' =65

keybd_event (65,0,0,0);

keybd_event (65,0,keyeventf_keyup,0); ...

Mouse_event preferably with the Setcursorpos (x,y) function, similar to keybd_event, Mouse_event has five parameters, the first is the option flag, for Mouseeventf_ When Leftdown, the left key is pressed as Mouseeventf_leftup to release the left key and send the corresponding message to the system. The 23rd parameter represents x,y, respectively.

The relative position, generally can be set to 0, 0, the 45th parameter is not important, generally can also be set to 0, 0. To get a more detailed use of the keybd_event and mouse_event functions, you can consult MSDN or Delphi Help.

The following is a sample code about Mouse_event:

Point Lppoint;

GetCursorPos (&lppoint);

Setcursorpos (Lppoint.x, LPPOINT.Y);

Mouse_event (mouseeventf_leftdown,0,0,0,0);

Mouse_event (mouseeventf_leftup,0,0,0,0);

The above code represents a double-click of the mouse, to indicate a click, with two mouse_event (one down, one release).

Note that, whether the analog keyboard or mouse events, you should pay attention to restore, that is, press the key to release, a KeyDown corresponding to a keyup; mouse click Finish also want to loosen, otherwise may affect the function of the program.

Well, hopefully this article will give you a rudimentary understanding of the analog keyboard and mouse buttons, and if you want to get a better understanding of the MSDN Online Help, and do a lot of hands-on work, it might help you a lot.

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.