-4

I want to create a program / script which triggers mouse clicks or drag an drops when the keyboard is used. For example: If u press 1, the mouse location is saved. If u press 2 the mouse will go to the saved location. I know this is possible in different programming languages and i was wondering which one is the best to use for this purpose. And could someone give me a little headstart?

Edit:

import java.awt.MouseInfo;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import java.awt.AWTException;
import java.awt.event.*;

public class nudan implements KeyListener{

    int x1;
    int y1;
    public static void main(String[] args) throws AWTException{
        JFrame jf = new JFrame("Key Event");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.addKeyListener(new nudan());
        jf.setVisible(true);
        jf.setAlwaysOnTop(true);

        Robot rt = new Robot();
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == e.VK_NUMPAD1){
            System.out.println("Key Pressed: " + e.getKeyChar());
            this.y1 = MouseInfo.getPointerInfo().getLocation().y;
            this.x1 = MouseInfo.getPointerInfo().getLocation().x;
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == e.VK_NUMPAD2){
            System.out.println(x1);
            System.out.println(y1);
            try {
                new Robot().mouseMove(x1, y1);

            } catch (AWTException e1) {
                e1.printStackTrace();
            }
        }

    }
    @Override
    public void keyTyped(KeyEvent e) {

    }

}

Thank you so far guys. So this works. It saves the location and Prints '1' if you press Numpad 1. When numpad 2 is pressed, it goes to the saved location and prints the saved location. But somehow when i start my game and try to use this, my mouse doesn't move, eventhought it prints the locations so the script is still running. Anyone has a clue?

  • Why have you tagged three different programming languages? – evolutionxbox Jun 19 '17 at 10:06
  • *"If u press 2 the mouse will go to the saved location"*, you can't do that in JavaScript nor should you be able to. It would be a serious security flaw if you could force the mouse to go to a certain location. – Spencer Wieczorek Jun 19 '17 at 10:06
  • I think mouse move cannot be done due to security reasons in the web based application. If you want to do a such a work, develop a stand alone program by using java, .net etc – MS48 Jun 19 '17 at 10:10
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named "[What topics can I ask about here?](http://stackoverflow.com/help/on-topic)" and "[What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask)". Also please [take the tour](http://stackoverflow.com/tour) and read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – ArturFH Jun 19 '17 at 12:01

1 Answers1

0

This can be done with pretty much every language in a fairly easy way, there is no need to use a specific language for it.

I assume you just started out programming, so one golden rule in programming: just Google it!

For java you can easily find the documentation of the MouseInfo class (java.awt.MouseInfo), which will in large provide the functionalities you'll need.

Java

import java.awt.MouseInfo;

public class ExampleMouseInfo {

     public static void main(String[] args){
         int mouseYPos = MouseInfo.getPointerInfo().getLocation().y;
         int mouseXPos = MouseInfo.getPointerInfo().getLocation().x;
         System.out.println(mouseXPos);
         System.out.println(mouseYPos);
     }
}

Output

488
477

This snippet of code will get the position of your mouse when you run the program. So if you want to trigger this part of code at a certain point (like you said by pressing a button) you make a function out of it and wrap it into an event handler.

Edit: example of moving a mouse can be found here.

Arno C
  • 470
  • 4
  • 18
  • Thanks Arno C!. I've googled already and indeed you can find ways to get the mouse position and print it. But i'm having troubles using the event handlers. I'll keep trying. – Luc Dijkstra Jun 19 '17 at 10:21
  • I've got it working, new problem. The game i want to click in disables all mouse movements, when i close the game it will work again. – Luc Dijkstra Jun 19 '17 at 11:22