How can I record mouse motions over the entire window?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

48 Replies - 1614 Views - Last Post: 20 June 2020 - 09:37 AM Rate Topic: -----

#1 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 17-August 13

How can I record mouse motions over the entire window?

Posted 19 April 2020 - 04:25 AM

Hello, I want to record the mouse position over the entire mainframe of an application,
however, I noticed that components on the window seem dead.
Must I addMouseMotionListerer() to EVERY JPanel, JTree, Box etc. in order to cover the entire application?
And is the argument that catches mouseMover() the same for all of them?
Finally, how do I deal with absolute and relative X and Y in order to get an address for the mainframe?
: I am planning to Robot.mouseMove to that address later.
Thanks.
;JOOP!

Is This A Good Question/Topic? 0
  • +

Replies To: How can I record mouse motions over the entire window?

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,980
  • Joined: 20-September 08

Re: How can I record mouse motions over the entire window?

Posted 19 April 2020 - 08:10 AM

Have a look at using a GlassPane
https://docs.oracle.....html#glasspane
Was This Post Helpful? 1
  • +
  • -

#3 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 17-August 13

Re: How can I record mouse motions over the entire window?

Posted 19 April 2020 - 08:23 AM

Interesting; I will look into it.
;JOOP!
Was This Post Helpful? 0
  • +
  • -

#4 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 17-August 13

Re: How can I record mouse motions over the entire window?

Posted 20 April 2020 - 04:24 AM

View Postg00se, on 19 April 2020 - 04:10 PM, said:

Have a look at using a GlassPane
https://docs.oracle.....html#glasspane

I have never used or thought of the GlassPane in 20 years. At least thanks for the idea.
Do I understand well:
- adding a MouseMotionListener to the GlassPane should not disturb any component below, unless those do the same thing?
(I read about blocking mouse clicks in case of a MouseListener)
- in my setup mouse coordinates from this listener will be relative to the entire window?
- assuming the GlassPane is by default only used to paint the cursor, can I paint on it too without problems below?
;JOOP!
Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: How can I record mouse motions over the entire window?

Posted 20 April 2020 - 04:36 AM

Quote

can I ...

Take the sample code from the tutorial and try experimenting with it to see what can be done.
If you have any problems, post the code and describe the problems.
Was This Post Helpful? 1
  • +
  • -

#6 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 17-August 13

Re: How can I record mouse motions over the entire window?

Posted 20 April 2020 - 04:39 AM

View PostNormR, on 20 April 2020 - 12:36 PM, said:

Quote

can I ...

Take the sample code from the tutorial and try experimenting with it to see what can be done.
If you have any problems, post the code and describe the problems.

OK
Was This Post Helpful? 0
  • +
  • -

#7 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 17-August 13

Re: How can I record mouse motions over the entire window?

Posted 06 June 2020 - 08:56 AM

View PostNormR, on 20 April 2020 - 12:36 PM, said:

Quote

can I ...

Take the sample code from the tutorial and try experimenting with it to see what can be done.
If you have any problems, post the code and describe the problems.

Sorry for the delay, I could not work on it for some time.

I downloaded the sample code from the tutorial and made a project of it in ECLIPSE (with JAVA 14.0.1).
The code appeared obsolete with respect to "e.getModifiers()" ; I changed it to "e.getModifiersEx()".
Consequently it does not work: as soon as the checkbox is hit, a red circle appears
and no checkbox or button responds anymore. The upper left close control works still, so I did not have to abort.
Is there a better code sample available?
;JOOP!
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: How can I record mouse motions over the entire window?

Posted 06 June 2020 - 08:59 AM

Please post the code with the problems. Also copy and paste here the compilers error messages.
Make sure to wrap the code in code tags.
Was This Post Helpful? 0
  • +
  • -

#9 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 17-August 13

Re: How can I record mouse motions over the entire window?

Posted 06 June 2020 - 09:05 AM

View PostNormR, on 06 June 2020 - 04:59 PM, said:

Please post the code with the problems. Also copy and paste here the compilers error messages.
Make sure to wrap the code in code tags.

The code is equal to the sample in the tutorial besides the correction mentioned.
There are NO compiler errors, only 37 warnings for missing comments, name hiding and the sort;
the complaints are caused by my sharp settings in ECLIPSE; those are of no concern here.

;JOOP!
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: How can I record mouse motions over the entire window?

Posted 06 June 2020 - 09:17 AM

Ok, I will until you can post the code and the problem description.

This might be another way: https://docs.oracle....ed_windows.html

This post has been edited by NormR: 06 June 2020 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#11 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 17-August 13

Re: How can I record mouse motions over the entire window?

Posted 06 June 2020 - 09:27 AM

View PostNormR, on 06 June 2020 - 05:17 PM, said:

Ok, I will until you can post the code and the problem description.

I guess you "will wait".
Well:
/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputAdapter;

/** An application that requires no other files. */
public class TestCase {
    static private MyGlassPane myGlassPane;

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TestCase");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Start creating and adding components.
        JCheckBox changeButton =
                new JCheckBox("Glass pane \"visible\"");
        changeButton.setSelected(false);
        
