SendKeys.Send/Wait only works first time!

Posted on 2006-04-10
Last Modified: 2012-05-05

I am using SendKeys.Send("hello world") to send a text string to an external application (eg. notepad) but it only works fine the first time but never again until you reset the controlling application.

I am "discovering" the handle for my target window, bringing it into foreground using unmanaged win32 calls which all seem fine.  I send my first string and it works great but after that it fails to send anything (that I can see).

I have this function on a button and each time I click it the target app. does indeed receive focus but no string is apparently sent.

I have used both the .Send and .SendWait methods but neither solves the problem.

Any thoughts?

Many thanks.

Question by:JAMES
    LVL 85

    Accepted Solution

    You may need a small delay after you set focus to your external app before sending your keystrokes to allow that window to fully receive the focus.  Otherwise your keys may be going somewhere else...


    Author Comment

    You're a star, that's done it.

    Thanks a million (or 500 anyway)....


    LVL 85

    Expert Comment

    by:Mike Tomlinson
    You'll have to play with the delay interval to find the ~best~ length...though it's not guaranteed to work everytime.

    You could make a loop that polls the current foreground window using the GetForegroundWindow() API:

        public  static extern IntPtr  GetForegroundWindow();

    and then sends the keys only after it has been brought to the front.

    Of course you run the risk of getting into an infinite loop if for some reason the target window never becomes the foreground window...

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    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…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    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

    19 Experts available now in Live!

    Get 1:1 Help Now