JDK-4838497 : Robot.keyPress(KeyEvent.VK_RIGHT) always types Numpad right arrow key
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6,6u23
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_2000,windows_7
  • CPU: x86
  • Submitted: 2003-03-27
  • Updated: 2010-12-08
Related Reports
Duplicate :  
Relates :  
Description
On windows, Robot.keyPress(KeyEvent.VK_RIGHT) always types NumPad right arrow key regardless of the NumLock on or off. There is no way to force the Robot to  to press the non-numpad arrow key.

Tested with jdk 1.4.2 b18.

Steps to reproduce:

1. Compile altkey.java attached below.
2. run using 'java altkey auto'
3. Robot always presses numpad right arrow key.
4. Some character is typed in the textfield.

5. run using 'java altkey man'
6. Press Alt+Right (non-numpad key) and release in the textfield.
7. No character will be typed in the testfield.



///////////////////////////////////////////////////////////
import	java.awt.*;
import	java.awt.event.*;

public class altkey
{
	Frame frame;
	TextField tf;
	TextArea ta;
	Robot robot;
	int keycode;

	public static void main(String argv[]){
		new altkey(argv[0]);
	}

	public altkey(String string){
	   Frame frame = new Frame("Alt+Right Test");
	   frame.setLayout(new FlowLayout());
	   tf = new TextField("TextField");
	   frame.add(tf);

	   frame.pack();
       frame.setVisible(true);
       tf.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e) {
				keycode = e.getKeyCode();
				System.out.println("TF key pressed:" + e.getKeyCode());
				System.out.println("KeyLocation: " + e.getKeyLocation());
	   			}
			public void keyReleased(KeyEvent e) {
				keycode = e.getKeyCode();
				System.out.println("TF key released:" + e.getKeyCode());
	   			}
			});
		try {
	           robot = new Robot();
	       } catch (AWTException e) {
	           System.out.println("Problem creating Robot.  FAIL.");
	           throw new RuntimeException("Problem creating Robot.  FAIL.");

       }
		robot.setAutoDelay(1000);
		if(string.equals("auto")) {
		auto();
		robot.setAutoDelay(3000);
		frame.dispose();
		}

	}

	public void auto() {
		//Click on the TextField
		int textX = (tf.getLocationOnScreen()).x;
		int textY = (tf.getLocationOnScreen()).y;
		robot.mouseMove( textX+2, textY+2);
		robot.mousePress(InputEvent.BUTTON1_MASK);
		robot.delay(100);
		robot.mouseRelease(InputEvent.BUTTON1_MASK);
		tf.setText("");

		robot.keyPress(KeyEvent.VK_ALT);
		robot.setAutoDelay(200);
		robot.keyPress(KeyEvent.VK_RIGHT);
		robot.keyRelease(KeyEvent.VK_RIGHT);

		robot.setAutoDelay(200);
		robot.keyRelease(KeyEvent.VK_ALT);
		robot.setAutoDelay(200);
		if(tf.getText() != null) {
			System.out.println("Fail: Some Symbol is typed when pressed Alt+Right arrow keys pressed");
		}
		else {
			System.out.println("Pass");
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Comments
EVALUATION In java.awt.Robot there's no API to mimic different variants of a key if available. For instance, spec says: "Key codes that have more than one physical key associated with them (e.g. KeyEvent.VK_SHIFT could mean either the left or right shift key) will map to the left key." Thus, in this sense numpad left arrow is "left" left arrow. My opinion is, this design flaw in Robot should be treated as RFE, and API should be provided: something like robot.keyPress(javakey, location).
01-08-2005

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
17-09-2004

EVALUATION See 4760074 as well. I'm kind of surprised that it generates a character, and that the numpad right arrow key is pressed (instead of the non-numpad right arrow). Those may be two separate problems, though. If so, it may make sense to split them into two separate bugs. I'll commit this to Tiger. We really should not be generating a character in the TextField. Perhaps the cause is similar to 4737679? ###@###.### 2003-03-27
27-03-2003