How to send an "ENTER" key press to another application?

28,324

Solution 1

Try this

PostMessage(AppHandle, WM_KEYDOWN, VK_RETURN, 0);

Bye.

Solution 2

Quick answer. Duplicate your final SendMessage call using the WM_KEYUP event. Some systems only trigger when the key is released not when it is pushed.

Something to try until the real programmers respond. (:-)

Solution 3

There are a couple of different ways that you can do what you want.

The first and easiest to detail is the SendKey32.pas file. This unit was specifically written to allow you to do just what your asking. This delphi.about.com article gives a good explanation about the unit and what it can do.

I've personally started using a different method because of some limitations I found in the SendKey32 stuff. The second method is Journal Playback messages. This is a type of Windows System Hook that allows you to send (keystroke) messages to the O/S and have it deal with all of the little fiddly stuff.

There is a unit I found online called hkSend.pas that has all of the necessary plumbing all setup into a single function call (SendKeys).

If you do a Google for it and look at the first couple of entries you'll find a copy of the .pas file and you can see what's involved. The person who wrote hkSend appears to have made it work in a similar fashion to SendKey32.pas but using Journal Playback instead.

HTH,

Ryan.

Solution 4

Try #13#10 instead of just #13.

Share:
28,324
JosephStyons
Author by

JosephStyons

I started out as a professional developer using Delphi and Oracle in a Win32 client-server environment for a manufacturing company. I worked for five years in consulting, implementing solutions for dozens of clients and using many disparate technologies. Since then, I've worked for and with the non-profit industry, building applications that help them move their missions forward. My bread-and-butter is VB.NET and C# against a SQL Server back-end using a SOA architecture. But I can and will use whatever tool gets the job done, and I've had fun doing so with Angular, jQuery, ASP.NET, PHP, and even my own homemade frameworks to deliver solutions against that platform.

Updated on July 09, 2022

Comments

  • JosephStyons
    JosephStyons almost 2 years

    I have code that launches an external application and automatically fills in a password prompt.

    I want to automate the pressing of the "ENTER" key, so that the user does not have to click "OK".

    How can I send the ENTER key to an external application?

    Below is my code as it stands right now.

    The first line to post the password to the application works fine.

    The second line to send an ENTER key press has no effect at all.

    I am using Delphi 2010.

        //now that we have the control handle, send the password to it
        SendMessage(AppHandle,WM_SETTEXT,0,Integer(PChar(pwd)));
    
        //and now push ENTER
        SendMessage(AppHandle,WM_KEYDOWN,0,Integer(PChar(#13)));
    
  • JosephStyons
    JosephStyons over 14 years
    Thanks for the idea, but no difference. I also tried sending them sequentially, with no luck there either.
  • Ates Goral
    Ates Goral over 14 years
    It's fairly common for UI's to respond to mouse up events as opposed to mouse down. I'm not sure about key presses. But good suggestion anyway. +1
  • JosephStyons
    JosephStyons over 14 years
    Simple and effective. Thanks!