BUG: SendKeys.sendKeys() and SendKeys.sendKeysWait() Do Not Work as Expected from Java (282931)



The information in this article applies to:

  • Microsoft Visual J++ 6.0

This article was previously published under Q282931

SYMPTOMS

The sendKeys and sendKeysWait methods that send keystrokes or combinations of keystrokes to an active application do not work as expected.

RESOLUTION

To work around this problem, use the WM_GETTEXT and WM_SETTEXT messages. However, this may only work for certain situations, for example when you enter text into an instance of Notepad. See the "More Information" section for sample code that illustrates this workaround.

Another workaround is to use J/Direct, which allows you to call Win32 Application Programming Interfaces (APIs) from Java. For more information, see the following articles on the Microsoft Developer Network (MSDN): These APIs are hook procedures that can be used with the SetWindowsHook and SetWindowsHookEx functions. For additional information about source code that uses these Windows Journal Hooks, click the article number below to view the article in the Microsoft Knowledge Base:

37138 Journal.exe Contains Windows Journal Hooks Source Code

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

To reproduce the behavior, compile and run the following code:
import com.ms.wfc.app.*;
import com.ms.win32.*;


public class SendKeyApp extends Window implements Runnable 
{
  private int MainHWND = 0;
  private int ChildHWND = 0;
  private int errorcount = 0;

  public static void main (String[] args)
  {
    try
    {
      Process p = Runtime.getRuntime().exec("notepad");
      
      new Thread(new SendKeyApp()).start();
      p.waitFor();
    }
    catch (Exception e)
    {
      System.out.println(e.getMessage());
    }
  }
  
  public void run()
  {
    try 
    {
      getHandles();
      setPosition();
      Thread.sleep(500);

      sendKeyTest();
      //wrkAround();
    }
    catch (Exception e)
    {
      if (e.getMessage().equalsIgnoreCase("Null Window Handle.") && errorcount++ < 5)
        try 
        {
          Thread.sleep(100);
          run();
        }
        catch (Exception e2){}
      else 
        System.out.println(e.getMessage());
    }
  }
  
  private void getHandles()
  {
    MainHWND = User32.FindWindow(null, "Untitled - Notepad");
    ChildHWND = User32.GetWindow(MainHWND, win.GW_CHILD);
  }
  
  private void setPosition() throws Exception
  {
    if (MainHWND == 0)
      throw new Exception("Null Window Handle.");
    
    User32.SetWindowPos(MainHWND, 
                        winh.HWND_TOPMOST,
                        50,
                        50,
                        450,
                        450,
                        wins.SWP_SHOWWINDOW);
    
  }
  
  private void sendKeyTest()
  {
    SendKeys.sendKeysWait("This is text from SendKeys.");
/*
    SendKeys.sendKeysWait("T");
    SendKeys.sendKeysWait("e");
    SendKeys.sendKeysWait("s");
    SendKeys.sendKeysWait("t");
    SendKeys.sendKeysWait("i");
    SendKeys.sendKeysWait("n");
    SendKeys.sendKeysWait("g");
*/ 
  } 

  private void wrkAround() throws Exception
  {
    if (ChildHWND == 0)
      throw new Exception("Null Window Handle.");

    StringBuffer buf = new StringBuffer("This is text from WM_SETTEXT.");
    int size = buf.length();
    
    User32.SendMessage(ChildHWND,
                       win.WM_SETTEXT,
                       size,
                       buf);
  }
}

				
To use the workaround, uncomment the call to wrkAround.

REFERENCES

For more information on J/Direct, see the SDK for Java documentation at the following Microsoft Web site: For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:6/14/2006
Keywords:kbBug kbpending kbWFC KB282931