-2

so I have a transparent window that draws a few lines and hud elements. I'm wondering if there's a way to get the position of the mouse within said window when I hit a hotkey set-up such as, say, ctrl-s or something and save the mouse x and y so I can repaint the frame with the updated variables.

My frame code is this:

JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.add(new AimDriver());
frame.setBackground(new Color(0,0,0,0));
frame.setSize(resolutionX, resolutionY);
frame.setAlwaysOnTop(true);
frame.setVisible(true);

Where aimDriver has all the painting methods. Thanks for any help!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Are you asking how to respond to the hot-key when your window/gui does not have the system focus? – Hovercraft Full Of Eels Apr 19 '16 at 23:06
  • 1
    [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – MadProgrammer Apr 19 '16 at 23:10
  • 1
    `frame.setBackground(new Color(0,0,0,0));` A **completely** transparent window will typically not receive events. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 20 '16 at 04:08

2 Answers2

3

A KeyBinding provides several advantages over an KeyListener. Perhaps the most important advantages is that a KeyBinding does not suffer from issues of focus that can plague a KeyListener (See this question for a detailed explanation.)

The below approach follows the KeyBinding Java Tutorial. First, create an AbstractAction that captures the location of the mouse within the window:

AbstractAction action = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent e) {
        Point mLoc = MouseInfo.getPointerInfo().getLocation();
        Rectangle bounds = frame.getBounds();

        // Test to make sure the mouse is inside the window
        if(bounds.contains(mLoc)){
            Point winLoc = bounds.getLocation();
            mouseLoc = new Point(mLoc.x - winLoc.x, mLoc.y - winLoc.y);
        }

    }
};

Note: It's important to test that the window contains the mouse location; if you don't, the mouse location could easily contain a meaningless coordinate (e.g. (-20,199930), what does that even mean?).

Now that you have the desired action, create the appropriate KeyBinding.

// We add binding to the RootPane 
JRootPane rootPane = frame.getRootPane();

//Specify the KeyStroke and give the action a name
KeyStroke KEY = KeyStroke.getKeyStroke("control S");
String actionName = "captureMouseLoc";

//map the keystroke to the actionName
rootPane.getInputMap().put(KEY, actionName);

//map the actionName to the action itself
rootPane.getActionMap().put(actionName, action);
Community
  • 1
  • 1
Austin
  • 8,018
  • 2
  • 31
  • 37
0

Add a Key Listener to your frame object. You can use this post as a reference. Go to the keyPressed event from the above post and replace the println method with code to retrieve the mouse pointer location and update your location variables. You should be able to use this code to get the relative mouse coordinates within your JFrame.

int xCoord = MouseInfo.getPointerInfo().getLocation().x - frame.getLocationOnScreen().x;
int yCoord = MouseInfo.getPointerInfo().getLocation().y - frame.getLocationOnScreen().y;
Community
  • 1
  • 1
Ben
  • 1,132
  • 1
  • 15
  • 35