JDK-4760074 : Robot cannot type VK_KP_RIGHT on Windows
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-10-08
  • Updated: 2005-02-10
Related Reports
Relates :  
Relates :  
Relates :  
Description
Calling Robot.keyPress(KeyEvent.VK_KP_RIGHT) on Windows 2000 results in the following:

Exception occurred during event dispatching:
java.lang.IllegalArgumentException: Invalid key code
        at sun.awt.windows.WRobotPeer.keyPress(Native Method)
        at java.awt.Robot.keyPress(Robot.java:183)
        at RobotArrow.actionPerformed(RobotArrow.java:50)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
        at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
        at java.awt.Component.processMouseEvent(Component.java:3715)
        at java.awt.Component.processEvent(Component.java:3544)
        at java.awt.Container.processEvent(Container.java:1164)
        at java.awt.Component.dispatchEventImpl(Component.java:2593)
        at java.awt.Container.dispatchEventImpl(Container.java:1213)
        at java.awt.Component.dispatchEvent(Component.java:2497)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
        at java.awt.Container.dispatchEventImpl(Container.java:1200)
        at java.awt.Window.dispatchEventImpl(Window.java:914)
        at java.awt.Component.dispatchEvent(Component.java:2497)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java

VK_KP_RIGHT represents "the numeric keypad right arrow key."  PC keyboards have numeric keypad right arrow keys, so I don't think this should be an illegal argument.  I'm guessing that the same happens w/ left, up, and down as well.

This is reproducible using 1.3.1 as well.

I used the following test case:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RobotArrow extends JTextField implements ActionListener, KeyListener {

    static Robot robot;

    public RobotArrow() {super();}

    public static void main(String[] args) {
        try {
            robot = new Robot();
            robot.setAutoDelay(100);
            JFrame jf = new JFrame("RobotArrow");
            RobotArrow jtf = new RobotArrow();
            jtf.addKeyListener(jtf);
            JButton jb = new JButton("Type Alt-Arrow");
            jb.addActionListener(jtf);
            jb.addKeyListener(jtf);
            Container c = jf.getContentPane();
            c.setLayout(new GridLayout(2,1));
            c.add(jtf);
            c.add(jb);
            jf.setBounds(100, 100, 200, 200);
            jf.setVisible(true);

        } catch (AWTException e) {
            System.out.println("Couldn't create Robot");
        }
    }

    public void actionPerformed(ActionEvent e) {
        requestFocus();

        System.out.println("Typing ALT-VK_KP_RIGHT");
        robot.keyPress(KeyEvent.VK_KP_RIGHT);
        robot.keyRelease(KeyEvent.VK_KP_RIGHT);
    }

    public void keyPressed(KeyEvent e) { System.out.println(e); }
    public void keyReleased(KeyEvent e) { System.out.println(e); }
    public void keyTyped(KeyEvent e) { System.out.println(e); }
}

Comments
EVALUATION The reason an IAE is thrown is that we don't translate this Java keycode in JavaKeyToWindowsKey in awt_Component.cpp because there are no entries for it in the keymap table. // NumPad with NumLock off & extended arrows block (triangular) {java_awt_event_KeyEvent_VK_LEFT, VK_LEFT}, {java_awt_event_KeyEvent_VK_RIGHT, VK_RIGHT}, {java_awt_event_KeyEvent_VK_UP, VK_UP}, {java_awt_event_KeyEvent_VK_DOWN, VK_DOWN}, This is because we don't honor the KP variants of these keys at all in Windows. If we started doing it now, it would mean that we would be backwards incompatible with previous releases. This is a really unfortunate repercussion of not doing this back when we designed the toolkit. See 4342184. ###@###.### 2002-10-08 I wonder if we could add a field to Robot.keyPress and keyRelease (actually new methods for both) that allow specifying the location? It might be useful, but the result on Windows would be that pressing the numpad arrow keys would have to produce Java key events for the distinct arrow keys. ###@###.### 2002-10-08 I wonder if we should do something more intelligent than throw an IAE here. That is what we do for other keys that are not on the windows keyboard (e.g. VK_HELP and keys that are dynamically mapped, but not in the current keyboard mapping). However, the keypad arrow keys are on the keyboard, so this is not quite the same situation. ###@###.### 2002-10-08 Perhaps a slightly better solution would be to add entries for the keypad arrow keys after the existing entries. That way, they would be found when you traverse the table looking for the java entry, but not when you traverse the table looking for the windows entry. // NumPad with NumLock off & extended arrows block (triangular) {java_awt_event_KeyEvent_VK_LEFT, VK_LEFT}, {java_awt_event_KeyEvent_VK_RIGHT, VK_RIGHT}, {java_awt_event_KeyEvent_VK_UP, VK_UP}, {java_awt_event_KeyEvent_VK_DOWN, VK_DOWN}, // NumPad with NumLock off & extended arrows block (triangular) again {java_awt_event_KeyEvent_VK_KP_LEFT, VK_LEFT}, {java_awt_event_KeyEvent_VK_KP_RIGHT, VK_RIGHT}, {java_awt_event_KeyEvent_VK_KP_UP, VK_UP}, {java_awt_event_KeyEvent_VK_KP_DOWN, VK_DOWN}, ###@###.### 2003-03-27
27-03-2003