This question already has answers here:
how to obtain mouse click coordinates outside my window in Java
(12 answers)
Closed 8 years ago.
I am learning java as a beginner and I was tasked to make an auto-click tool. I used JFrame as an Interface but now I'm stuck with this problem:
- I want to get location of pointer related to screen after user clicked a button (Ctrl for example). I added a button on JFrame, click it and the JFrame will be minimized so user can move the pointer around desktop.
- Whenever user pressed Ctrl key, I should println the x,y position of the pointer at that time.
- I've been swum around to find a solution, but it seemed like my brain couldn't catch up.
It would be a very appreciation if anyone can provide me a simple example. Thanks.
My idea so far:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.setState(JFrame.ICONIFIED);
this.addKeyListener(this);
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.CTRL_DOWN_MASK) {
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
System.out.println(x + ":" + y);
}
}
I want to get location of pointer related to screen after user clicked a button..
On button click:
Hide the frame.
Get a screenshot of the entire desktop.
Show the screenshot in a JLabel in a JWindow
Add a MouseListener to the label.
Get the Point from the MouseEvent.
E.G. similar to what is shown in this answer.
Related
This question already has answers here:
Get Mouse Position
(10 answers)
Closed 7 years ago.
As the title says I want to know where my program to know where my mouse is while I'm holding a mouse button and moving it around the JFrame. I could only find a way to detect after I stop holding the button.
Use the method MouseInfo.getPointerInfo().getLocation() - it returns a Point object corresponding to current mouse position.
Add a MouseEventListener to your Button or JFrame. The based on event either MousePress or MouseClick, you can get position event.getX() or event.getY()
public class MouseEventDemo implements MouseListener {
//Register for mouse events on button or frame
yourButton.addMouseListener(this);
...
public void mousePressed(MouseEvent e) {
saySomething("Mouse pressed; # of clicks: " + e.getX(), e);
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am quite new to Java. I decided to code tic-tac-toe as practice (from scratch).
Anyway, I am trying to have the 'JLabel label' change when I click; going from 1, then 2, etc. Eventually, it will change to something other than numbers. But, for now, it works for a test.
The 'System.out.println("Number of clicks: " + mouseIn);' works fine, and produces the output I want. A picture of the console and JFrame/JPanel/JLabel can be seen here:
(The little 1 in the JFrame is the JLabel. I want to match what is output in the console.
I have googled, and searched, and tried everything I know (which ain't much!) and I can't get the dang thing to work...I'm just asking for some guidance. Main method just includes building of JFrame/Panel.
Code below:
From the main class, called (namone.java [named it this for my own reasons]):
public void run(JPanel p) //Takes panel from main method {
for(int i = 0; i < ticList.length; i++){
ticList[i] = new JButton(buttonText); //For every position in
//ticList[], create a JButton object
p.add(ticList[i]);
ticList[i].setPreferredSize(new Dimension(140,140));
ticList[i].addMouseListener(mouse); //Load mouseListner
}
//Set mouseIn to click value in MouseEvents Class
int mouseIn = mouse.getClicks();
//Set text value to text value in MouseEvents class
text = mouse.getText();
//Output...
System.out.println("Number of clicks: " + mouseIn); //For testing
String mouse = Integer.toString(mouseIn); //Convert mouseIn value (from MouseEvents.java) to mouse
JLabel label = new JLabel(); //New JLabel
p.add(label); //Add label to screen
label.setText(mouse); //Set the text of the label to value of mouse
System.out.println(mouse); //For testing
//So I can see if it's working (clicks wise)
}
And then the code from my MouseEvents.java class:
public class MouseEvents extends namone implements MouseListener {
int clicks = 1;
String text = "first"; //For testing purposes
public namone game = new namone();
public int getClicks(){
return clicks;
}
public String getText(){
return text;
}
public int intToString(){
Integer.toString(clicks);
return clicks;
}
#Override
public void mouseClicked(MouseEvent e) {
clicks++;
intToString();
JPanel p = new JPanel();
text = "" + clicks;
game.run(p);
}
As I said. I am very new to Java and I'm trying to learn how to develop applications with it. I'm sure it's caused by my own ignorance.
Thanks.
Assuming that mouse is of type MouseEvents that you write, one possibility is that you need to pass mouse.getText() to your call to label.setText(.).
Regardless, the way you set up your game is a bit strange to me. What is the reason to create a brand new JPanel every time someone clicks? Why not maintain the original JPanel and update it instead. I personally would attach a custom ActionListener to each JButton that runs some code everytime the button is clicked. If this ActionListener is an inner class, it can also view variables in the scope that the JButton is defined.
I want my app to detect mouse clicks anywhere on the screen without having to have the app focused. I want it to detect mouse events universally even if its minimized. So far I've only been able to detect mouse events within a swing gui.
Autohotkey can detect mouse clicks and get the mouse's position at any time, how can I do this with java?
It is possible with a little trick. Should be 100% cross-platform (tested on Linux & Windows). Basically, you create a small JWindow, make it "alwaysOnTop" and move it around with the mouse using a timer.
Then, you can record the click, dismiss the window and forward the click to the actual receiver using the Robot class.
Short left and right clicks work completely fine in my tests.
You could also simulate dragging and click-and-hold, just forwarding that seems harder.
I have code for this, but it is in my Java extension (JavaX). JavaX does translate into Java source code, so you can check out the example here.
The code in JavaX:
static int windowSize = 11; // odd should look nice. Set to 1 for an invisible window
static int clickDelay = 0; // Delay in ms between closing window and forwarding click. 0 seems to work fine.
static int trackingSpeed = 10; // How often to move the window (ms)
p {
final new JWindow window;
window.setSize(windowSize, windowSize);
window.setVisible(true);
window.setAlwaysOnTop(true);
JPanel panel = singleColorPanel(Color.red);
window.setContentPane(panel);
revalidate(window);
final new Robot robot;
panel.addMouseListener(new MouseAdapter {
// public void mousePressed(final MouseEvent e) {}
public void mouseReleased(final MouseEvent e) {
print("release! " + e);
window.setVisible(false);
int b = e.getButton();
final int mod =
b == 1 ? InputEvent.BUTTON1_DOWN_MASK
: b == 2 ? InputEvent.BUTTON2_DOWN_MASK
: InputEvent.BUTTON3_DOWN_MASK;
swingLater(clickDelay, r {
print("clicking " + mod);
robot.mousePress(mod);
robot.mouseRelease(mod);
});
}
});
swingEvery(window, trackingSpeed, r {
Point p = getMouseLocation();
window.setLocation(p.x-windowSize/2, p.y-windowSize/2);
//print("moving");
});
}
I am trying to make a player in java.
Have made a seekbar using jprogressbar as shown in this link in Andrew Thompson's answer,
I have been able to add a mouselistener and detect click on jprogressbar, but how do I get the selected value of jprogressbar to which I will seek my bar to?
I tried,
progressBar.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int v = progressBar.getSelectedValue();
jlabel.setText("----"+v);
}
});
But didn't work as I expected, could not even find anything on internet.
Please help me. Thanks for your time and effort, really appreciated.
You would probably have to calculate the location on the JProgressBar based solely on the mouse click co-ordinates. You could essential do this:
progressBar.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int v = progressBar.getValue();
jlabel.setText("----"+v);
//Retrieves the mouse position relative to the component origin.
int mouseX = e.getX();
//Computes how far along the mouse is relative to the component width then multiply it by the progress bar's maximum value.
int progressBarVal = (int)Math.round(((double)mouseX / (double)progressBar.getWidth()) * progressBar.getMaximum());
progressBar.setValue(progressBarVal);
}
});
Is there any way to get mouse coordinates on a click of desktop screen, i dont want to click inside the java frame, want to click the mouse pointer straight away on desktop and have to know the x,y coordinates ? please help me out? (windows)
Rectangle rectScreenSize = new Rectangle(x1,y1,x2,y2);
BufferedImage biScreen = robot.createScreenCapture (rectScreenSize);
finally want to pass the coordinates for rectangle, to determine the screen size for robot class?
You can make a transparent, undecorated JFrame on top of everything, and pass the click on with the Robot class.
By the way, the following does not work outside your own window (I had hoped so):
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
System.out.println("event: " + event);
if (event.toString().contains("MOUSE_EXITED")) {
System.out.println("mouse_exited");
}
}
}, AWTEvent.MOUSE_EVENT_MASK);