Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4100001

Function keys set as shortcuts get screwed up

XMLWordPrintable

    • generic, x86
    • generic, windows_nt



      Name: rm29839 Date: 12/17/97


      There is a line of code in KeyEvent in the constructor.
          public MenuShortcut(int key, boolean useShiftModifier) {
              // Convenience conversion for programmers who confuse key posts with
              // ASCII characters -- do not internationalize! They *should* be
              // using KeyEvent virtual keys, such as VK_A.

              if (key >= 'a' && key <= 'z') {
                  key = (int)Character.toUpperCase((char)key);
              }

              this.key = key;
              this.usesShift = useShiftModifier;
      }
              
      Since above you are comparing the key with the chars
      a-z function keys and certain other keys are converted
      to uppercase. Example;The F5 key is converted to
      uppercase X when given to the shortcut.
      The conveniance line above needs to be removed!
      (Review ID: 22038)
      ======================================================================


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

      public class test extends Frame implements ActionListener {
          MenuItem mi;
          
          public test () {
      MenuBar menubar = new MenuBar();
      Menu menu = new Menu("File");

      // Add a menu item using the VK_F1 for a function key.
      //
      mi = new MenuItem("A", new MenuShortcut(KeyEvent.VK_F1));

      menu.add(mi);
      mi.addActionListener(this);

      menubar.add(menu);

      setMenuBar(menubar);
      setBounds(0, 0, 200, 200);
      show();
          }
          public void actionPerformed(ActionEvent event) {
      if ( event.getSource() == mi ) {
      System.out.println("Beep!");
      }
          }
          public static void main(String args[]) {
      new test();
          }
      }

               Description of Problem:

      Function keys cannot be used as menu shortcuts.
       
            
               Problem Analysis:
            
               MenuShortcut contains a "convenience conversion" that breaks the
      java spec. and causes this bug. Some code has been added to
      the MenuShortCut constructor for "programmers who confuse key
      posts with ASCII characters" - essentially so that MenuShortCuts
      can be called with lowercase ASCII chars instead of KeyEvent virtual
      keys like it is supposed to be. This conversion is:

      if (key >= 'a' && key <= 'z') {
                  key = (int)Character.toUpperCase((char)key);
                  this.key = key;
                  this.usesShift = useShiftModifier;
               }

      So, if MenuShortCut is called correctly with a KeyEvent.VK and
      this KeyEvent.VK happens to have a value between 'a' and 'z',
      then the key is converted to something else. Function keys
      have KeyEvent.VK values that translate to between 'a' and 'z'
      (e.g. F1 = 0x70 = 'p') hence this problem.

       

      keith.troy@Ireland 1999-09-02

            ehawkessunw Eric Hawkes (Inactive)
            rmandelsunw Ronan Mandel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: