Reputation: 39
im trying to create a second cursor clicker, i've made the code to insert a fixed location where i want this "second cursor" to click using the "x" key but its getting my original cursor to make the click, i want it to be a separated click. I dont want it to be a second mouse just a second separated cursor clicker where it doesnt affects the original cursor clicking or moving. Theres my actual code for this application where its using the original windows cursor to click.
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
public class MultiCursorSimulator implements NativeKeyListener {
private static Robot robot;
private static JSlider xSlider;
private static JSlider ySlider;
private static JFrame overlayFrame;
private static OverlayPanel overlayPanel;
private static int clickX = 300;
private static int clickY = 300;
public static void main(String[] args) throws AWTException {
robot = new Robot();
try {
GlobalScreen.registerNativeHook();
} catch (Exception e) {
e.printStackTrace();
}
GlobalScreen.addNativeKeyListener(new MultiCursorSimulator());
JFrame mainWindow = new JFrame("Multi Cursor Simulator");
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setSize(400, 300);
mainWindow.setLayout(new BorderLayout());
JPanel controlPanel = new JPanel(new GridLayout(4, 1));
xSlider = new JSlider(0, 1920, 300); // 0 a 1920 (ajuste conforme necessário)
xSlider.setMajorTickSpacing(200);
xSlider.setMinorTickSpacing(50);
xSlider.setPaintTicks(true);
xSlider.setPaintLabels(true);
xSlider.addChangeListener(e -> updateXPosition());
controlPanel.add(new JLabel("Posição X:"));
controlPanel.add(xSlider);
ySlider = new JSlider(0, 1080, 300); // 0 a 1080 (ajuste conforme necessário)
ySlider.setMajorTickSpacing(200);
ySlider.setMinorTickSpacing(50);
ySlider.setPaintTicks(true);
ySlider.setPaintLabels(true);
ySlider.addChangeListener(e -> updateYPosition());
controlPanel.add(new JLabel("Posição Y:"));
controlPanel.add(ySlider);
JButton simulateClickButton = new JButton("Simular Clique");
simulateClickButton.addActionListener(e -> simulateClick());
controlPanel.add(simulateClickButton);
mainWindow.add(controlPanel, BorderLayout.SOUTH);
mainWindow.setVisible(true);
createOverlayFrame();
}
private static void createOverlayFrame() {
overlayFrame = new JFrame();
overlayFrame.setUndecorated(true);
overlayFrame.setSize(15, 15);
overlayPanel = new OverlayPanel();
overlayFrame.add(overlayPanel);
overlayFrame.setAlwaysOnTop(true);
overlayFrame.setVisible(true);
}
private static void updateXPosition() {
clickX = xSlider.getValue();
updateCirclePosition();
}
private static void updateYPosition() {
clickY = ySlider.getValue();
updateCirclePosition();
}
private static void updateCirclePosition() {
overlayFrame.setLocation(clickX, clickY);
overlayPanel.repaint();
}
private static void simulateClick() {
try {
robot.mouseMove(clickX, clickY);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
System.out.println("Clique simulado na posição: (x: " + clickX + ", y: " + clickY + ")");
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void nativeKeyPressed(NativeKeyEvent e) {
if (e.getKeyCode() == NativeKeyEvent.VC_X) {
simulateClick();
}
}
@Override
public void nativeKeyReleased(NativeKeyEvent e) {
}
@Override
public void nativeKeyTyped(NativeKeyEvent e) {
}
static class OverlayPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(0, 0, 20, 20);
}
}
}
Upvotes: 0
Views: 25