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

MouseInfo Incorrectly Reports Location

    XMLWordPrintable

    Details

    • Subcomponent:
    • Understanding:
      Cause Known
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      FULL PRODUCT VERSION :
      java version "1.5.0_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP Professional SP2

      A DESCRIPTION OF THE PROBLEM :
      Whilst running a program that reports on the location of the mouse cursor at any given time (using the java.awt.MouseInfo lib), I noticed "jumps" in mouse location whenever I locked and unlocked my Windows workstation.

        To see what was up, I added an accumulator to my application to see how many pixels total were being "jumped", and each time the workstation is locked and unlocked, Java believes the cursor to have moved around 200 million pixels with each successive lock and unlock.

      I have tested this with holding the mouse in the air and by simply unplugging it, but always I get a mis-report of cursor location.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create an arbitrary class that extends Thread.

      Call sleep(1) and after every sleep, report the location of the mouse cursor (via system.out.println, logfile, etc).

        To get the accumulated distance travelled in pixels, I used the following code:

      int previousX = MouseInfo.getPointerInfo().getLocation().x;
      int previousY = MouseInfo.getPointerInfo().getLocation().y;

      try {
      sleep(1);
      } catch (InterruptedException ie) { ; }

      int currentX = MouseInfo.getPointerInfo().getLocation().x;
      int currentY = MouseInfo.getPointerInfo().getLocation().y;

      if (previousX != currentX) {
      if (Math.abs(previousX) < Math.abs(currentX)) {
      accumulator += currentX - previousX;
      } else if (Math.abs(previousX) > Math.abs(currentX)) {
      accumulator += previousX - currentX;
      }
      }

      if (previousY != currentY) {
      if (Math.abs(previousY) < Math.abs(currentY)) {
      accumulator += currentY - previousY;
      } else if (Math.abs(previousY) > Math.abs(currentY)) {
      accumulator += previousY - currentY;
      }
      }

      System.out.println("Accumulator >>> " + accumulator);

      Run the application, and then lock and unlock your workstation.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I get a polling running total that increases by an *almost* random amount each time I lock and unlock the workstation, but the output would look something like the following:

      Accumulator >>> 0
      ...
      Accumulator >>> 0
      [lock and unlock done here]
      Accumulator >>> 206879196
      ...
      Accumulator >>> 206879196
      [another lock and unlock]
      Accumulator >>> 413295930

      ...and so on
      ACTUAL -
      I don't think you'd appreciate me posting a huge polled list of output, so I'll just condense what I got:

      Accumulator >>> 0
      Accumulator >>> 310501882
      Accumulator >>> 413295162

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No errors at all. As far as Java is concerned, the program runs fine.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.MouseInfo;
      import java.net.URL;

      public class MouseWatch extends Thread {

      static int accumulator = 0;
      static int currentInterval = 0;
      static boolean watchMouse;

      protected MouseWatch() {
      watchMouse = true;
      start();
      }

      public void run() {
      // Running for one minute
      int updateInterval = 60000;

      while (watchMouse == true) {
      int previousX = MouseInfo.getPointerInfo().getLocation().x;
      int previousY = MouseInfo.getPointerInfo().getLocation().y;

      try {
      sleep(1);
      } catch (InterruptedException ie) { ; }

      int currentX = MouseInfo.getPointerInfo().getLocation().x;
      int currentY = MouseInfo.getPointerInfo().getLocation().y;

      if (previousX != currentX) {
      if (Math.abs(previousX) < Math.abs(currentX)) {
      accumulator += currentX - previousX;
      } else if (Math.abs(previousX) > Math.abs(currentX)) {
      accumulator += previousX - currentX;
      }
      }
      if (previousY != currentY) {
      if (Math.abs(previousY) < Math.abs(currentY)) {
      accumulator += currentY - previousY;
      } else if (Math.abs(previousY) > Math.abs(currentY)) {
      accumulator += previousY - currentY;
      }
      }

      // Plus 2 to the current interval, since we're sleeping for 1 above, therefore 2 have in fact passed
      currentInterval += 2;

      System.out.println("Accumulator >>> " + accumulator);

      if (currentInterval == updateInterval) {
      watchMouse = false;
      }
      }
      currentInterval = 0;
      accumulator = 0;

      // Just in case
      System.gc();
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround at present.

      Perhaps add a keylistener that checks for ctrl+alt+delete key combination, which suspends the thread?

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              denis Denis Fokin (Inactive)
              Reporter:
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Imported:
                Indexed: