From a6c43fd6469fdbd9ba552080a3cb98dcacbb580a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 5 Aug 2024 21:29:02 +0200 Subject: [PATCH] Improve button and keystrokes handling --- .../java/wtf/beatrice/autosqueal/Main.java | 6 ++- .../autosqueal/listener/KeyPressListener.java | 3 +- .../beatrice/autosqueal/ui/MainWindow.java | 45 +++++++++++++------ .../beatrice/autosqueal/util/RunnerUtil.java | 4 ++ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/main/java/wtf/beatrice/autosqueal/Main.java b/src/main/java/wtf/beatrice/autosqueal/Main.java index 9614974..5a9a8fa 100644 --- a/src/main/java/wtf/beatrice/autosqueal/Main.java +++ b/src/main/java/wtf/beatrice/autosqueal/Main.java @@ -16,13 +16,13 @@ public class Main { private static boolean running = true; private static final Logger LOGGER = LogManager.getLogger(Main.class); + private static final MainWindow mainWindow = new MainWindow(); public static void main(String[] args) { LOGGER.info("Hello world!"); registerJNativeHook(); - MainWindow mainWindow = new MainWindow(); mainWindow.init(); } @@ -41,4 +41,8 @@ public class Main { } } + public static MainWindow getMainWindow() { + return mainWindow; + } + } diff --git a/src/main/java/wtf/beatrice/autosqueal/listener/KeyPressListener.java b/src/main/java/wtf/beatrice/autosqueal/listener/KeyPressListener.java index 623baca..5e5ae67 100644 --- a/src/main/java/wtf/beatrice/autosqueal/listener/KeyPressListener.java +++ b/src/main/java/wtf/beatrice/autosqueal/listener/KeyPressListener.java @@ -6,6 +6,7 @@ import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent; import com.github.kwhat.jnativehook.keyboard.NativeKeyListener; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import wtf.beatrice.autosqueal.Main; import java.util.ArrayList; import java.util.List; @@ -50,7 +51,7 @@ public class KeyPressListener implements NativeKeyListener LOGGER.warn("Received shutdown keystroke: {}", keys); - System.exit(0); + Main.getMainWindow().toggleRunning(); } } } diff --git a/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java b/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java index 02a43fc..38aeda8 100644 --- a/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java +++ b/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java @@ -1,5 +1,6 @@ package wtf.beatrice.autosqueal.ui; +import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import wtf.beatrice.autosqueal.controls.CursorMover; @@ -8,7 +9,6 @@ import wtf.beatrice.autosqueal.util.RunnerUtil; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; import java.util.Timer; @@ -19,22 +19,27 @@ public class MainWindow private static final int WINDOW_HEIGHT = 600; private static final int WINDOW_WIDTH = 800; - private final Timer TIMER_RUNNER = new Timer(); - - private CursorMover cursorMover = null; + private Timer timerRunner = new Timer(); + private CursorMover cursorMover = new CursorMover(); + private Button toggleButton; public MainWindow() { + } public void init() { + JFrame frame = new JFrame(); frame.setSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT)); + frame.setTitle("autosqueal"); + frame.setResizable(false); - Button toggleButton = new Button("Start"); - toggleButton.setBounds(new Rectangle((WINDOW_WIDTH / 2) - 40, WINDOW_HEIGHT - 60, 80, 30)); - toggleButton.addActionListener(e -> toggleRunning(toggleButton)); + toggleButton = new Button(); + toggleButton.setBounds(new Rectangle((WINDOW_WIDTH / 2) - 60, WINDOW_HEIGHT - 60, 120, 30)); + toggleButton.addActionListener(e -> toggleRunning()); frame.add(toggleButton); + toggleRunning(); int bordersPx = 10; int rescaleRateo = ((WINDOW_WIDTH - (2 * bordersPx)) * 100) / RunnerUtil.SCREEN_WIDTH; @@ -61,24 +66,38 @@ public class MainWindow } } - private void toggleRunning(Button button) { + public void toggleRunning() { + + String label; if (cursorMover == null) { + timerRunner = new Timer(); CursorMoveListener cursorMoveListener = new CursorMoveListener(); - TIMER_RUNNER.schedule(cursorMoveListener, 0L, 1000L); + timerRunner.schedule(cursorMoveListener, 0L, 1000L); cursorMover = new CursorMover(); - TIMER_RUNNER.schedule(cursorMover, 1000L, RunnerUtil.SECONDS_BETWEEN_MOVES * 1000L); + timerRunner.schedule(cursorMover, 1000L, RunnerUtil.SECONDS_BETWEEN_MOVES * 1000L); - button.setLabel("Stop"); + label = "Stop [" + + NativeKeyEvent.getKeyText(NativeKeyEvent.VC_CONTROL) + + "][" + + NativeKeyEvent.getKeyText(NativeKeyEvent.VC_ALT) + + "]"; } else { - TIMER_RUNNER.cancel(); + timerRunner.cancel(); cursorMover = null; - button.setLabel("Start"); + label = "Start [" + + NativeKeyEvent.getKeyText(NativeKeyEvent.VC_CONTROL) + + "][" + + NativeKeyEvent.getKeyText(NativeKeyEvent.VC_ALT) + + "]"; + } + toggleButton.setLabel(label); + } } diff --git a/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java b/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java index 6018090..a0046d9 100644 --- a/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java +++ b/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java @@ -4,6 +4,10 @@ import java.awt.*; public class RunnerUtil { + private RunnerUtil() { + throw new AssertionError("The RunnerUtil class is not intended to be instantiated."); + } + public static final int SECONDS_BETWEEN_MOVES = 10; public static final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;