/* Copyright (c) 2007 Timothy Wall, All Rights Reserved
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.  
 */
package com.sun.jna.examples;

import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import com.sun.jna.Platform;
import junit.framework.TestCase;

public class KeyboardUtilsTest extends TestCase {
    
    public void testIsPressed() throws Exception {
        // Can't run this test headless; not yet implemented on mac
        if (GraphicsEnvironment.isHeadless() || Platform.isMac())
            return;
        
        Robot robot = new Robot();
        int[] keys = {
            // order these to avoid any visible characters
            KeyEvent.VK_CONTROL,
            KeyEvent.VK_A, KeyEvent.VK_Z,
            KeyEvent.VK_0, KeyEvent.VK_9, 
            KeyEvent.VK_SHIFT,
            KeyEvent.VK_U,
        };
        String[] keystr = {
            "VK_CONTROL", "VK_A", "VK_Z", "VK_0", "VK_9", "VK_SHIFT", "VK_U", 
        };
        int[] nonkeys = {
            KeyEvent.VK_B, KeyEvent.VK_1,
            KeyEvent.VK_ALT,
        };
        String[] nonkeystr = {
            "VK_B", "VK_1", "VK_ALT",
        };
        for (int i=0;i < keys.length;i++) {
            robot.keyPress(keys[i]);
        }
        robot.delay(1000);
        try {
            for (int i=0;i < keys.length;i++) {
                assertTrue("Key should be pressed: " + keystr[i], KeyboardUtils.isPressed(keys[i]));
            }
            for (int i=0;i < nonkeys.length;i++) {
                assertFalse("Key should not be pressed: " + nonkeystr[i], KeyboardUtils.isPressed(nonkeys[i]));
            }
        }
        finally {
            for (int i=0;i < keys.length;i++) {
                try { robot.keyRelease(keys[i]); }
                catch(Exception e) { }
            }
        }
    }
}

@@ -1109,169 +1109,81 @@
  // 
-order these to avoid any visible characters%0A            KeyEvent.VK_CONTROL,%0A            KeyEvent.VK_A, KeyEvent.VK_Z,%0A            KeyEvent.VK_0, KeyEvent.VK_9, 
+Avoid terminal control letters (like %5EZ)%0A            KeyEvent.VK_CONTROL,
 %0A   
@@ -1214,35 +1214,8 @@
 FT,%0A
-            KeyEvent.VK_U,%0A
     
@@ -1283,53 +1283,13 @@
 %22VK_
-A%22, %22VK_Z%22, %22VK_0%22, %22VK_9%22, %22VK_SHIFT%22, %22VK_U
+SHIFT
 %22, %0A
@@ -1547,32 +1547,13 @@
     
-robot.keyPress(keys%5Bi%5D);
+try %7B
 %0A   
@@ -1549,34 +1549,32 @@
   try %7B%0A        
-%7D%0A
         robot.de
@@ -1575,18 +1575,24 @@
 bot.
-delay(1000
+keyPress(keys%5Bi%5D
 );%0A 
@@ -1602,128 +1602,81 @@
     
-try %7B%0A
+ 
        
-     for (int i=0;i %3C keys.length;i++) %7B%0A                assertTrue(%22Key should be pressed: %22 + keystr%5Bi%5D, 
+long start = System.currentTimeMillis();%0A                while (!
 Keyb
@@ -1696,33 +1696,34 @@
 ressed(keys%5Bi%5D))
-;
+ %7B
 %0A            %7D%0A 
@@ -1719,26 +1719,24 @@
         
-%7D%0A
         
     for 
@@ -1731,114 +1731,123 @@
     
-    for (int i=0;i %3C nonkeys.length;i++) %7B%0A                assertFalse(%22Key should not be 
+if (System.currentTimeMillis() - start %3E 5000) %7B%0A                        fail(%22Timed out waiting for key
 press
-ed
 : %22 + 
-non
 keys
@@ -1855,48 +1855,75 @@
 r%5Bi%5D
-, KeyboardUtils.isPressed(nonkeys%5Bi%5D));%0A
+);%0A                    %7D%0A                    Thread.sleep(10);%0A    
     
@@ -1944,27 +1944,13 @@
     
-%7D%0A
     
-    finally %7B
+%7D
 %0A   
@@ -1963,40 +1963,14 @@
    f
-or (int i=0;i %3C keys.length;i++)
+inally
  %7B%0A 
@@ -1983,22 +1983,16 @@
         
- try %7B
  robot.k
@@ -2010,16 +2010,28 @@
 eys%5Bi%5D);
+%0A           
  %7D%0A     
@@ -2033,24 +2033,26 @@
         
+%7D%0A
         
 catch(Ex
@@ -2047,30 +2047,46 @@
     
-catch(Exception e
+for (int i=0;i %3C nonkeys.length;i++
 ) %7B
- %7D
 %0A   
@@ -2086,33 +2086,127 @@
 ) %7B%0A            
-%7D
+assertFalse(%22Key should not be pressed: %22 + nonkeystr%5Bi%5D, KeyboardUtils.isPressed(nonkeys%5Bi%5D));
 %0A        %7D%0A    %7D

Remove ^Z from keyboard test, since it may suspend the test