Important alert: (current site time 7/16/2013 5:55:41 AM EDT)
 

winzip icon

Send Keystrokes to Applications Using Windows' API SendInput

Email
Submitted on: 1/15/2010 9:13:10 AM
By: Ambriel Angel  
Level: Advanced
User Rating: By 7 Users
Compatibility: Delphi 7, Delphi 6, Delphi 5, Delphi 4
Views: 56464
author picture
(About the author)
 
     Send virtual keystrokes using the Windows API SendInput. (SendKey, SendKeys, Keyboard Output.) Allows you to send keys to the application in focus.
 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/14/2005 8:42:42 AMmark gascoyne

very good i always used to use the keyboard event function :)
(If this comment was disrespectful, please report it.)

 
3/14/2005 9:30:03 PM

I'm newer about delphi, I have put a button on the form1, and a input editbox name edit1,now,who can tell me ,how can use this unit to send string "abc" to the edit1?

procedure TForm1.Button1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
edit1.SetFocus ;
Pressascii(65,10);
PressAscii(66,10);
PressAscii(67,10);
end;
(If this comment was disrespectful, please report it.)

 
4/22/2006 4:00:47 AMAmbriel

This is a late reply, but hopefully it will help somebody.

You may find that the TypeText function is efficient if you are simply trying to send a string of basic text to the object in focus.

//Example use of the TypeText procedure

procedure TForm1.Button1Click(Sender: TObject);
begin
// Prepare fields and buttons
Edit1.Text := '';
Edit2.Text := '';
Edit3.Text := '';

Edit1.SetFocus;

// <--- Unit comes with Wait function. Added for effect.
// 1/10 second is here
Wait(10);

// <--- Demo of TypeText here. Use 0 to disable time. 1 is fastest typing.
TypeText('Excuse me! -- in a rush!',1);

// wait one second so you don't miss anything (for effect only).
Wait(100);

Edit2.SetFocus;

//<--- Slow typing at 2/10 (200/1000) seconds
TypeText('But I wan--',20);

Wait(100); // one second

Edit3.SetFocus;
TypeText('Listen, who has the time?!',5);

end;
(If this comment was disrespectful, please report it.)

 
3/17/2007 1:35:52 PMGasper

The best code ever!!!! I have use it for my graphic virtual keyboard. I have it configured into slovenian language.

I have not solved one just one problem. I cant call the following signs: "[ ] { } @" can any one help me with these?
ps: this "@" sign has following keys "ALT GR + V" (For slovenian keyboard)

Keep on good working:)

Gasper Juhart
(If this comment was disrespectful, please report it.)

 
5/22/2007 1:08:26 PMnewbie

i got a problem: when i`m add this to my project and try to compile i recive error: MMDebug.dcu not found, What is this? where i can find this file? im using Delphi7
(If this comment was disrespectful, please report it.)

 
6/16/2007 8:23:12 PMAmbriel

Yes, remove the "USES" for MMDebug
It shouldn't be there, I will fix that.
(If this comment was disrespectful, please report it.)

 
3/27/2009 8:47:55 AMSanket Shah

Hi...!
I have two different programs, from first exe i am passing the sendmessege to another then second exe will open the IE and after opening that IE Window, I am trying to fire keystroke on it, but sometimes it missed, i mean when i am going to pass the keystroke "ABCDEF" 10 time it will work fine, but when I am going to run that command more then 100 time it will skip some character, i mean it will fire keystroke like "ABCEF", not any fixed key stroke, it skips randomly. and one another thing when i am opening the same internet explorer windows from the first exe it will fire the entire keystroke perfectly. this happens only when i am trying to open that Internet explorer with second exe and it happens only with internext explorer window.

I am always be online on my following Ids. for all working days.

yahoo: snkt_shah@yahoo.com
gmail: snkt.shah@gmail.com
MSN: snkt_shah@hotmail.com

Thanks & Regards,
Sanket Shah
(If this comment was disrespectful, please report it.)

 
4/10/2010 4:14:27 AMAmbriel Angel

@Sanket You've probably already resolved it by now. I apologize it took me so long to see this posting here. This account was linked to an email I hardly use.

- Your output may be too fast for your computer or IE, trying slowing down the output between automated keys.

- Make sure you are programming to wait for the application to fully load before sending keystrokes.

My guess is that the issue lays in the speed of the keystrokes and\or the readiness of the application you are sending the keys to.

For any future inquires regarding this code, please contact me at the ambrielnet.com website, where you will find the most current ways to reach me.

Thank you very much.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.