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

Peers process KeyEvents before KeyEventPostProcessors

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.2
    • 1.4.1
    • client-libs
    • None
    • mantis
    • generic
    • generic



      Name: osR10079 Date: 06/11/2002

      This bug is about AWT-part of problem described in
      4172991 (Win32 PLAF: Alt key must be pressed twice for focus toggle)

      The notion of KeyEventPostProcessor was introduced to give user way to
      process all KeyEvents after they have been dispatched by registered
      listener. KeyEventPostProcessor should be
      considered as last place to process KeyEvents on java-level. And so
      KeyEvents must be processed by peers only after post processors. But
      now AWT transfers KeyEvents to peers in Component.dispatchEvent()
      which is called before post processors.

      As result, even if post processor consumes an event, this event will
      still be processed by peer as a non-consumed event.

      Here is a testcase in which post processor consumes all ALT
      presses and releases.
      //************** ConsumedKeyEventTest.java
      import java.awt.*;
      import java.awt.event.*;

      public class ConsumedKeyEventTest implements KeyEventPostProcessor
      {
          public static void main(String[] args) {
              KeyboardFocusManager.getCurrentKeyboardFocusManager().
                  addKeyEventPostProcessor(new ConsumedKeyEventTest());
              Frame frame = new Frame("Main Frame");
              LWComponent comp = new LWComponent();
              Label label = new Label("The Label");
              label.setBackground(Color.RED);
              frame.setLayout(new FlowLayout());
              frame.add(label);
              frame.add(comp);
              frame.pack();
              System.err.println(comp.getPeer());

              MouseListener mouseListener = new FocusRequestor();
              label.addMouseListener(mouseListener);
              comp.addMouseListener(mouseListener);
              frame.addMouseListener(mouseListener);
              frame.setVisible(true);
          }

          public boolean postProcessKeyEvent(KeyEvent e) {
              System.err.println("postProcessor(" + e + ")");
              if (e.getKeyCode() == KeyEvent.VK_ALT) {
                  System.err.println("\tconsumed");
                  e.consume();
                  return true;
              }
              return false;
          }

      }

      class LWComponent extends Component {
          LWComponent() {
              super();
              setSize(100, 100);
          }

          public Dimension getPreferredSize() {
              return new Dimension(100, 100);
          }

          public void paint(Graphics g) {
              g.setColor(Color.BLACK);
              g.fillRect(0, 0, 100, 100);
          }
      }

      class FocusRequestor extends MouseAdapter {
          static int counter = 0;
          public void mouseClicked(MouseEvent me) {
              System.err.println(me);
              System.err.println("mouseClicked " + (counter++));
              me.getComponent().requestFocus();
          }
          public void mousePressed(MouseEvent me) {
              System.err.println(me);
          }
          public void mouseReleased(MouseEvent me) {
              System.err.println(me);
          }
      }
      //***** End of ConsumedKeyEventTest.java
      ======================================================================

            son Oleg Sukhodolsky (Inactive)
            son Oleg Sukhodolsky (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: