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

Passing an Event to KeyAdapter overriden KeyPress method does not parse Mouse Position

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      This would be a more intuitive way to provide hold commands for keypresses that utilize mouse movement.

      KeyAdapter Overrided KeyPress function, when parsing and event does not parse the mouse position.
      Hence another less intuitive method is required to solve the problem.
      The problem is that in a complex program with 3000 lines of code, there may be 3 problems together, which all seem like the same problem, hence figuring out how that works is a very time consuming task.

      If it was possible to parse the mouse position with the key press, then it would be more intuitive? But less efficient?

      REGRESSION : Last worked in version 16

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      public class simple_example {
      static JFrame jf1;
      static int dif_h;
      static int dif_w;
      static int prev_mouse_drag_height;
      static int prev_mouse_drag_width;
      static class GlobalKeyListener extends KeyAdapter {
      public void keyPressed(KeyEvent e) {
      int h = MouseInfo.getPointerInfo().getLocation().y - jf1.getY(); //this is non intuitive and hard to figure out in a very complex enviroment
      int w = MouseInfo.getPointerInfo().getLocation().x - jf1.getX(); //this is non intuitive and hard to figure out in a very complex enviroment

      System.out.println("keyPressed:\nh:" + h + " w:" + w + "\ndif_h:" + dif_h + " dif_w:" + dif_w + "\nprev_h:" + prev_mouse_drag_height + " prev_w:" + prev_mouse_drag_width + "\n");

      dif_h = h - prev_mouse_drag_height;
      dif_w = w - prev_mouse_drag_width;
      prev_mouse_drag_height = h;
      prev_mouse_drag_width = w;
      }
      }
      static class GlobalMouseListener extends MouseAdapter {
      public void mousePressed(MouseEvent e) {
      prev_mouse_drag_width = e.getX();
      prev_mouse_drag_height = e.getY();
      }
      }
      static class MiddleMouseMotionListener extends MouseMotionAdapter {
      public void mouseDragged(MouseEvent e) {
      int h = e.getY();
      int w = e.getX();

      System.out.println("mouseDragged:\nh:" + h + " w:" + w + "\ndif_h:" + dif_h + " dif_w:" + dif_w + "\nprev_h:" + prev_mouse_drag_height + " prev_w:" + prev_mouse_drag_width + "\n");

      dif_h = h - prev_mouse_drag_height;
      dif_w = w - prev_mouse_drag_width;
      prev_mouse_drag_height = h;
      prev_mouse_drag_width = w;
      }
      }
      static int frame_width = 400;
      static int frame_height = 300;
      public static void main(String[] args) {
      jf1 = new JFrame();
      jf1.setLayout(null);
      jf1.setSize(frame_width+16,frame_height+39); //+16,+39 for window sizes
      jf1.setTitle("Renderer");

      jf1.addMouseListener(new GlobalMouseListener());
      jf1.addMouseMotionListener(new MiddleMouseMotionListener());
      jf1.addKeyListener(new GlobalKeyListener());

      jf1.setDefaultCloseOperation(3);
      jf1.setVisible(true);
      }
      }

      This is an extremely simplified version of the code, the code shows the non intuitive part in keypress which does a very similar thing to the mousedrag. This is obviously very complex inside a much larger program.

      1. Run the program
      2. Drag the mouse on screen
      3. Drag the key on screen
      4. Compare the results
      5. Look at the code. the part that is unintuitive has parts buried deep within documentation that is very difficult to find, the parts consist of a JFrame that is far away and a MouseInfo.getPointerInfo().getLocation() which is very far away from e.getY()
      Figuring out exactly what the problem is when you don't know what the problem is involves a significant amount of reading, and testing very far away from the gui aspect and is immensely difficult and time consuming.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program works fine now, I'm asking for a feature, there might be a way to add the feature in efficiently and intuitively.
      ACTUAL -
      The program works fine now, I'm asking for a feature, there might be a way to add the feature in efficiently and intuitively.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      public class simple_example {
      static JFrame jf1;
      static int dif_h;
      static int dif_w;
      static int prev_mouse_drag_height;
      static int prev_mouse_drag_width;
      static class GlobalKeyListener extends KeyAdapter {
      public void keyPressed(KeyEvent e) {
      int h = MouseInfo.getPointerInfo().getLocation().y - jf1.getY(); //this is non intuitive and hard to figure out in a very complex enviroment
      int w = MouseInfo.getPointerInfo().getLocation().x - jf1.getX(); //this is non intuitive and hard to figure out in a very complex enviroment

      System.out.println("keyPressed:\nh:" + h + " w:" + w + "\ndif_h:" + dif_h + " dif_w:" + dif_w + "\nprev_h:" + prev_mouse_drag_height + " prev_w:" + prev_mouse_drag_width + "\n");

      dif_h = h - prev_mouse_drag_height;
      dif_w = w - prev_mouse_drag_width;
      prev_mouse_drag_height = h;
      prev_mouse_drag_width = w;
      }
      }
      static class GlobalMouseListener extends MouseAdapter {
      public void mousePressed(MouseEvent e) {
      prev_mouse_drag_width = e.getX();
      prev_mouse_drag_height = e.getY();
      }
      }
      static class MiddleMouseMotionListener extends MouseMotionAdapter {
      public void mouseDragged(MouseEvent e) {
      int h = e.getY();
      int w = e.getX();

      System.out.println("mouseDragged:\nh:" + h + " w:" + w + "\ndif_h:" + dif_h + " dif_w:" + dif_w + "\nprev_h:" + prev_mouse_drag_height + " prev_w:" + prev_mouse_drag_width + "\n");

      dif_h = h - prev_mouse_drag_height;
      dif_w = w - prev_mouse_drag_width;
      prev_mouse_drag_height = h;
      prev_mouse_drag_width = w;
      }
      }
      static int frame_width = 400;
      static int frame_height = 300;
      public static void main(String[] args) {
      jf1 = new JFrame();
      jf1.setLayout(null);
      jf1.setSize(frame_width+16,frame_height+39); //+16,+39 for window sizes
      jf1.setTitle("Renderer");

      jf1.addMouseListener(new GlobalMouseListener());
      jf1.addMouseMotionListener(new MiddleMouseMotionListener());
      jf1.addKeyListener(new GlobalKeyListener());

      jf1.setDefaultCloseOperation(3);
      jf1.setVisible(true);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Work around is inside the code

      FREQUENCY : always


            honkar Harshitha Onkar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: