How To Emulate A Click

Table of contents:

How To Emulate A Click
How To Emulate A Click

Video: How To Emulate A Click

Video: How To Emulate A Click
Video: How to simulate a click with pure JavaScript? 2024, April
Anonim

Sometimes the script of a script or program requires imitation of some user actions - for example, pressing a key or clicking with the mouse on an element. This can be realized either by the built-in means of the programming language in which the program is written, or using a special interface of the operating system. Such an interface is intended for interaction of application programs with system programs and is called API - Application Programming Interfaces.

How to emulate a click
How to emulate a click

Instructions

Step 1

Find out if the language you are using to write the program or script has built-in keystroke emulation. For example, in JavaScript, the left mouse button is simulated using a method that is bound to specific elements of the user interface. For example, to emulate a left mouse button press while the cursor is over a button named autoClkButton placed in a form named autoClkForm, you need to use the document.autoClkButton.autoClkForm.click () construct. In this language, not only buttons (button, reset, submit) have a click () property, but also select elements - checkbox and radio.

Step 2

Use the external keybd_event function if the language you are using does not have the built-in tools that you need to do everything automatically. This is a Win32 API function, so in order to be able to access them from the program, you should place a block at the beginning of the code that imports the functions of the external library. This should be done in accordance with the syntax of the used software environment. For example, in the MQL (MetaQuotes Language) terminal programming language for stock trading, to call functions placed in the user32.dll system library, you should place the following lines at the beginning of the code: #import "user32.dll" bool keybd_event (int bVk, int bScan); # import After that, it will be possible to use the keybd_event function declared in the import block.

Step 3

Keybd_event has four parameters. The first (bVk, data type BYTE) can take one of 255 values and indicates the key that will be simulated when pressed. Find out which of these values is assigned to the key you need on this page - https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx. The second parameter (bScan, BYTE type) is the "scan code" that is generated when the selected key is pressed. The third (dwFlags, type DWORD) can take one or both of its provided values (KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP). The first indicates that an extended key code will be generated, and the second indicates that the button was pressed and then released. The fourth parameter (dwExtraInfo, type ULONG_PTR) can contain additional flags specific to each key.

Recommended: