Use program to simulate keyboards and mouse keyboard

zhaozj2021-02-11  187

Today, Windows interface programs are welcomed by users today. The operations of these programs are not more than two, keyboard input control and mouse input control. Sometimes, for more complicated, or repeated operations, can we replace manual input by a programming program, and use the program to simulate the input of the keyboard and mouse? The answer is yes. This is mainly implemented by two API functions.

Here is Delphi as an example to introduce how to implement these two functions. Analog Keyboard We use keybd_event this API function, analog mouse button with a mouse_event function. Don't worry, it is very convenient to call the API function in Delphi. Let's introduce the keybd_event function. KeyBD_Event can trigger a button event, that is, a WM_KEYDOWN or WM_KEYUP message is generated back. Of course, you can also use the two messages to analog buttons, but there is no direct use of this function. KeyBD_Event has four parameters, the first one for the virtual key value of the button, such as the Enter key is VK_RETURN, the Tab key is VK_TAB. The second parameter is a scan code, which is generally not set, and use 0 to replace it. The third parameter is the option flag. If it is set to KeyDown, if Keyup is set to "Keyeventf_Keyup", the fourth parameter is typically 0. Use the following code to implement analog Press I key, where $ 49 represents the virtual key value of the I key:

KeyBD_Event ($ 49, 0, 0); KeyBD_Event ($ 49, 0, KeyEventf_Keyup, 0); ...

Mouse_Event is best to use with the setcursorpos (x, y) function, similar to keybd_event, mouse_event has five parameters, the first one is the option flag, indicating the left mouseeventf_leftdown, indicating the left mouseeventf_leftup, indicating the left button, to the system Send the corresponding message. The second three parameters represent x, y relative position, which is generally set to 0, 0, and the fourth five parameters are not important, and may also be set to 0,0. To get a more detailed usage of keybd_event and mouse_event functions, you can consult the MSDN or Delphi Help. Here is a sample code for mouse_event:

setcursorpos (20,132); mouse_event (MOUSEEVENTF_LEFTDOWN, 0,0,0,0); mouse_event (MOUSEEVENTF_LEFTUP, 0,0,0,0); mouse_event (MOUSEEVENTF_LEFTDOWN, 0,0,0,0); mouse_event (MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); ...

The above code indicates the double click of the mouse. To indicate, you can use two mouse_event (let go of it, loose once). Note, whether it is analog keyboard or a mouse event, pay attention to restore, that is, press the key to loosen, a keydown corresponds to a keyup; the mouse click Also release, otherwise it may affect the function of the program. Ok, I hope this article can have a preliminary understanding of the analog keyboard and mouse button. If you want more deeply understand the mystery, you can check the detailed MSDN online help, and do a lot of practice. If you don't understand the program, you can download the corresponding source program to Coolbaby.Delphibbs.com.

转载请注明原文地址:https://www.9cbs.com/read-3803.html

New Post(0)