        //Set up the content pane, where the "main GUI" lives.
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new FlowLayout());
        contentPane.add(changeButton);
        contentPane.add(new JButton("Button 1"));
        contentPane.add(new JButton("Button 2"));

        //Set up the menu bar, which appears above the content pane.
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        menu.add(new JMenuItem("Do nothing"));
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);

        //Set up the glass pane, which appears over both menu bar
        //and content pane and is an item listener on the change
        //button.
        myGlassPane = new MyGlassPane(changeButton, menuBar,
                                      frame.getContentPane());
        changeButton.addItemListener(myGlassPane);
        frame.setGlassPane(myGlassPane);

        //Show the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

/**
 * We have to provide our own glass pane so that it can paint.
 */
class MyGlassPane extends JComponent
                  implements ItemListener {
    Point point;

    //React to change button clicks.
    public void itemStateChanged(ItemEvent e) {
        setVisible(e.getStateChange() == ItemEvent.SELECTED);
    }

    protected void paintComponent(Graphics g) {
        if (point != null) {
            g.setColor(Color.red);
            g.fillOval(point.x - 10, point.y - 10, 20, 20);
        }
    }

    public void setPoint(Point p) {
        point = p;
    }

    public MyGlassPane(AbstractButton aButton,
                       JMenuBar menuBar,
                       Container contentPane) {
        CBListener listener = new CBListener(aButton, menuBar,
                                             this, contentPane);
        addMouseListener(listener);
        addMouseMotionListener(listener);
    }
}

/**
 * Listen for all events that our check box is likely to be
 * interested in.  Redispatch them to the check box.
 */
class CBListener extends MouseInputAdapter {
    Toolkit toolkit;
    Component liveButton;
    JMenuBar menuBar;
    MyGlassPane glassPane;
    Container contentPane;

    public CBListener(Component liveButton, JMenuBar menuBar,
                      MyGlassPane glassPane, Container contentPane) {
        toolkit = Toolkit.getDefaultToolkit();
        this.liveButton = liveButton;
        this.menuBar = menuBar;
        this.glassPane = glassPane;
        this.contentPane = contentPane;
    }

    public void mouseMoved(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mouseDragged(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mouseClicked(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mouseEntered(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mouseExited(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mousePressed(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mouseReleased(MouseEvent e) {
        redispatchMouseEvent(e, true);
    }

    //A basic implementation of redispatching events.
    private void redispatchMouseEvent(MouseEvent e,
                                      boolean repaint) {
        Point glassPanePoint = e.getPoint();
        Container container = contentPane;
        Point containerPoint = SwingUtilities.convertPoint(
                                        glassPane,
                                        glassPanePoint,
                                        contentPane);
        if (containerPoint.y < 0) { //we're not in the content pane
            if (containerPoint.y + menuBar.getHeight() >= 0) { 
                //The mouse event is over the menu bar.
                //Could handle specially.
            } else { 
                //The mouse event is over non-system window 
                //decorations, such as the ones provided by
                //the Java look and feel.
                //Could handle specially.
            }
        } else {
            //The mouse event is probably over the content pane.
            //Find out exactly which component it's over.  
            Component component = 
                SwingUtilities.getDeepestComponentAt(
                                        container,
                                        containerPoint.x,
                                        containerPoint.y);
                            
            if ((component != null) 
                && (component.equals(liveButton))) {
                //Forward events over the check box.
                Point componentPoint = SwingUtilities.convertPoint(
                                            glassPane,
                                            glassPanePoint,
                                            component);
                component.dispatchEvent(new MouseEvent(component,
                                                     e.getID(),
                                                     e.getWhen(),
                                                     e.getModifiersEx(),
                                                     componentPoint.x,
                                                     componentPoint.y,
                                                     e.getClickCount(),
                                                     e.isPopupTrigger()));
            }
        }
        
        //Update the glass pane if requested.
        if (repaint) {
            glassPane.setPoint(glassPanePoint);
            glassPane.repaint();
        }
    }
}


The problem description is already been posted.
;JOOP!
Was This Post Helpful? 0
  • +
  • -

#12 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,980
  • Joined: 20-September 08

Re: How can I record mouse motions over the entire window?

Posted 06 June 2020 - 09:31 AM

Quote

Consequently it does not work: as soon as the checkbox is hit, a red circle appears
and no checkbox or button responds anymore. The upper left close control works still, so I did not have to abort.


That's what it's meant to do ;)
You need to study the code to see how it's doing exactly what you want to do (from your question title)
Was This Post Helpful? 1
  • +
  • -

#13 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 17-August 13

Re: How can I record mouse motions over the entire window?

Posted 06 June 2020 - 09:33 AM

If the demo does not do what the tutorial promised ... I give up.
;JOOP!
Was This Post Helpful? 0
  • +
  • -

#14 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: How can I record mouse motions over the entire window?

Posted 06 June 2020 - 09:44 AM

The code compiles and executes as expected with Java 8 and java 14.
Was This Post Helpful? 1
  • +
  • -

#15 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 17-August 13

Re: How can I record mouse motions over the entire window?

Posted 07 June 2020 - 06:39 AM

View PostNormR, on 06 June 2020 - 05:44 PM, said:

The code compiles and executes as expected with Java 8 and java 14.

Funny: the program indeed runs with the deprecated method call, but NOT with the replacement
method as indicated in the JAVADOC.
I must look into this and estimate if all required changes to my application are worth it.
I did not work with ItemListeners so far.

Thanks for your help.
Closed.

;JOOP!
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »