The SendKeys function in Delphi

Source: Internet
Author: User

Friends who have used programming languages such as Visual Basic must be very familiar with their SendKeys functions. You can use this function to send a string to a target window (such as Notepad) or a control (such as an edit control), as if you were entering the string in the target window or control.

However, the Delphi does not provide us with such a function. In fact, Borland engineers have long provided a convenient way: in the Delphi5.0 Enterprise version of the installation disk \info\extras\sendkeys\ folder, there is a Sndkey32.pas file,

Simply copy the file to the Delphi installation directory under the \lib\ folder and use the SendKeys function after referencing the file in the project.

The two main functions are:

SendKeys (Keystring:pchar; Wait:boolean): Boolean;

AppActivate (Windowname:pchar): Boolean;

Usage is:

The SendKeys function sends a string to the window or control that currently has focus, keysering the contents of the string.

Wait indicates whether the window or control waiting to receive the string is finished processing and returned. Generally set to false.

For example: SendKeys (' ABCDEFG ', false);

It is worth mentioning that the SendKeys function supports sending special characters and combination characters, such as arrow keys, ALT, and CTRL key combinations.

When used, just use the specified prefix. + for shift,^ to ctrl,% for Alt.

For example:

' +monday ' means to send shift+m and Onday

' + (Monday) ' means to send Shift+monday

for invisible characters (such as arrow keys, F1~F11, carriage returns, and so on), you can enclose them in {}.

For example:

The '%{f4} ' is a combination of sending alt+f4 to the application.

For more detailed instructions, refer to the comments in the Sndkey32.pas file.

The role of the AppActivate function is to set a window as the current window. Simply pass the title of the window to the past.

If a window is successfully activated, the return value is true.

Example: After you click Send, the contents of the edit control above will be sent to the following edit control.

The program is simple, with just two edit controls and a button control.

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.