send key press to other application in delphi

Posted on 2011-10-12
Last Modified: 2012-05-12
THe below code gets compiled, but it doesnt send the key press to the note pad. I have opened the note pad and kept as it is. can anyone help me..?

procedure TForm1.Button1Click(Sender: TObject);
  Window: hwnd;

  Window := FindWindowA('notepad', nil); // Finds notepad.
  if Window <> 0 then
    keybd_event(Ord('B'), 0, 0, 0);

Open in new window

Question by:jimsweb
    LVL 36

    Expert Comment

    by:Geert Gruwez
    you don't pass the window variable to the keybd_event
    so it can never work
    LVL 22

    Expert Comment

    by:Ferruccio Accalai
    You have to specify which keyboard_event  are you passing.
    Any keystroke is composed by a keydown and a Keyup

    So to send the B key you have to do

    keybd_event(VkKeyScan('B'), 1, 0, 0); //key down
    keybd_event(VkKeyScan('B'), 1, KEYEVENTF_KEYUP, 0); //key up

    LVL 19

    Expert Comment

    Try unit sndkey32...

    How to send keystrokes to another application (a better one)
    LVL 19

    Accepted Solution

    You can also use SendMessage(...)

    Send keys to another application
    LVL 8

    Author Closing Comment


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Conversion ? 15 84
    proper way to parse url in delphi 2 97
    delphi parse string to params 3 47
    Convert a string into a TDateTime 5 24
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now