Microsoft KB Archive/282931

= BUG: SendKeys.sendKeys and SendKeys.sendKeysWait Do Not Work as Expected from Java =

Article ID: 282931

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Visual J++ 6.0 Standard Edition

-



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 &quot;More Information&quot; 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):

JournalPlayBackProc

http://msdn.microsoft.com/library/psdk/winbase/hooks_0aoz.htm

JournalRecordProc

http://msdn.microsoft.com/library/psdk/winbase/hooks_4t9f.htm

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

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

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

To use the workaround, uncomment the call to wrkAround.

