JDK-4106941 : KeyCodes for numeric keypad arrow keys are inconsistent between win32/Solaris
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 1998-01-27
  • Updated: 2001-04-12
  • Resolved: 2001-04-12
Related Reports
Relates :  
Relates :  
Description
KeyCodes for the arrow keys on the numeric keypad are different between Win32 and Solaris.  They used to be identical in 1.1.6 (37, 38, 39, 40, but in 1.2 they have changed on Solaris (226, 224, 227, 225) and not changed on Win32.

Run the following applet on Solaris, click in the TextArea, and hit the arrow keys on the numeric keypad, with NumLock OFF.  It will print the following KeyCodes:
    KeyChar= KeyCode=226 KeyText=Left
    KeyChar= KeyCode=224 KeyText=Up
    KeyChar= KeyCode=227 KeyText=Right
    KeyChar= KeyCode=225 KeyText=Down

Then run the applet on Win32, click in the TextArea, and hit the same arrow keys.  It will print the following KeyCodes:
    KeyChar= KeyCode=37 KeyText=Left
    KeyChar= KeyCode=38 KeyText=Up
    KeyChar= KeyCode=39 KeyText=Right
    KeyChar= KeyCode=40 KeyText=Down


//-------------------ArrowKey2.html--------------------------
<html>
<body>
<applet code=ArrowKey2.class width=400 height=150></applet>
</body>
</html>


//-------------------ArrowKey2.java--------------------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ArrowKey2 extends Applet
{
    private KeyWatch keyear;
    private TextArea fld;

    public ArrowKey2() {}

    public void init()
    {
        keyear = new KeyWatch();
        this.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 30));
        fld = new TextArea("Some Text\nSome Text\nSome Text\nSome Text",
                    10, 20);
        this.add(fld);
        fld.addKeyListener(keyear);
        this.addKeyListener(keyear);
    }
}

class KeyWatch extends KeyAdapter
{
    public void keyPressed(KeyEvent e)
    {
        System.out.print("KeyChar=" + e.getKeyChar());
        System.out.print(" KeyCode=" + e.getKeyCode() );
        System.out.println(" KeyText=" + e.getKeyText(e.getKeyCode()));
    }
}

Comments
EVALUATION eric.hawkes@eng 1998-03-02 The numpad arrow keys return incorrect keycodes on Solaris 2.6 using build 1.2beta3 G. They are as the user says (226, 224, 227, 225). The keycode should not change because of the state of the modifiers, in this case Numlock. See page 927 of "The Java Class Libraries, Second Edition, Volume 2." (The Key Characters, on the other hand, DO change based on the modifiers. There are several bugs that have been caused or fixed improperly because of this confusion.) The correct keycodes for the numeric keypad are VK_NUMPAD4 = 100, VK_NUMPAD8 = 104, VK_NUMPAD6 = 102, VK_NUMPAD2 = 98. The distinct arrow keys are returning the correct keycodes (VK_LEFT = 37, VK_UP = 38, VK_RIGHT = 39, VK_DOWN = 40). This bug was introduced in 1.1 and 1.2 as the result of a fix for 4087898/4087994. The fix was backed out in 1.1, and should have been backed out in 1.2 as well. The affected file is canvas.c eric.hawkes@eng 2000-06-22 I'm no longer so sure the above comments are correct. There have been a number of requests to provide a way to distinguish the numpad arrow keys from the distinct arrow keys. One way to handle this would be to change the win32 implementation to match the Solaris implementation. Another way would be to provide a method like isExtendedKey() which would tell us whether the key pressed is on the extended protion of the keyboard, or isNumpadKey() which would tell us whether the key is on the numpad. eric.hawkes@eng 2000-06-22 It is also probably worth noting that the keycodes generated will differ based on the way the keyboard is configured. I think that the default setting on Solaris is for both the distinct arrow keys and the numpad arrow keys to generate XK_Left, etc. This will cause the same keycodes to be produced for both the numpad arrow keys and the distinct arrow keys. The keycodes the user reported seeing are generated when the numpad arrow keys are mapped to XK_KP_Left, etc. eric.hawkes@eng 2000-06-22 We can't fix this with the current key event model without breaking backwards compatibility. This is also reported in bugid 4342184 (see the Comments). There is a way to differentiate between the numpad and the distinct arrow keys using the APIs putback with 4424517. (This also provides a way to discriminate between the left and right versions of shift, ctrl, alt, etc.) Closing as Will Not Fix. eric.hawkes@eng 2001-04-11
11-04-2001

WORK AROUND Use xmodmap to set the numeric keypad arrow keys to generate XK_Left, etc. instead of XK_KP_Left. This will cause the keycodes generated to be the same for both numpad arrow keys and distinct arrow keys. eric.hawkes@eng 2000-06-22
22-06-2000