Chodzi o to, że chciałem, aby po kliknięciu przycisku np. 't' startowała się pętla, która non-stop klikała myszką. Ale jeśli podczas tego, użytkownik nacisnął 's', to pętla by się zatrzymała.
Próbowałem z robotem, i keylistenerem, ale nie wiem jak to zrobić ;-;... Pomoże ktoś?
0
0
Tutaj prosta implementacja z dwoma wątkami, pozdrawiam cieplutko :)
public class Klikacz {
public static void main(String[] args) {
final Clicker clicker = new Clicker();
final UserInput userInput = new UserInput(clicker);
final Thread t1 = new Thread(clicker);
final Thread t2 = new Thread(userInput);
t1.start();
t2.start();
}
static class UserInput implements Runnable {
private static final String STOPCHAR = "s";
private static final String STARTCHAR = "t";
private final Clicker clicker;
UserInput(final Clicker clicker) {
this.clicker = clicker;
}
@Override
public void run() {
final Scanner scanner = new Scanner(System.in);
while (true) {
final String userInput = scanner.nextLine();
if (userInput.equals(STARTCHAR)) {
clicker.start();
} else if (userInput.equals(STOPCHAR)) {
clicker.stop();
}
}
}
}
static class Clicker implements Runnable {
private volatile boolean run = false;
void stop() {
this.run = false;
}
void start() {
this.run = true;
}
@Override
public void run() {
try {
final Robot robot = new Robot();
final int mask = InputEvent.BUTTON1_MASK;
while (true) {
if (run) {
robot.mousePress(mask);
robot.mouseRelease(mask);
Thread.sleep(2000);
}
}
} catch (AWTException | InterruptedException e) {
e.printStackTrace();
}
}
}
}
0
Miedziany Szczur napisał(a):
Tutaj prosta implementacja z dwoma wątkami, pozdrawiam cieplutko :)
public class Klikacz { public static void main(String[] args) { final Clicker clicker = new Clicker(); final UserInput userInput = new UserInput(clicker); final Thread t1 = new Thread(clicker); final Thread t2 = new Thread(userInput); t1.start(); t2.start(); } static class UserInput implements Runnable { private static final String STOPCHAR = "s"; private static final String STARTCHAR = "t"; private final Clicker clicker; UserInput(final Clicker clicker) { this.clicker = clicker; } @Override public void run() { final Scanner scanner = new Scanner(System.in); while (true) { final String userInput = scanner.nextLine(); if (userInput.equals(STARTCHAR)) { clicker.start(); } else if (userInput.equals(STOPCHAR)) { clicker.stop(); } } } } static class Clicker implements Runnable { private volatile boolean run = false; void stop() { this.run = false; } void start() { this.run = true; } @Override public void run() { try { final Robot robot = new Robot(); final int mask = InputEvent.BUTTON1_MASK; while (true) { if (run) { robot.mousePress(mask); robot.mouseRelease(mask); Thread.sleep(2000); } } } catch (AWTException | InterruptedException e) { e.printStackTrace(); } } } }
No właśnie... Wątkami... Nie mam ich ogarniętych... Dziękuję za to!