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) {
}
}
}