1

I am making a macro program. While CTRL mode is on, the program registers the mouse coordinates when the user clicks. CTRL mode is activated when "CTRL" is pressed and deactivated when it's pressed again. If deactivated, the program asks "Perform actions?" If the response is yes, the program click on the registered coordinates.

I am now trying to make the program write if the user wrote something on CTRL mode. Is this possible? If so, how?

Edit: My explanation was poor so i decided to add an example.

EXAMPLE

I active CTRL mode. Then click on notepad and write "hello". Then I deactivate CTRL and run the actions. The program would click on notepad and write "hello".

I don't think you need this but here is the code:

public class Gui extends JFrame {
private JPanel mousePanel;

private JLabel statusBar;
private JLabel keyBar;

public boolean ctrl;

List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
List<KeyEvent> charTyped = new ArrayList<KeyEvent>(); 

public int[] x;
public int[] y;

public Gui() {
    super("Program");
    mousePanel = new JPanel();
    mousePanel.setBackground(Color.WHITE);
    add(mousePanel, BorderLayout.CENTER);
    statusBar = new JLabel("No events");
    keyBar = new JLabel("No key events");
    add(keyBar, BorderLayout.NORTH);;
    add(statusBar, BorderLayout.SOUTH);

    HandlerClass handler = new HandlerClass();
    mousePanel.addMouseListener(handler);
    mousePanel.addMouseMotionListener(handler);
    this.addKeyListener(handler);
}


public void Click(int x, int y) throws AWTException {
    Robot bot = new Robot();
    bot.mouseMove(x, y);
    bot.mousePress(InputEvent.BUTTON1_MASK);
    bot.mouseRelease(InputEvent.BUTTON1_MASK);
}

private class HandlerClass implements MouseListener, MouseMotionListener, KeyListener {

    //Mouse Listener
    public void mouseClicked(MouseEvent event) {
        statusBar.setText(String.format("Clicked at %d, %d", event.getX(), event.getY()));
        if(ctrl) {
            xList.add(MouseInfo.getPointerInfo().getLocation().x);
            yList.add(MouseInfo.getPointerInfo().getLocation().y);
        }
    }

    public void mousePressed(MouseEvent event) {
        statusBar.setText(String.format("You are pressing the mouse at %d, %d", event.getX(), event.getY()));
    }

    public void mouseReleased(MouseEvent event) {
        statusBar.setText(String.format("Released at %d, %d", event.getX(), event.getY()));
    }

    public void mouseEntered(MouseEvent event) {
        statusBar.setText(String.format("Mouse entered at %d, %d", event.getX(), event.getY()));
        mousePanel.setBackground(Color.RED);
    }

    public void mouseExited(MouseEvent event) {
        statusBar.setText(String.format("Mouse exited at %d, %d", event.getX(), event.getY()));
        mousePanel.setBackground(Color.WHITE);
    }

    //Mouse Motion
    public void mouseDragged(MouseEvent event) {
        statusBar.setText(String.format("Dragging mouse at %d, %d", event.getX(), event.getY()));
    }

    public void mouseMoved(MouseEvent event) {
        statusBar.setText(String.format("Moving mouse at %d, %d", event.getX(), event.getY()));
    }

    //Key Listener
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == e.VK_CONTROL && !(ctrl)){
            keyBar.setText("CTRL ON");
            ctrl = true;
        }
        else if(e.getKeyCode() == e.VK_CONTROL && ctrl) {
            keyBar.setText("CTRL OFF");
            ctrl = false;
             if(JOptionPane.showOptionDialog(null, "Perform actions?", "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null) == JOptionPane.YES_OPTION) {
                 int index = 0;
                 for(int actionX : xList) {
                     try {
                        Click(actionX, yList.get(index));
                    } catch (AWTException e1) {
                        e1.printStackTrace();
                    }
                     index++;
                     try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                 }
             }
        }
    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {

    }
}
}
vincentes
  • 45
  • 1
  • 1
  • 7
  • 1
    @MadProgrammer that doesnt make any sense. – specializt Dec 12 '14 at 23:29
  • Why do you want to write this in Java? – Dawood ibn Kareem Dec 13 '14 at 00:13
  • indeed, achieving this with Java will be very hard - this requires knowledge of native APIs like WINAPI, in fact your whole application should be native otherwise you will have a hard time fiddling with JNI - which simply makes things much more complex and error-prone if you're no JNI-expert. – specializt Dec 13 '14 at 00:17
  • @specializt Why? Java can't detect key events that don't go to one it's programs Swing/AWT/JavaFX), you need to use a native solution implemented using JNI or JNA, which means the solution is not part of the default, available libraries... – MadProgrammer Dec 13 '14 at 00:33
  • @David Wallace I want to write this just to practice programming. – vincentes Dec 13 '14 at 00:35
  • this may just be the worst of all choices to practise programming - you chose a very advanced topic, it takes years to be able to master multiple languages at the same time. – specializt Dec 13 '14 at 00:36
  • @specializt Oh, auto correct, how I love you – MadProgrammer Dec 13 '14 at 00:37
  • 1
    It just seems to me that Java is not a very good choice for a program that has to get in and mess around at the operating-system level. Your code will end up being quite operating-system specific; and one of Java's strengths is its relative agnosticism (by comparison with other languages) over which operating system you're using. – Dawood ibn Kareem Dec 13 '14 at 00:38
  • Java can not detect key events which are not triggered by the java application itself (there's no way to monitor global key strokes at the is level), you will need to use a JNI/JNA based solution – MadProgrammer Dec 13 '14 at 00:40
  • please stop repeating the same sentence over and over again - its getting very annoying. Read what has already been written, investigate the mentioned library and stop spamming. – specializt Dec 13 '14 at 00:41

1 Answers1

1

On seeing your latest edit, this is probably what you're looking for.

I deleted the other content in my post since I kept getting comments on the old stuff.

Community
  • 1
  • 1
gknicker
  • 5,509
  • 2
  • 25
  • 41
  • in his case, he simply needs to to `if(ctrl)` – specializt Dec 12 '14 at 23:32
  • I'm trying to make the program WRITE if the user typed something while having CTRL mode on. It would be something like [this](http://download.cnet.com/Ghost-Control/3000-2084_4-10350444.html). I already know how to check if the CTRL key is pressed. – vincentes Dec 12 '14 at 23:34
  • http://www.programmerinterview.com/index.php/java-questions/how-system-out-println-works/ – specializt Dec 12 '14 at 23:42
  • Alright, I think I found what you're looking for... see my last edit. – gknicker Dec 13 '14 at 00:24
  • This won't detect key events outside of the program – MadProgrammer Dec 13 '14 at 00:28
  • @MadProgrammer You sure about that? http://stackoverflow.com/questions/12177416/java-key-listener-to-track-all-keystrokes – gknicker Dec 13 '14 at 00:34
  • You can't detect key events triggered outside of java program using KeyListener, you have to use a JNI/JNA solution which hooks into the OSs keyboard/message queue – MadProgrammer Dec 13 '14 at 00:36
  • you should've visited his link. The mentioned library uses native binaries to hook into the windows messaging system. – specializt Dec 13 '14 at 00:39
  • @gknicker Yes, I am certain that `KeyListener` will not detect OS/global key strokes, which was your answer when I commented. As I stated, unless you use a JNI/JNA solution, which you linked answer suggests, which now makes your answer a link only answer, which could draw downvotes... – MadProgrammer Dec 13 '14 at 01:22
  • @gknicker Oh, and yes, I've done this kind of "recorder" thing before, trust me, JNI/JNA is the only way... – MadProgrammer Dec 13 '14 at 01:24