Send simulated KeyStroke ??

How can i send a keystroke to another application in Delphi ?
I have hWnd.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sending keys to another app in 3 or 4 easy steps.

You have the hWnd of the window.

First, set focus to it using the SetForegroundWindow(hwnd); windows api call.

2) just in case, to give the system time to fully reset window focus, do a Application.ProcessMessages;

send keys using the keybd_event() windows api call, as for example:

keybd_event(<Key>,0,0,0); sends a keydown to the focused app.

keybd_event(<key>,0,KEYEVENTF_KEYUP,0); sends a keyup to the app.

Sometimes just sending the keydowns is enough, but to be sure, you should send both a keydown and a keyup, as in:


That sent an 'A' keystroke to the focused app.

Special keys can be sent by sending something like:


to sends a F7 key. The VK_ codes are defined in windows.pas;

4) when you're down sending keys, you may want to return focus back to your app with another SetForegroundWindow(Self.hwnd);

Good luck and regards.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brain2000Author Commented:
It works fine, thankyou !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.