SendKeys function in Delphi

zhaozj2021-02-16  101

Friends who have used Visual Basic's programming languages ​​must be very familiar with its SendKeys function. With this function, you can send a string to the target window (such as a notepad) or a control (such as an Edit control), as if it is the same as you enter this string in the target window or control. However, in Delphi, there is no such function for us. In fact, Borland's engineers have long provided a convenient way: in the InfoExTrassendKeys folder of the Delphi5.0 Enterprise Edition, there is a Sndkey32.PAS file, just copy the file to the Delphi installation directory LIB In the folder, you can use the sendKeys function after reference to this file. Two of the two major functions are: sendKeys (keyString: pchar; wait: boolean): boolean; appactivate (windowname: pchar): boolean; Usage: The SendKeys function sends a string to the currently focused window or control, KeySering is the content of the string. WAIT indicates whether the window or control of the receiving string or control is returned. Generally set to false. For example: SendKeys ('Abcdefg', False); It is worth mentioning that the SendKeys function supports sending special characters and combined characters, such as arrow keys, alt, and ctrl combinations. When using, just use the prescribed prefix to indicate. Is Shift, ^ is Ctrl,% is Alt. For example: ' monday' means sending Shift M and ONDAY ' (Monday)' means sending Shift Monday for invisible characters (such as direction keys, F1 ~ F11, Enter, etc.), available {} stand up. For example, '% {f4}' is sent to the application to the application. For more detail, please refer to the comments in the SNDKEY32.PAS file. The role of the appactivate function is to set a window to the current window. Simply pass the title of the window over. If a window is successfully activated, the return value is TRUE.

Example: After clicking the send, the content of the above EDIT control will be sent to the following EDIT control. The program is simple, as long as two Edit controls and a Button control. Source code as follows: unit sendkey1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, sndkey32, StdCtrls; // Note the row you want to add sndker32 !! type TForm1 = class (TForm) Edit1: Tedit; Edit2: Tedit; Button1: TButton; Procedure Button1Click (sender: TOBJECT); private {private declarations}}}}}}}} end; var form1: tform1;

Implementation {$ r * .dfm} procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT); begin edit2.setfocus; // Place the focus at Edit2 SendKeys (Pchar (Edit1.Text), False); // Put the string in Edit1 Send it to edit2 // to convert END in advance;

End.

Very simple, is it? In fact, not only EDIT controls, such as MEMO, Richedit, etc., can receive the transmitted characters. The above example is only transmitted in this application, the following example is to send the string to Microsoft Word. The controls used are not much different from the above example. Source as: unit unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, sndkey32, StdCtrls; // Do not forget sndKey32 type TForm1 = class (TForm) Button1: TButton; Edit1:! TEdit ; Button2: TButton; procedure Button1Click (Sender: TObject); procedure Button2Click (Sender: TObject); private {Private declarations} public {public declarations} end; var Form1: TForm1; implementation

{$ R * .dfm} {This function places the focus in Word, if it fails, return false} Function setfocuStoword: Boolean; var f: boolean; begin f: = true; if not appactivate ('Document 1 - Microsoft Word') The {string content is: document space 1 space - space Microsoft space word} begin f: = false; messagedlg ('did not find word!', mTerror, [mbok], 0); // display error End; result : = f; end; procedure SetFormActive; // set the focus back to the subroutine begin Appactivate ( 'SendToWord'); form1.SetFocus; form1.Edit1.SetFocus; end; procedure TForm1.Button1Click (Sender: TObject); begin if NOT setfocustoword dam; // Didn't find word, exiting SendKeys (pchar (form1.edit1.text), false); // found, send string setformactive; end;

Procedure tform1.button2click (sender: TOBJECT); Begin if not setfocuStoword kiln @ / 没 没 没 w w s;;;;;; 到 到 到 到 到 到 到 到 到;; 到;; 到;;;;;;;;; This example is just a simple demonstration. In fact, you can change the part of the activation of Word (this article finds 'document 1 - Microsoft Word' to achieve, it seems too simple.) Delphi itself has a series of controls with Office collaboration. But only simple collaboration, isn't it simpler? The SendKeys function is very powerful, otherwise Borland will not provide this file in the installation disc. Better use of SendKeys's ideas, you have to look at all friends!

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

New Post(0)