Delphi TSendKeys FindWindowEx

Posted on 2007-10-04
Last Modified: 2010-05-18
I am using a TSendKeys object to send create a virtual user to do data entry.  For most windows this works fine - we set the handle to the main window and then run our functions.  

However, I have a case where I need to touch a child window in another application.  I can get the handle and set the focus on the main window - but it has a separate meaning for Alt-V than the child window within the app.  I tried using FindWindowEx but I don't think I am passing the right parameters.  

Here is my code so far - but it keeps returning chandle = o

      handle := SendKeysMacro.HandleFromTitle(FindWindowTitle(FMacro.ValueFromIndex[nLine -1]));
      chandle := FindWindowEx(handle, 0, PChar('WINDOW'), nil);//PChar(FMacro.ValueFromIndex[nLine]));

I think the third parameter is my issue - but I don't know - I'm not sure what lpszClass this is supposed to be and I can't seem to find a listing of options for this field either.  Most of the other questions I have seen related to this want to press a button in another app or something, not set the focus on a child window.

Any help is appreciated!

Question by:Chipmunk77
    LVL 17

    Accepted Solution

    if you know the relative location of te child window, you could try using
    childhandle := Childwindowfrompoint(mainhandle,point(50,50));
    LVL 16

    Assisted Solution

    Or use the application and see what keys need to be pressed to set the focus to the child window.
    For example if tab key sends the focus to the desired child window then send a tab key to the main window !
    This will enable the child window.
    Sending mouse keys could be risky if the form remembers the last position.

    To send keys to another application do this..

    Example :
    procedure TForm1.Button1Click(Sender: TObject);
     hnp:= FindWindow('notepad',nil);
     if hnp>0 then begin
      SendMessage(hnp, WM_CHAR, Ord('H'),0); // Send 'H' letter.
      SendMessage(hnp, WM_CHAR, Ord('i'),0); // Send 'i' letter.
      SendMessage(hnp, WM_CHAR, Ord('!'),0); // Send '!' letter.
      PostMessage(hnp, WM_KEYDOWN, VK_RETURN,0); // Send ENTER key.

    Or since the desired window is focused (in our case the main window not the child)
    You can send keys with keybd_Event
    keybd_Event(ord('X'), 0, 0, 0);  {X key down}
    keybd_Event(ord('X'), 0, KEYEVENTF_KEYUP, 0);   {X  key up}
    To see a list of all the virtual keys codes (use with keybd_event), take a look at this :

    Hope this helps :)
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 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

    22 Experts available now in Live!

    Get 1:1 Help Now