Mega Code Archive

 
Categories / Delphi / Examples
 

How to send keystrokes to another application

Title: How to send keystrokes to another application Question: How can i send keystrokes to any running application from within Delphi. Answer: Download the following component: Name: Sendkeys Download from: http://delphi.icm.edu.pl/ftp/d20free/sendkeys.zip Description: This component takes a (client)window handle and makes it possible to send any keystroke(s) to the component or window specified by that handle. --------------------------------- Example: After installing the component discribed above creat a new application and place a button and the SendKeys component on the mainform. Write the followin code in the OnClick eventhandler for the button: procedure TForm1.Button1Click(Sender: TObject); begin // Start Notepad, so we can send keystrokes to it WinExec('NotePad.exe', SW_SHOW); // Replace the parameter of the GetWindowHandle procedure // with the text in the captionbar of your Notepad window. // This Codesample is used with the Dutch language version SendKeys1.GetWindowHandle('Naamloos - Kladblok'); // If the windowhandle is specified send some tekst to it if SendKeys1.WindowHandle 0 then SendKeys1.SendKeys('This is a test'); // Note: You can also send other keys like // a the RETURN key by using the following code: // SendKeys1.SendKeys(Chr(13)); end; --------------------------------- Have Fun! I'm so sorry, if someone had the idea that I wrote that component, he/she is wrong. The credits are for Mr. Pedro Gardete. He can be reached by E-Mail at gardete@mail.telepac.pt