Simulate the keyboard and mouse keyboard with a program

Source: Internet
Author: User
Today, Windows has become popular among many users. There are two types of operations on these programs: keyboard input control and mouse input control. Sometimes, for complicated or repetitive operations, can we compile a program instead of manual input, and use a program to simulate keyboard and mouse input? The answer is yes. This is mainly implemented through two API functions.

The following uses Delphi as an example to describe how to implement these two functions. We use the keybd_event API function to simulate the keyboard, and use the mouse_event function to simulate mouse buttons. You don't have to worry about it. It is very convenient to call API functions in Delphi.

First, we will introduce the keybd_event function. Keybd_event can trigger a key event, that is, a message wm_keydown or wm_keyup will be generated. Of course, the two messages can also be generated to simulate the buttons, but this function is not directly used for convenience. The keybd_event has four parameters. The first parameter is the virtual key value of the key. For example, the Enter key is vk_return and the tab key is vk_tab. The second parameter is the scan code. Generally, do not set it. Use 0 instead. The third parameter is the option flag. If it is keydown, set it to 0. If it is keyup, set it to "keyeventf_keyup". The fourth parameter is usually set to 0. Run the following code to simulate pressing the I key. $49 indicates the virtual key value of the I key:

Keybd_event ($49,0, 0, 0 );
Keybd_event ($49,0, keyeventf_keyup, 0 );...

  

It is best to use mouse_event with the setcursorpos (x, y) function. Similar to keybd_event, mouse_event has five parameters. The first one is the option flag. If it is mouseeventf_leftdown, it indicates that the left button is pressed, for mouseeventf_leftup, the left-click release is used to send messages to the system. The second three parameters indicate the relative positions of X and Y, which can be set to 0, 0, and the fourth Five parameters are not important. They can also be set to 0, 0. For more detailed usage of the keybd_event and mouse_event functions, refer to the msdn or Delphi help. The following is the sample code about mouse_event:
 

Setcursorpos (20,132 );
Mouse_event (mouseeventf_leftdown, 0, 0, 0 );
Mouse_event (mouseeventf_leftup, 0, 0, 0 );
Mouse_event (mouseeventf_leftdown, 0, 0, 0 );
Mouse_event (mouseeventf_leftup, 0, 0, 0 );...


The code above indicates double-click of the mouse. to double-click the mouse, use two mouse_events (put down once and release once ).
Note: whether it is a simulated keyboard or a mouse event, you must pay attention to restoration, that is, press the key to release, and a keydown corresponds to a keyup; after the mouse is clicked, you must release it, otherwise, the function of the program may be affected.
 
Well, I hope this article will give you a preliminary understanding of the simulated keyboard and mouse buttons. If you want to learn more about the mysteries, you can refer to the detailed msdn online help, and more hands-on practices.

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.